7d48a7fdf615762d66b8b468864267af9a99d40c
[it/test.git] / XTesting / kubespray / docs / ansible.md
1 # Ansible
2
3 ## Installing Ansible
4
5 Kubespray supports multiple ansible versions and ships different `requirements.txt` files for them.
6 Depending on your available python version you may be limited in chooding which ansible version to use.
7
8 It is recommended to deploy the ansible version used by kubespray into a python virtual environment.
9
10 ```ShellSession
11 VENVDIR=kubespray-venv
12 KUBESPRAYDIR=kubespray
13 ANSIBLE_VERSION=2.12
14 virtualenv  --python=$(which python3) $VENVDIR
15 source $VENVDIR/bin/activate
16 cd $KUBESPRAYDIR
17 pip install -U -r requirements-$ANSIBLE_VERSION.txt
18 test -f requirements-$ANSIBLE_VERSION.yml && \
19   ansible-galaxy role install -r requirements-$ANSIBLE_VERSION.yml && \
20   ansible-galaxy collection -r requirements-$ANSIBLE_VERSION.yml
21 ```
22
23 ### Ansible Python Compatibility
24
25 Based on the table below and the available python version for your ansible host you should choose the appropriate ansible version to use with kubespray.
26
27 | Ansible Version | Python Version |
28 | --------------- | -------------- |
29 | 2.11            | 2.7,3.5-3.9    |
30 | 2.12            | 3.8-3.10       |
31
32 ## Inventory
33
34 The inventory is composed of 3 groups:
35
36 * **kube_node** : list of kubernetes nodes where the pods will run.
37 * **kube_control_plane** : list of servers where kubernetes control plane components (apiserver, scheduler, controller) will run.
38 * **etcd**: list of servers to compose the etcd server. You should have at least 3 servers for failover purpose.
39
40 Note: do not modify the children of _k8s_cluster_, like putting
41 the _etcd_ group into the _k8s_cluster_, unless you are certain
42 to do that and you have it fully contained in the latter:
43
44 ```ShellSession
45 etcd ⊂ k8s_cluster => kube_node ∩ etcd = etcd
46 ```
47
48 When _kube_node_ contains _etcd_, you define your etcd cluster to be as well schedulable for Kubernetes workloads.
49 If you want it a standalone, make sure those groups do not intersect.
50 If you want the server to act both as control-plane and node, the server must be defined
51 on both groups _kube_control_plane_ and _kube_node_. If you want a standalone and
52 unschedulable control plane, the server must be defined only in the _kube_control_plane_ and
53 not _kube_node_.
54
55 There are also two special groups:
56
57 * **calico_rr** : explained for [advanced Calico networking cases](/docs/calico.md)
58 * **bastion** : configure a bastion host if your nodes are not directly reachable
59
60 Below is a complete inventory example:
61
62 ```ini
63 ## Configure 'ip' variable to bind kubernetes services on a
64 ## different ip than the default iface
65 node1 ansible_host=95.54.0.12 ip=10.3.0.1
66 node2 ansible_host=95.54.0.13 ip=10.3.0.2
67 node3 ansible_host=95.54.0.14 ip=10.3.0.3
68 node4 ansible_host=95.54.0.15 ip=10.3.0.4
69 node5 ansible_host=95.54.0.16 ip=10.3.0.5
70 node6 ansible_host=95.54.0.17 ip=10.3.0.6
71
72 [kube_control_plane]
73 node1
74 node2
75
76 [etcd]
77 node1
78 node2
79 node3
80
81 [kube_node]
82 node2
83 node3
84 node4
85 node5
86 node6
87
88 [k8s_cluster:children]
89 kube_node
90 kube_control_plane
91 ```
92
93 ## Group vars and overriding variables precedence
94
95 The group variables to control main deployment options are located in the directory ``inventory/sample/group_vars``.
96 Optional variables are located in the `inventory/sample/group_vars/all.yml`.
97 Mandatory variables that are common for at least one role (or a node group) can be found in the
98 `inventory/sample/group_vars/k8s_cluster.yml`.
99 There are also role vars for docker, kubernetes preinstall and control plane roles.
100 According to the [ansible docs](https://docs.ansible.com/ansible/latest/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable),
101 those cannot be overridden from the group vars. In order to override, one should use
102 the `-e` runtime flags (most simple way) or other layers described in the docs.
103
104 Kubespray uses only a few layers to override things (or expect them to
105 be overridden for roles):
106
107 Layer | Comment
108 ------|--------
109 **role defaults** | provides best UX to override things for Kubespray deployments
110 inventory vars | Unused
111 **inventory group_vars** | Expects users to use ``all.yml``,``k8s_cluster.yml`` etc. to override things
112 inventory host_vars | Unused
113 playbook group_vars | Unused
114 playbook host_vars | Unused
115 **host facts** | Kubespray overrides for internal roles' logic, like state flags
116 play vars | Unused
117 play vars_prompt | Unused
118 play vars_files | Unused
119 registered vars | Unused
120 set_facts | Kubespray overrides those, for some places
121 **role and include vars** | Provides bad UX to override things! Use extra vars to enforce
122 block vars (only for tasks in block) | Kubespray overrides for internal roles' logic
123 task vars (only for the task) | Unused for roles, but only for helper scripts
124 **extra vars** (always win precedence) | override with ``ansible-playbook -e @foo.yml``
125
126 ## Ansible tags
127
128 The following tags are defined in playbooks:
129
130 |                       Tag name | Used for
131 |--------------------------------|---------
132 |                       annotate | Create kube-router annotation
133 |                           apps | K8s apps definitions
134 |                        asserts | Check tasks for download role
135 |             aws-ebs-csi-driver | Configuring csi driver: aws-ebs
136 |               azure-csi-driver | Configuring csi driver: azure
137 |                        bastion | Setup ssh config for bastion
138 |                   bootstrap-os | Anything related to host OS configuration
139 |                         calico | Network plugin Calico
140 |                      calico_rr | Configuring Calico route reflector
141 |                          canal | Network plugin Canal
142 |             cephfs-provisioner | Configuring CephFS
143 |                   cert-manager | Configuring certificate manager for K8s
144 |                         cilium | Network plugin Cilium
145 |              cinder-csi-driver | Configuring csi driver: cinder
146 |                         client | Kubernetes clients role
147 |                 cloud-provider | Cloud-provider related tasks
148 |                  cluster-roles | Configuring cluster wide application (psp ...)
149 |                            cni | CNI plugins for Network Plugins
150 |                     containerd | Configuring containerd engine runtime for hosts
151 |   container_engine_accelerator | Enable nvidia accelerator for runtimes
152 |               container-engine | Configuring container engines
153 |             container-runtimes | Configuring container runtimes
154 |                        coredns | Configuring coredns deployment
155 |                           crio | Configuring crio container engine for hosts
156 |                           crun | Configuring crun runtime
157 |                     csi-driver | Configuring csi driver
158 |                      dashboard | Installing and configuring the Kubernetes Dashboard
159 |                            dns | Remove dns entries when resetting
160 |                         docker | Configuring docker engine runtime for hosts
161 |                       download | Fetching container images to a delegate host
162 |                           etcd | Configuring etcd cluster
163 |                   etcd-secrets | Configuring etcd certs/keys
164 |                       etchosts | Configuring /etc/hosts entries for hosts
165 |      external-cloud-controller | Configure cloud controllers
166 |             external-openstack | Cloud controller : openstack
167 |           external-provisioner | Configure external provisioners
168 |               external-vsphere | Cloud controller : vsphere
169 |                          facts | Gathering facts and misc check results
170 |                          files | Remove files when resetting
171 |                        flannel | Network plugin flannel
172 |                            gce | Cloud-provider GCP
173 |              gcp-pd-csi-driver | Configuring csi driver: gcp-pd
174 |                         gvisor | Configuring gvisor runtime
175 |                           helm | Installing and configuring Helm
176 |             ingress-controller | Configure ingress controllers
177 |                    ingress_alb | AWS ALB Ingress Controller
178 |                           init | Windows kubernetes init nodes
179 |                       iptables | Flush and clear iptable when resetting
180 |                k8s-pre-upgrade | Upgrading K8s cluster
181 |                    k8s-secrets | Configuring K8s certs/keys
182 |                 k8s-gen-tokens | Configuring K8s tokens
183 |                kata-containers | Configuring kata-containers runtime
184 |                           krew | Install and manage krew
185 |                        kubeadm | Roles linked to kubeadm tasks
186 |                 kube-apiserver | Configuring static pod kube-apiserver
187 |        kube-controller-manager | Configuring static pod kube-controller-manager
188 |                       kube-vip | Installing and configuring kube-vip
189 |                        kubectl | Installing kubectl and bash completion
190 |                        kubelet | Configuring kubelet service
191 |                       kube-ovn | Network plugin kube-ovn
192 |                    kube-router | Network plugin kube-router
193 |                     kube-proxy | Configuring static pod kube-proxy
194 |                      localhost | Special steps for the localhost (ansible runner)
195 |         local-path-provisioner | Configure External provisioner: local-path
196 |       local-volume-provisioner | Configure External provisioner: local-volume
197 |                        macvlan | Network plugin macvlan
198 |                         master | Configuring K8s master node role
199 |                        metallb | Installing and configuring metallb
200 |                 metrics_server | Configuring metrics_server
201 |                     netchecker | Installing netchecker K8s app
202 |                        network | Configuring networking plugins for K8s
203 |                         mounts | Umount kubelet dirs when reseting
204 |                         multus | Network plugin multus
205 |                          nginx | Configuring LB for kube-apiserver instances
206 |                           node | Configuring K8s minion (compute) node role
207 |                   nodelocaldns | Configuring nodelocaldns daemonset
208 |                     node-label | Tasks linked to labeling of nodes
209 |                   node-webhook | Tasks linked to webhook (grating access to resources)
210 |                     nvidia_gpu | Enable nvidia accelerator for runtimes
211 |                            oci | Cloud provider: oci
212 |             persistent_volumes | Configure csi volumes
213 | persistent_volumes_aws_ebs_csi | Configuring csi driver: aws-ebs
214 | persistent_volumes_cinder_csi  | Configuring csi driver: cinder
215 | persistent_volumes_gcp_pd_csi  | Configuring csi driver: gcp-pd
216 | persistent_volumes_openstack   | Configuring csi driver: openstack
217 |              policy-controller | Configuring Calico policy controller
218 |                    post-remove | Tasks running post-remove operation
219 |                   post-upgrade | Tasks running post-upgrade operation
220 |                     pre-remove | Tasks running pre-remove operation
221 |                    pre-upgrade | Tasks running pre-upgrade operation
222 |                     preinstall | Preliminary configuration steps
223 |                       registry | Configuring local docker registry
224 |                          reset | Tasks running doing the node reset
225 |                     resolvconf | Configuring /etc/resolv.conf for hosts/apps
226 |                rbd-provisioner | Configure External provisioner: rdb
227 |                       services | Remove services (etcd, kubelet etc...) when resetting
228 |                       snapshot | Enabling csi snapshot
229 |            snapshot-controller | Configuring csi snapshot controller
230 |                        upgrade | Upgrading, f.e. container images/binaries
231 |                         upload | Distributing images/binaries across hosts
232 |             vsphere-csi-driver | Configuring csi driver: vsphere
233 |                          weave | Network plugin Weave
234 |                      win_nodes | Running windows specific tasks
235 |                          youki | Configuring youki runtime
236
237 Note: Use the ``bash scripts/gen_tags.sh`` command to generate a list of all
238 tags found in the codebase. New tags will be listed with the empty "Used for"
239 field.
240
241 ## Example commands
242
243 Example command to filter and apply only DNS configuration tasks and skip
244 everything else related to host OS configuration and downloading images of containers:
245
246 ```ShellSession
247 ansible-playbook -i inventory/sample/hosts.ini cluster.yml --tags preinstall,facts --skip-tags=download,bootstrap-os
248 ```
249
250 And this play only removes the K8s cluster DNS resolver IP from hosts' /etc/resolv.conf files:
251
252 ```ShellSession
253 ansible-playbook -i inventory/sample/hosts.ini -e dns_mode='none' cluster.yml --tags resolvconf
254 ```
255
256 And this prepares all container images locally (at the ansible runner node) without installing
257 or upgrading related stuff or trying to upload container to K8s cluster nodes:
258
259 ```ShellSession
260 ansible-playbook -i inventory/sample/hosts.ini cluster.yml \
261     -e download_run_once=true -e download_localhost=true \
262     --tags download --skip-tags upload,upgrade
263 ```
264
265 Note: use `--tags` and `--skip-tags` wise and only if you're 100% sure what you're doing.
266
267 ## Bastion host
268
269 If you prefer to not make your nodes publicly accessible (nodes with private IPs only),
270 you can use a so called *bastion* host to connect to your nodes. To specify and use a bastion,
271 simply add a line to your inventory, where you have to replace x.x.x.x with the public IP of the
272 bastion host.
273
274 ```ShellSession
275 [bastion]
276 bastion ansible_host=x.x.x.x
277 ```
278
279 For more information about Ansible and bastion hosts, read
280 [Running Ansible Through an SSH Bastion Host](https://blog.scottlowe.org/2015/12/24/running-ansible-through-ssh-bastion-host/)
281
282 ## Mitogen
283
284 Mitogen support is deprecated, please see [mitogen related docs](/docs/mitogen.md) for useage and reasons for deprecation.
285
286 ## Beyond ansible 2.9
287
288 Ansible project has decided, in order to ease their maintenance burden, to split between
289 two projects which are now joined under the Ansible umbrella.
290
291 Ansible-base (2.10.x branch) will contain just the ansible language implementation while
292 ansible modules that were previously bundled into a single repository will be part of the
293 ansible 3.x package. Pleasee see [this blog post](https://blog.while-true-do.io/ansible-release-3-0-0/)
294 that explains in detail the need and the evolution plan.
295
296 **Note:** this change means that ansible virtual envs cannot be upgraded with `pip install -U`.
297 You first need to uninstall your old ansible (pre 2.10) version and install the new one.
298
299 ```ShellSession
300 pip uninstall ansible ansible-base ansible-core
301 cd kubespray/
302 pip install -U .
303 ```