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.

299 lines
18 KiB

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