KUBESPRAY

Shiwani Biradar
6 min readJul 18, 2020

In this article we will see new tool which is used to deploy kubernetes cluster but before discussing on kubespray , I will give little introduction on kubernetes where we will see function and feature of kubernetes .

Introduction to Kubernetes

Kubernetes is Orchestration tool which is used to managed containers of different hosts at a time . when we discussed about Docker, Docker can managed only containers of single host only ( Though to managed containers of multiple hosts we have docker-swarm but kubernetes provides some extra features that we cant do with docker swarm ). so to managed containers of multiple hosts we have this orchestration tool. kubernetes have some key feature also that makes kubernetes special than other orchestration tools and that features are Horizontal autoscalibilty, GUI of kubernetes, self monitoring , storage orchestration , Automate rollout and rollback etc.

So there are lots of ways to installing kubernetes cluster . Now-a-days cloud providers like AWS, GCP , AZURE started providing services of kubernetes . but we can also go the manual installation of kubernetes cluster .

KUBESPRAY

(New way for installing kubernetes cluster)

kubernetes cluster can also be installed using various automation tools so kubespray is combination of kubernetes + ansible . That means we can installed kubernetes using ansible . we can deployed cluster using kubespray on cloud’s compute services like EC2(AWS)

Whats are benefits of using kubespray ?

  • Kubespray provides a good trade-off in the flexibility of a deployment. It allows you not only to easily deploy a cluster, but also to customize all aspects of the deployment.
  • Kubespray strikes a balance between flexibility and ease of use.
  • Only run 1 playbook and your cluster ready to serve.

above diagram shows the deployment architecture of kubespray.

Kubeadm Vs kubespray

Kubeadm provides domain Knowledge of kubernetes clusters’ life cycle management, including self-hosted layouts, dynamic discovery services and so on. Had it belonged to the new operators world, it may have been named a “kubernetes cluster operator”. Kubespray however, does generic configuration management tasks from the “OS operators” ansible world, plus some initial K8s clustering (with networking plugins included) and control plane bootstrapping. Kubespray supports kubeadm for cluster creation since v2.3 (and deprecated non-kubeadm deployment starting from v2.8) in order to consume life cycle management domain knowledge from it and offload generic OS configuration things from it, which hopefully benefits both sides.

lets start demo

Before going on demonstration part i recommended that Kubespray is under active development, so make sure to use a stable release version. so here i created 3 vms comprises of 1 master and 2 nodes on AWS compute instances.so whatever steps i did in demonstration are totally run on aws instances . so lets start the demo part now :

Created 3 virtual instances on aws , 1 for master & 2 for nodes purpose . As we already discussed kubespray used ansible to install kubernetes cluster so we start with installation of ansible . I took vms of centos 7 linux distribution so we can installed ansible using yum package manager

yum install epel-release

yum install ansible -y

After installing ansible , we need to installed some of the packages that you may required in next steps . So they are

yum install python36

yum install python-pip

pip2 install jinja2

after installing these prerequisites packages, we will do ansible setup . basically we know ansible worked on ssh connectivity so we need to do ssh-keygen on master node and will copy keygen on all machines including master node.(for passwordless authentication)

now we will clone the official repository of kubespray from git hub

git clone https://github.com/kubernetes-incubator/kubespray.git

now we go inside the kubespray directory and will install the dependencies from requirement.txt file using pip

pip install -r requirements.txt

it will install all dependencies

But in case you may faced issue with request package so for that follow following steps:

  • Download the latest “requests” package (.tar.gz file)
  • Untar the tar file, go inside that directory and run command --- python setup.py install command

In this way we will installed all the requirements packages .

now Copy inventory/sample as inventory/my-cluster

cp -rfp inventory/sample inventory/my-cluster

After that declare variable which includes private ip of your vms and then will call that variable

declare -a IPS=(10.0.0.210 10.0.0.12 10.0.0.213)

bt before that you need to install some requirement which is helpfull for next steps:

