Ansible的k8s模块完全兼容OpenShift资源
2019-08-12 本文已影响8人
潘晓华Michael
Ansible和Openshift
Ansible的k8s模块完全兼容openshift资源
安装
使用k8s模块需要安装openshift的python扩展
$ pip install --ignore-installed openshift
安装过程中报如下错误
TypeError: find_packages() got an unexpected keyword argument include
需要升级setuptools
$ pip install -U setuptools
在执行k8s命令的主机上登录kubernetes/openshift平台
$ oc login https://master.example.com:8443 -u admin -p
password
测试
使用ansible k8s模块创建namespace
$ oc -i hosts all -m k8s -a 'state=present name=testproject kind=Project'
使用ansible k8s模块基于dc.yaml文件创建dc
- 创建dc.yaml文件
$ oc run nginx --image=nginx -n testproject --dry-run -o yaml
将内容输入到nginx-dc.yaml文件中,去掉些默认值,添加namespace
$ cat nginx-dc.yaml
apiVersion: v1
kind: DeploymentConfig
metadata:
labels:
run: nginx
name: nginx
namespace: testproject
spec:
replicas: 1
selector:
run: nginx
template:
metadata:
labels:
run: nginx
spec:
containers:
- image: nginx
name: nginx
- 使用ansible创建
$ ansible -i hosts all -m k8s -a 'state=present src=/root/sample/nginx-dc.yaml'
使用ansible k8s模块基于模板文件创建dc
- 创建playbook文件
---
- hosts: all
tasks:
- name: create nginx yaml
k8s:
state: present
definition: "{{ lookup('template', '/root/sample/dc.yaml') | from_yaml }}"
EOF
- 使用ansible-playbook创建nginx应用
$ ansible-playbook -i hosts playbook.yaml
删除nginx DeploymentConfig
$ ansible -i hosts all -m k8s -a 'state=absent kind=DeploymentConfig namespace=testproject name=nginx'