Ansible variables

Inventory

The inventory is composed of 3 groups:

  • kube-node : list of kubernetes nodes where the pods will run.
  • kube_control_plane : list of servers where kubernetes control plane components (apiserver, scheduler, controller) will run.
  • etcd: list of servers to compose the etcd server. You should have at least 3 servers for failover purpose.

Note: do not modify the children of k8s-cluster, like putting the etcd group into the k8s-cluster, unless you are certain to do that and you have it fully contained in the latter:

  1. k8s-cluster etcd => kube-node etcd = etcd

When kube-node contains etcd, you define your etcd cluster to be as well schedulable for Kubernetes workloads. If you want it a standalone, make sure those groups do not intersect. If you want the server to act both as control-plane and node, the server must be defined on both groups kube_control_plane and kube-node. If you want a standalone and unschedulable master, the server must be defined only in the kube_control_plane and not kube-node.

There are also two special groups:

Below is a complete inventory example:

  1. ## Configure 'ip' variable to bind kubernetes services on a
  2. ## different ip than the default iface
  3. node1 ansible_host=95.54.0.12 ip=10.3.0.1
  4. node2 ansible_host=95.54.0.13 ip=10.3.0.2
  5. node3 ansible_host=95.54.0.14 ip=10.3.0.3
  6. node4 ansible_host=95.54.0.15 ip=10.3.0.4
  7. node5 ansible_host=95.54.0.16 ip=10.3.0.5
  8. node6 ansible_host=95.54.0.17 ip=10.3.0.6
  9. [kube_control_plane]
  10. node1
  11. node2
  12. [etcd]
  13. node1
  14. node2
  15. node3
  16. [kube-node]
  17. node2
  18. node3
  19. node4
  20. node5
  21. node6
  22. [k8s-cluster:children]
  23. kube-node
  24. kube_control_plane

Group vars and overriding variables precedence

The group variables to control main deployment options are located in the directory inventory/sample/group_vars. Optional variables are located in the inventory/sample/group_vars/all.yml. Mandatory variables that are common for at least one role (or a node group) can be found in the inventory/sample/group_vars/k8s-cluster.yml. There are also role vars for docker, kubernetes preinstall and master roles. According to the ansible docs, those cannot be overridden from the group vars. In order to override, one should use the -e runtime flags (most simple way) or other layers described in the docs.

Kubespray uses only a few layers to override things (or expect them to be overridden for roles):

LayerComment
role defaultsprovides best UX to override things for Kubespray deployments
inventory varsUnused
inventory group_varsExpects users to use all.yml,k8s-cluster.yml etc. to override things
inventory host_varsUnused
playbook group_varsUnused
playbook host_varsUnused
host factsKubespray overrides for internal roles’ logic, like state flags
play varsUnused
play vars_promptUnused
play vars_filesUnused
registered varsUnused
set_factsKubespray overrides those, for some places
role and include varsProvides bad UX to override things! Use extra vars to enforce
block vars (only for tasks in block)Kubespray overrides for internal roles’ logic
task vars (only for the task)Unused for roles, but only for helper scripts
extra vars (always win precedence)override with ansible-playbook -e @foo.yml

Ansible tags

The following tags are defined in playbooks:

Tag nameUsed for
appsK8s apps definitions
azureCloud-provider Azure
bastionSetup ssh config for bastion
bootstrap-osAnything related to host OS configuration
calicoNetwork plugin Calico
canalNetwork plugin Canal
cloud-providerCloud-provider related tasks
dockerConfiguring docker for hosts
downloadFetching container images to a delegate host
etcdConfiguring etcd cluster
etcd-pre-upgradeUpgrading etcd cluster
etcd-secretsConfiguring etcd certs/keys
etchostsConfiguring /etc/hosts entries for hosts
factsGathering facts and misc check results
flannelNetwork plugin flannel
gceCloud-provider GCP
k8s-pre-upgradeUpgrading K8s cluster
k8s-secretsConfiguring K8s certs/keys
kube-apiserverConfiguring static pod kube-apiserver
kube-controller-managerConfiguring static pod kube-controller-manager
kubectlInstalling kubectl and bash completion
kubeletConfiguring kubelet service
kube-proxyConfiguring static pod kube-proxy
kube-schedulerConfiguring static pod kube-scheduler
localhostSpecial steps for the localhost (ansible runner)
masterConfiguring K8s master node role
netcheckerInstalling netchecker K8s app
networkConfiguring networking plugins for K8s
nginxConfiguring LB for kube-apiserver instances
nodeConfiguring K8s minion (compute) node role
openstackCloud-provider OpenStack
preinstallPreliminary configuration steps
resolvconfConfiguring /etc/resolv.conf for hosts/apps
upgradeUpgrading, f.e. container images/binaries
uploadDistributing images/binaries across hosts
weaveNetwork plugin Weave
ingress_albAWS ALB Ingress Controller
ambassadorAmbassador Ingress Controller

Note: Use the bash scripts/gen_tags.sh command to generate a list of all tags found in the codebase. New tags will be listed with the empty “Used for” field.

Example commands

Example command to filter and apply only DNS configuration tasks and skip everything else related to host OS configuration and downloading images of containers:

  1. ansible-playbook -i inventory/sample/hosts.ini cluster.yml --tags preinstall,facts --skip-tags=download,bootstrap-os

And this play only removes the K8s cluster DNS resolver IP from hosts’ /etc/resolv.conf files:

  1. ansible-playbook -i inventory/sample/hosts.ini -e dns_mode='none' cluster.yml --tags resolvconf

And this prepares all container images locally (at the ansible runner node) without installing or upgrading related stuff or trying to upload container to K8s cluster nodes:

  1. ansible-playbook -i inventory/sample/hosts.ini cluster.yml \
  2. -e download_run_once=true -e download_localhost=true \
  3. --tags download --skip-tags upload,upgrade

Note: use --tags and --skip-tags wise and only if you’re 100% sure what you’re doing.

Bastion host

If you prefer to not make your nodes publicly accessible (nodes with private IPs only), you can use a so called bastion host to connect to your nodes. To specify and use a bastion, simply add a line to your inventory, where you have to replace x.x.x.x with the public IP of the bastion host.

  1. [bastion]
  2. bastion ansible_host=x.x.x.x

For more information about Ansible and bastion hosts, read Running Ansible Through an SSH Bastion Host

Mitogen

You can use mitogen to speed up kubespray.