Ansible kubernetes.core 模块的使用
介绍
Ansible Guide: https://docs.ansible.com/ansible/latest/collections/kubernetes/core/index.html#description
code: https://github.com/ansible-collections/kubernetes.core
模块列表:https://github.com/ansible-collections/kubernetes.core#modules
kubernetes.core可以自动化管理kubernetes或openshift集群的应用与资源对象,以及集群的运维管理。
该collection环境要求:
kubernetes >= 1.19
python >= 3.6
ansible >= 2.9.17
Modules
kubernetes.core collection包含有多个模块插件:inventory plugin, lookup plugin, connection plugin, K8s filter plugin以及多个Model
Name | Description |
---|---|
kubernetes.core.helm | Manages Kubernetes packages with the Helm package manager |
kubernetes.core.helm_info | Get information from Helm package deployed inside the cluster |
kubernetes.core.helm_plugin | Manage Helm plugins |
kubernetes.core.helm_plugin_info | Gather information about Helm plugins |
kubernetes.core.helm_pull | download a chart from a repository and (optionally) unpack it in local directory. |
kubernetes.core.helm_repository | Manage Helm repositories. |
kubernetes.core.helm_template | Render chart templates |
kubernetes.core.k8s | Manage Kubernetes (K8s) objects |
kubernetes.core.k8s_cluster_info | Describe Kubernetes (K8s) cluster, APIs available and their respective versions |
kubernetes.core.k8s_cp | Copy files and directories to and from pod. |
kubernetes.core.k8s_drain | Drain, Cordon, or Uncordon node in k8s cluster |
kubernetes.core.k8s_exec | Execute command in Pod |
kubernetes.core.k8s_info | Describe Kubernetes (K8s) objects |
kubernetes.core.k8s_json_patch | Apply JSON patch operations to existing objects |
kubernetes.core.k8s_log | Fetch logs from Kubernetes resources |
kubernetes.core.k8s_rollback | Rollback Kubernetes (K8S) Deployments and DaemonSets |
kubernetes.core.k8s_scale | Set a new size for a Deployment, ReplicaSet, Replication Controller, or Job. |
kubernetes.core.k8s_service | Manage Services on Kubernetes |
kubernetes.core.k8s_taint | Taint a node in a Kubernetes/OpenShift cluster |
准备环境
查看当前支持的inventory插件
1 | ❯ ansible-doc -t inventory -l | grep kubernetes |
判断是否安装了kubernetes.core collection
1 | ❯ ansible-galaxy collection list | grep kubernetes |
如果没有的话,需要安装
1 | ❯ ansible-galaxy collection install kubernetes.core |
配置inventory
kubernetes.core.k8s inventory支持多种方式配置,如token, kubeconfig
1 | ❯ cat k8s.yaml |
配置完成inventory后,可以查看inventory内容
1 | ❯ ansible-inventory -i k8s.yaml --list > out |
该inventory将根据namespace, 及label信息等为k8s中的资源创建了很多组,方便在运行具体tasks时进行选择。同时为pod默认配置了连接方式 "ansible_connection": "kubernetes.core.kubectl",
运行ansible playbook
ansible将通过kubectl工具,在相关的pod中运行指定的模块,其中pod就相当于一台机器,它必须安装有python环境,才能正常运行模块。
1 | --- |
运行 playbook
1 | ansible-playbook -i k8s.yaml a.yaml |
使用kubernetes.core.k8s_exec模块在指定的pod中运行命令
1 | - hosts: localhost |
运行Playbook
1 | ❯ ansible-playbook main.yaml |
对于其它模块可以参考它的说明,配置相关的参数。