You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
14 KiB

2 years ago
  1. # Ansible
  2. ## Installing Ansible
  3. Kubespray supports multiple ansible versions and ships different `requirements.txt` files for them.
  4. Depending on your available python version you may be limited in choosing which ansible version to use.
  5. It is recommended to deploy the ansible version used by kubespray into a python virtual environment.
  6. ```ShellSession
  7. VENVDIR=kubespray-venv
  8. KUBESPRAYDIR=kubespray
  9. python3 -m venv $VENVDIR
  10. source $VENVDIR/bin/activate
  11. cd $KUBESPRAYDIR
  12. pip install -U -r requirements.txt
  13. ```
  14. In case you have a similar message when installing the requirements:
  15. ```ShellSession
  16. ERROR: Could not find a version that satisfies the requirement ansible==7.6.0 (from -r requirements.txt (line 1)) (from versions: [...], 6.7.0)
  17. ERROR: No matching distribution found for ansible==7.6.0 (from -r requirements.txt (line 1))
  18. ```
  19. It means that the version of Python you are running is not compatible with the version of Ansible that Kubespray supports.
  20. If the latest version supported according to pip is 6.7.0 it means you are running Python 3.8 or lower while you need at least Python 3.9 (see the table below).
  21. ### Ansible Python Compatibility
  22. 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.
  23. | Ansible Version | Python Version |
  24. |-----------------|----------------|
  25. | >= 2.16.4 | 3.10-3.12 |
  26. ## Customize Ansible vars
  27. Kubespray expects users to use one of the following variables sources for settings and customization:
  28. | Layer | Comment |
  29. |----------------------------------------|------------------------------------------------------------------------------|
  30. | inventory vars | |
  31. | - **inventory group_vars** | most used |
  32. | - inventory host_vars | host specifc vars overrides, group_vars is usually more practical |
  33. | **extra vars** (always win precedence) | override with ``ansible-playbook -e @foo.yml`` |
  34. [!IMPORTANT]
  35. Extra vars are best used to override kubespray internal variables, for instances, roles/vars/.
  36. Those vars are usually **not expected** (by Kubespray developers) to be modified by end users, and not part of Kubespray
  37. interface. Thus they can change, disappear, or break stuff unexpectedly.
  38. ## Ansible tags
  39. The following tags are defined in playbooks:
  40. | Tag name | Used for |
  41. |--------------------------------|-------------------------------------------------------|
  42. | annotate | Create kube-router annotation |
  43. | apps | K8s apps definitions |
  44. | asserts | Check tasks for download role |
  45. | aws-ebs-csi-driver | Configuring csi driver: aws-ebs |
  46. | azure-csi-driver | Configuring csi driver: azure |
  47. | bastion | Setup ssh config for bastion |
  48. | bootstrap-os | Anything related to host OS configuration |
  49. | calico | Network plugin Calico |
  50. | calico_rr | Configuring Calico route reflector |
  51. | cephfs-provisioner | Configuring CephFS |
  52. | cert-manager | Configuring certificate manager for K8s |
  53. | cilium | Network plugin Cilium |
  54. | cinder-csi-driver | Configuring csi driver: cinder |
  55. | client | Kubernetes clients role |
  56. | cloud-provider | Cloud-provider related tasks |
  57. | cluster-roles | Configuring cluster wide application (psp ...) |
  58. | cni | CNI plugins for Network Plugins |
  59. | containerd | Configuring containerd engine runtime for hosts |
  60. | container_engine_accelerator | Enable nvidia accelerator for runtimes |
  61. | container-engine | Configuring container engines |
  62. | container-runtimes | Configuring container runtimes |
  63. | control-plane | Configuring K8s control plane node role |
  64. | coredns | Configuring coredns deployment |
  65. | crio | Configuring crio container engine for hosts |
  66. | crun | Configuring crun runtime |
  67. | csi-driver | Configuring csi driver |
  68. | dashboard | Installing and configuring the Kubernetes Dashboard |
  69. | dns | Remove dns entries when resetting |
  70. | docker | Configuring docker engine runtime for hosts |
  71. | download | Fetching container images to a delegate host |
  72. | etcd | Configuring etcd cluster |
  73. | etcd-secrets | Configuring etcd certs/keys |
  74. | etchosts | Configuring /etc/hosts entries for hosts |
  75. | external-cloud-controller | Configure cloud controllers |
  76. | external-openstack | Cloud controller : openstack |
  77. | external-provisioner | Configure external provisioners |
  78. | external-vsphere | Cloud controller : vsphere |
  79. | facts | Gathering facts and misc check results |
  80. | files | Remove files when resetting |
  81. | flannel | Network plugin flannel |
  82. | gce | Cloud-provider GCP |
  83. | gcp-pd-csi-driver | Configuring csi driver: gcp-pd |
  84. | gvisor | Configuring gvisor runtime |
  85. | helm | Installing and configuring Helm |
  86. | ingress-controller | Configure ingress controllers |
  87. | ingress_alb | AWS ALB Ingress Controller |
  88. | init | Windows kubernetes init nodes |
  89. | iptables | Flush and clear iptable when resetting |
  90. | k8s-pre-upgrade | Upgrading K8s cluster |
  91. | kata-containers | Configuring kata-containers runtime |
  92. | kubeadm | Roles linked to kubeadm tasks |
  93. | kube-apiserver | Configuring static pod kube-apiserver |
  94. | kube-controller-manager | Configuring static pod kube-controller-manager |
  95. | kube-vip | Installing and configuring kube-vip |
  96. | kubectl | Installing kubectl and bash completion |
  97. | kubelet | Configuring kubelet service |
  98. | kube-ovn | Network plugin kube-ovn |
  99. | kube-router | Network plugin kube-router |
  100. | kube-proxy | Configuring static pod kube-proxy |
  101. | localhost | Special steps for the localhost (ansible runner) |
  102. | local-path-provisioner | Configure External provisioner: local-path |
  103. | local-volume-provisioner | Configure External provisioner: local-volume |
  104. | macvlan | Network plugin macvlan |
  105. | master (DEPRECATED) | Deprecated - see `control-plane` |
  106. | metallb | Installing and configuring metallb |
  107. | metrics_server | Configuring metrics_server |
  108. | netchecker | Installing netchecker K8s app |
  109. | network | Configuring networking plugins for K8s |
  110. | mounts | Umount kubelet dirs when reseting |
  111. | multus | Network plugin multus |
  112. | nginx | Configuring LB for kube-apiserver instances |
  113. | node | Configuring K8s minion (compute) node role |
  114. | nodelocaldns | Configuring nodelocaldns daemonset |
  115. | node-label | Tasks linked to labeling of nodes |
  116. | node-webhook | Tasks linked to webhook (granting access to resources)|
  117. | nvidia_gpu | Enable nvidia accelerator for runtimes |
  118. | oci | Cloud provider: oci |
  119. | persistent_volumes | Configure csi volumes |
  120. | persistent_volumes_aws_ebs_csi | Configuring csi driver: aws-ebs |
  121. | persistent_volumes_cinder_csi | Configuring csi driver: cinder |
  122. | persistent_volumes_gcp_pd_csi | Configuring csi driver: gcp-pd |
  123. | persistent_volumes_openstack | Configuring csi driver: openstack |
  124. | policy-controller | Configuring Calico policy controller |
  125. | post-remove | Tasks running post-remove operation |
  126. | post-upgrade | Tasks running post-upgrade operation |
  127. | pre-remove | Tasks running pre-remove operation |
  128. | pre-upgrade | Tasks running pre-upgrade operation |
  129. | preinstall | Preliminary configuration steps |
  130. | registry | Configuring local docker registry |
  131. | reset | Tasks running doing the node reset |
  132. | resolvconf | Configuring /etc/resolv.conf for hosts/apps |
  133. | rbd-provisioner | Configure External provisioner: rdb |
  134. | services | Remove services (etcd, kubelet etc...) when resetting |
  135. | snapshot | Enabling csi snapshot |
  136. | snapshot-controller | Configuring csi snapshot controller |
  137. | system-packages | Install packages using OS package manager |
  138. | upgrade | Upgrading, f.e. container images/binaries |
  139. | upload | Distributing images/binaries across hosts |
  140. | vsphere-csi-driver | Configuring csi driver: vsphere |
  141. | weave | Network plugin Weave |
  142. | win_nodes | Running windows specific tasks |
  143. | youki | Configuring youki runtime |
  144. Note: Use the ``bash scripts/gen_tags.sh`` command to generate a list of all
  145. tags found in the codebase. New tags will be listed with the empty "Used for"
  146. field.
  147. ## Example commands
  148. Example command to filter and apply only DNS configuration tasks and skip
  149. everything else related to host OS configuration and downloading images of containers:
  150. ```ShellSession
  151. ansible-playbook -i inventory/sample/hosts.ini cluster.yml --tags preinstall,facts --skip-tags=download,bootstrap-os
  152. ```
  153. And this play only removes the K8s cluster DNS resolver IP from hosts' /etc/resolv.conf files:
  154. ```ShellSession
  155. ansible-playbook -i inventory/sample/hosts.ini -e dns_mode='none' cluster.yml --tags resolvconf
  156. ```
  157. And this prepares all container images locally (at the ansible runner node) without installing
  158. or upgrading related stuff or trying to upload container to K8s cluster nodes:
  159. ```ShellSession
  160. ansible-playbook -i inventory/sample/hosts.ini cluster.yml \
  161. -e download_run_once=true -e download_localhost=true \
  162. --tags download --skip-tags upload,upgrade
  163. ```
  164. Note: use `--tags` and `--skip-tags` wisely and only if you're 100% sure what you're doing.
  165. ## Mitogen
  166. Mitogen support is deprecated, please see [mitogen related docs](/docs/advanced/mitogen.md) for usage and reasons for deprecation.
  167. ## Troubleshooting Ansible issues
  168. Having the wrong version of ansible, ansible collections or python dependencies can cause issue.
  169. In particular, Kubespray ship custom modules which Ansible needs to find, for which you should specify [ANSIBLE_LIBRAY](https://docs.ansible.com/ansible/latest/dev_guide/developing_locally.html#adding-a-module-or-plugin-outside-of-a-collection)
  170. ```ShellSession
  171. export ANSIBLE_LIBRAY=<kubespray_dir>/library`
  172. ```
  173. A simple way to ensure you get all the correct version of Ansible is to use
  174. the [pre-built docker image from Quay](https://quay.io/repository/kubespray/kubespray?tab=tags).
  175. You will then need to use [bind mounts](https://docs.docker.com/storage/bind-mounts/)
  176. to access the inventory and SSH key in the container, like this:
  177. ```ShellSession
  178. git checkout v2.27.0
  179. docker pull quay.io/kubespray/kubespray:v2.27.0
  180. docker run --rm -it --mount type=bind,source="$(pwd)"/inventory/sample,dst=/inventory \
  181. --mount type=bind,source="${HOME}"/.ssh/id_rsa,dst=/root/.ssh/id_rsa \
  182. quay.io/kubespray/kubespray:v2.27.0 bash
  183. # Inside the container you may now run the kubespray playbooks:
  184. ansible-playbook -i /inventory/inventory.ini --private-key /root/.ssh/id_rsa cluster.yml
  185. ```