如何使用 Ansible 模块 k8s 自动化创建名为“myapp”的命名空间项目,适用于 Kubernetes (K8s) 或 OpenShift (OCP)。

Ansible 创建 Kubernetes 或 OpenShift 命名空间项目

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,用于创建名为“myapp”的命名空间项目:

---
- name: k8s demo
  hosts: localhost
  gather_facts: false
  connection: local
  vars:
    project_name: "myapp"
  tasks:
    - name: create {{ project_name }} namespace
      kubernetes.core.k8s:
        api_version: v1
        kind: Namespace
        name: "{{ project_name }}"
        state: present

执行过程

执行命令

ansible-pilot $ ansible-playbook kubernetes/namespace.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 myapp namespace] ****************************************
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/namespace.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 myapp namespace] ****************************************
ok: [localhost]
PLAY RECAP ***********************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

执行前

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

ansible-pilot $ oc projects | grep myapp
ansible-pilot $ oc projects
You have access to the following projects and can switch between them with 'oc project <projectname>':
default
  * example
    kube-node-lease
    kube-public
    kube-system
    openshift
    openshift-apiserver
    openshift-apiserver-operator
    openshift-authentication
    openshift-authentication-operator
    openshift-cloud-controller-manager
    openshift-cloud-controller-manager-operator
    openshift-cloud-credential-operator
    openshift-cluster-csi-drivers
    openshift-cluster-machine-approver
    openshift-cluster-node-tuning-operator
    openshift-cluster-samples-operator
    openshift-cluster-storage-operator
    openshift-cluster-version
    openshift-config
    openshift-config-managed
    openshift-config-operator
    openshift-console
    openshift-console-operator
    openshift-console-user-settings
    openshift-controller-manager
    openshift-controller-manager-operator
    openshift-dns
    openshift-dns-operator
    openshift-etcd
    openshift-etcd-operator
    openshift-host-network
    openshift-image-registry
    openshift-infra
    openshift-ingress
    openshift-ingress-canary
    openshift-ingress-operator
    openshift-insights
    openshift-kni-infra
    openshift-kube-apiserver
    openshift-kube-apiserver-operator
    openshift-kube-controller-manager
    openshift-kube-controller-manager-operator
    openshift-kube-scheduler
    openshift-kube-scheduler-operator
    openshift-kube-storage-version-migrator-operator
    openshift-kubevirt-infra
    openshift-machine-api
    openshift-machine-config-operator
    openshift-marketplace
    openshift-monitoring
    openshift-multus
    openshift-network-diagnostics
    openshift-network-operator
    openshift-node
    openshift-oauth-apiserver
    openshift-openstack-infra
    openshift-operator-lifecycle-manager
    openshift-operators
    openshift-ovirt-infra
    openshift-sdn
    openshift-service-ca
    openshift-service-ca-operator
    openshift-user-workload-monitoring
    openshift-vsphere-infra
Using project "example" on server "https://api.crc.testing:6443".

执行后

执行 Playbook 后,名为“myapp”的命名空间已成功创建:

ansible-pilot $ oc projects | grep myapp
    myapp
ansible-pilot $ oc projects
You have access to the following projects and can switch between them with 'oc project <projectname>':
default
  * example
    kube-node-lease
    kube-public
    kube-system
    myapp
    openshift
    openshift-apiserver
    openshift-apiserver-operator
    openshift-authentication
    openshift-authentication-operator
    openshift-cloud-controller-manager
    openshift-cloud-controller-manager-operator
    openshift-cloud-credential-operator
    openshift-cluster-csi-drivers
    openshift-cluster-machine-approver
    openshift-cluster-node-tuning-operator
    openshift-cluster-samples-operator
    openshift-cluster-storage-operator
    openshift-cluster-version
    openshift-config
    openshift-config-managed
    openshift-config-operator
    openshift-console
    openshift-console-operator
    openshift-console-user-settings
    openshift-controller-manager
    openshift-controller-manager-operator
    openshift-dns
    openshift-dns-operator
    openshift-etcd
    openshift-etcd-operator
    openshift-host-network
    openshift-image-registry
    openshift-infra
    openshift-ingress
    openshift-ingress-canary
    openshift-ingress-operator
    openshift-insights
    openshift-kni-infra
    openshift-kube-apiserver
    openshift-kube-apiserver-operator
    openshift-kube-controller-manager
    openshift-kube-controller-manager-operator
    openshift-kube-scheduler
    openshift-kube-scheduler-operator
    openshift-kube-storage-version-migrator-operator
    openshift-kubevirt-infra
    openshift-machine-api
    openshift-machine-config-operator
    openshift-marketplace
    openshift-monitoring
    openshift-multus
    openshift-network-diagnostics
    openshift-network-operator
    openshift-node
    openshift-oauth-apiserver
    openshift-openstack-infra
    openshift-operator-lifecycle-manager
    openshift-operators
    openshift-ovirt-infra
    openshift-sdn
    openshift-service-ca
    openshift-service-ca-operator
    openshift-user-workload-monitoring
    openshift-vsphere-infra
Using project "example" on server "https://api.crc.testing:6443".

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