pip3 install -r contrib/inventory_builder/requirements.txt/requirements.txt

If you dont fullfill the installation of requirement packages of above command then it will definitely prompt error in next command.

Run the following command:

CONFIG_FILE=inventory/mycluster/hosts.yml python3 contrib/inventory_builder/inventory.py ${IPS[@]}

so now It should generate the “inventory/mycluster/hosts.yml” file with following hosts mapping, you can change it as per your need. ( just change name of nodes according to your hostname of nodes)

all:
hosts:
master:
ansible_host: 10.0.0.210
ip: 10.0.0.210
access_ip: 10.0.0.210
node1:
ansible_host: 10.0.0.12
ip: 10.0.0.12
access_ip: 10.0.0.12
node2:
ansible_host: 10.0.0.213
ip: 10.0.0.213
access_ip: 10.0.0.213
children:
kube-master:
hosts:
master:
kube-node:
hosts:
master:
node1:
node2:
etcd:
hosts:
master:
node1:
node2:
k8s-cluster:
children:
kube-master:
kube-node:
calico-rr:
hosts: {}

after setting this hosts.yml file according to your configuration , we need to change some vars file which is at location

inventory/my-cluster/group_vars/all.yml
inventory/my-cluster/group_vars/k8s-cluster.yml

so first go inside inventory/mycluster/group_vars/all.yml and uncomment the line which include following part:

# The read-only port for the Kubelet to serve on with no authentication/authorization. Uncomment to enable.kube_read_only_port: 10255

now go inside inventory/my-cluster/group_vars/k8s-cluster.yml file and change

# Choose network plugin (cilium, calico, contiv, weave or flannel)
# Can also be set to 'cloud', which lets the cloud provider setup appropriate routing
kube_network_plugin: weave

Now we are all set ansible-playbook -i inventory/my-cluster/hosts.yml reset.ymlto run playbook for cluster , for deploy kubespray with Ansible playbook use:

ansible-playbook -i inventory/my-cluster/hosts.yml cluster.yml

This will take max 5 or 7 min according to your vms configurations. Once the playbook completed you can check whether your cluster is ready or not.

# kubectl get nodesNAME      STATUS    ROLES         AGE       VERSIONmaster    Ready     master        4m        v1.18.5
node1 Ready node 4m v1.18.5
node2 Ready node 4m v1.18.5

now some additional steps to add or remove node in your kubernetes cluster so for that you have to update your hosts.yml file

Now we are adding node3 in our kubernetes cluster so for that we need to edit inventory/my-cluster/hosts.yml file

In all section all your node3

[all]
master ansible_host=10.0.0.210
node1 ansible_host=10.0.0.12
node2 ansible_host=10.0.0.213
node3 ansible_host=10.0.0.252

and in [kube-node] section

[kube-node]
master
node1
node2
node3

Now run following command to add node in your cluster

ansible-playbook -i inventory/my-cluster/hosts.yml scale.yml

after completing this playbook you can check that your node3 is successfully added in your cluster

# kubectl get nodesNAME      STATUS    ROLES         AGE       VERSIONmaster    Ready     master        4m        v1.18.5
node1 Ready node 4m v1.18.5
node2 Ready node 4m v1.18.5
node3 Ready node 4m v1.18.5

Now to remove node3 we will reverse the process of adding node. that means we will removed all the entries of node3 from hosts.yml file i.e from [all] section and from [kube-node] section.

then we will run playbook to remove the node3 from my cluster

ansible-playbook -i inventory/my-cluster/hosts.yml remove-node.yml

after completing this playbook you can check that your node3 is removed from your kubernetes cluster.

This is way you can deploy kubernetes cluster using kubespray .

You can also reset the cluster i.e flush the cluster for fresh installation,so for that just run the playbook as below mentioned:

ansible-playbook -i inventory/my-cluster/hosts.yml reset.yml

So this is way you can play with kubernetes using kubespray .

If faced any kind of issue while installation put that in comment section , i will try to solve that error

Thank you .

--

--