如何使用 Ansible 模块 k8s 自动化创建 Kubernetes K8s 或 OpenShift OCP 中的“nginx”Pod,将其部署在“example”命名空间中。

Ansible 创建 Kubernetes 或 OpenShift Pod

Ansible 模块 k8s

kubernetes.core.k8s 管理 Kubernetes (K8s) 对象

让我们来探讨 Ansible 的 k8s 模块。它的全名是 kubernetes.core.k8s,属于 Ansible 用于与 Kubernetes 和 Red Hat OpenShift 集群交互的模块集合。它用于管理 Kubernetes (K8s) 对象。

参数说明

  • name(字符串)/ namespace(字符串):对象名称/命名空间。
  • api_version(字符串):API 版本,默认为“v1”。
  • kind(字符串):对象模型类型。
  • state(字符串):present/absent/patched,指定对象的状态。
  • definition(字符串):YAML 定义。
  • src(路径):YAML 定义文件的路径。
  • template(原始):YAML 模板定义。
  • validate(字典):验证资源定义。

k8s 模块有大量参数,以下是最常用的几个:

  • name 和 namespace 用于指定对象名称和命名空间,适用于创建、删除或发现对象,无需提供完整的资源定义。
  • api_version 指定 Kubernetes API 版本,默认为“v1”。
  • kind 指定对象模型类型。
  • state 决定对象是创建(present)、更新(patched)还是删除(absent)。
  • definition 允许提供一个有效的 YAML 定义(字符串、列表或字典),用于创建或更新对象。
  • src 提供一个包含有效 YAML 定义的文件路径。
  • template 可以指定 YAML 定义模板。
  • validate 用于定义如何根据 Kubernetes 模式验证资源定义,需安装 kubernetes-validate Python 模块。

示例代码

Ansible Playbook (ansible_playbook.yml)

---
- name: k8s demo
  hosts: localhost
  gather_facts: false
  connection: local
  vars:
    myproject: "example"
  tasks:
    - name: create k8s pod
      kubernetes.core.k8s:
        src: mypod.yaml
        namespace: "{{ myproject }}"
        state: present

Pod 定义文件 (mypod.yaml)

---
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
    - name: nginx
      image: nginx:1.21.6
      ports:
        - containerPort: 80

执行过程

执行命令

ansible-pilot $ ansible-playbook kubernetes/pod.yml

输出结果

[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [k8s demo] ******************************************************
TASK [create k8s pod] ***********************************************
changed: [localhost]
PLAY RECAP ***********************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

幂等性测试

再次运行 Playbook 时,不会重复执行已成功完成的任务:

ansible-pilot $ ansible-playbook kubernetes/pod.yml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [k8s demo] ******************************************************
TASK [create k8s pod] ***********************************************
ok: [localhost]
PLAY RECAP ***********************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

执行前

在执行 Playbook 之前,目标命名空间中不存在名为“nginx”的 Pod:

Kubernetes (k8s)

ansible-pilot $ kubectl get pods -n example
No resources found in example namespace.

OpenShift (OCP)

ansible-pilot $ oc get pods -n example
No resources found in example namespace.

执行后

执行 Playbook 后,名为“nginx”的 Pod 已成功创建并运行:

Kubernetes (k8s)

ansible-pilot $ kubectl get pods -n example
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          5m31s

OpenShift (OCP)

ansible-pilot $ oc get pods -n example
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          5m31s

Nginx Pod 日志

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/04/12 10:59:16 [notice] 1#1: using the "epoll" event method
2022/04/12 10:59:16 [notice] 1#1: nginx/1.21.6
2022/04/12 10:59:16 [notice] 1#1: built by gcc 10.2.1 20201101 (Debian 10.2.1-6)
2022/04/12 10:59:16 [notice] 1#1: OS: Linux 4.18.0-305.30.1.el8_4.x86_64
2022/04/12 10:59:16 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/04/12 10:59:16 [notice] 1#1: start worker processes
2022/04/12 10:59:16 [notice] 1#1: start worker process 33
2022/04/12 10:59:16 [notice] 1#1: start worker process 34
2022/04/12 10:59:16 [notice] 1#1: start worker process 35
2022/04/12 10:59:16 [notice] 1#1: start worker process 36

通过以上步骤,你可以使用 Ansible 在 Kubernetes 或 OpenShift 集群中自动化创建 Nginx Pod,并确保其正常运行。