Provisioning Kubernetes cluster using Ansible and Kubespray

Kubespray is a collection of Ansible Playbooks, inventory and provisioning tools.

Here, we are going to use below 3 VMs to set up Kubernetes cluster,

1) Ansible Controller Machine -> ansible-controller /
2) Kubernetes master -> kube-master-1 /
3) Kubernetes node -> cent7-node-1 /

Requirements for Ansible Controller Machine:

Following needs to installed in Ansible controller machine with latest version,

Anisble has been installed with below version

[root@ansible-controller ~]# ansible --version
ansible 2.5.5
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
[root@ansible-controller ~]#

Ansible Configuration:

[ansible@ansible-controller ~]$ cat ansible.cfg | grep -v ^#
inventory = /home/ansible/hosts
remote_user = ansible
[ansible@ansible-controller ~]$

Installing ‘python-netaddr’ in Ansible controller machine:

[root@ansible-controller ~]# pip install netaddr
Collecting netaddr
Downloading (1.6MB)
100% |████████████████████████████████| 1.6MB 12.5MB/s
Installing collected packages: netaddr
Successfully installed netaddr-0.7.19
[root@ansible-controller ~]# 

Please make sure, managed nodes are reachable from Ansible controller machine as below,’

[ansible@ansible-controller ~]$ ansible -m ping all
cent7-node-1 | SUCCESS => {
"changed": false,
"ping": "pong"
kube-master-1 | SUCCESS => {
"changed": false,
"ping": "pong"
[ansible@ansible-controller ~]$ 

Download kubespray:


[ansible@ansible-controller ~]$ tar -zxvf v2.5.0.tar.gz

[ansible@ansible-controller ~]$ ls
ansible.cfg hosts install_docker.yml kubespray-2.5.0 v2.5.0.tar.gz
[ansible@ansible-controller ~]$

[ansible@ansible-controller inventory]$ pwd
[ansible@ansible-controller inventory]$ ls
local sample
[ansible@ansible-controller inventory]$ cp -rf sample/* /home/ansible/inventory/k8ssetup/
[ansible@ansible-controller inventory]$

[ansible@ansible-controller k8ssetup]$ pwd
[ansible@ansible-controller k8ssetup]$ ls
group_vars hosts.ini
[ansible@ansible-controller k8ssetup]$

Edit the hosts.ini file as bleow,

[ansible@ansible-controller k8ssetup]$ cat hosts.ini | grep -v ^#
kube-master-1 ansible_ssh_host=
cent7-node-1 ansible_ssh_host=




[ansible@ansible-controller k8ssetup]$

Checking the connectivity as below,

ansible -i /home/ansible/inventory/k8ssetup/hosts.ini -m ping all

[ansible@ansible-controller group_vars]$ pwd
[ansible@ansible-controller group_vars]$

Under group_vars, edit the ‘all.yml’ file according to our requirements as below,

[ansible@ansible-controller group_vars]$ cat all.yml | grep -v ^#
bootstrap_os: centos

etcd_data_dir: /var/lib/etcd

bin_dir: /usr/local/bin
[ansible@ansible-controller group_vars]$

Note: SWAP needs to be disabled in managed nodes for k8s setup,

[root@cent7-node-1 ~]# free -m
total used free shared buff/cache available
Mem: 1595 145 1215 8 235 1281
Swap: 819 0 819
[root@cent7-node-1 ~]# swapoff -a
[root@cent7-node-1 ~]# free -m
total used free shared buff/cache available
Mem: 1595 146 1214 8 234 1280
Swap: 0 0 0
[root@cent7-node-1 ~]# 

Now we can execute anible playbook ‘/home/ansible/kubespray-2.5.0/cluster.yml’ to set up kubernetes cluster,

ansible-playbook -b -i /home/ansible/inventory/k8ssetup/hosts.ini cluster.yml

PLAY RECAP *********************************************************************************************************
cent7-node-1 : ok=211 changed=26 unreachable=0 failed=0
kube-master-1 : ok=317 changed=47 unreachable=0 failed=0
localhost : ok=2 changed=0 unreachable=0 failed=0

Monday 13 August 2018 02:21:56 -0400 (0:00:00.037) 0:06:47.708 *********

[root@kube-master-1 ~]# kubectl version --short
Client Version: v1.9.5
Server Version: v1.9.5
[root@kube-master-1 ~]#

Ref. URL: