What is k8s?
Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It has a large, rapidly growing ecosystem. Kubernetes services, support, and tools are widely available
The name Kubernetes originates from Greek, meaning helmsman or pilot. K8s as an abbreviation results from counting the eight letters between the “K” and the “s”. Google open-sourced the Kubernetes project in 2014.
There are many different ways to install k8s. some of them are below:
- Kops
- KRIB
- Kubeadm
- Kubespray
In this post we are going to setup k8s cluster with kubespray
What is kubespray?
It’s a new way to install kubernetes cluster.Its is a combination of Ansible and kubernetes . This means we can install kubernetes using Ansible .
Kubespray is a project that contains a bunch of ansible playbooks which deploy kubernetes clusters in an automated way. It is very suitable installation, configuration and maintaining method for on-premise environments since you can use it to deploy kubernetes clusters on different linux distros like, Ubuntu, Coreos and Centos.
Benefits of using kubespray?
- Kubespray provides deployment flexibility. It allows you to deploy a cluster quickly and customize all aspects of the implementation.
- Kubespray strikes a balance between flexibility and ease of use.
- You only need to run one Ansible playbook and your cluster ready to serve.
Lets Start…….
In this post I am setting up 3 node cluster
Requirement
Here I am installing three node clusters which include one master node and two worker node. And make sure that password less authentication is enabled between the servers
Steps:-
- Disable SElinux
- Install Packages(git, epel-release, python)
- Clone kubespray repo and Install requirements
- Generate ssh key for password less authentication.
- Update the ansible inventory file.
- Now lets Deploy cluster with ansible
Step 1:- Disable SElinux
Here disable SElinux on all nodes
Preparing ansible
setenforce 0
sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
systemctl disable firewalld; systemctl stop firewalld; systemctl mask firewalld
Setp 2:- Install Packages(git, epel-release, python)
Install these packages on Ansible node (from where you are going to execute our ansible playbook)
yum update -y
yum install git -y
yum install epel-release -y
yum install python-pip -y
pip install --upgrade pip
Step 3:- Clone Kubespray repo and Install requirements.
- clone Kubespray repo
git clone https://github.com/kubernetes-sigs/kubespray.git
- Install Requirements
cd kubespray
pip install -r requirements.txt
If you face below error while installing requirements.txt
then follow below steps:-
wget https://bootstrap.pypa.io/pip/2.7/get-pip.py
python get-pip.py
- Copy inventory/sample as inventory/mycluster (change mycluster to any name you want for the cluster)
cp -rfp inventory/sample inventory/cloudsbaba
Step 4:- Generate ssh key and placed over all the nodes
ssh-keygen -t rsa
Step 5:- Update the ansible inventory file
cd kubespray/inventory/cloudsbaba
cp inventory.ini hosts.ini
update the host.ini with your hosts like below
After adding host info check if all the servers are accessible via ansible or not
ansible -i hosts.ini -m ping all
Step 6:- Now lets Deploy cluster with ansible
ansible-playbook -i inventory/cloudsbaba/hosts.ini cluster.yml --become -vvv
it will take 5-10 min to complete depending upon your network speed
lets check the status of the node
kubectl get nodes
Here you can see the status of nodes
lets suppose you want to remove the node from the cluster
ansible-playbook -i inventory/cloudsbaba/hosts.ini remove-node.yml -e "node=worker-node1" --become -vvv
lets suppose you want to add node to cluster
Navigate to your hosts.ini file and add a nod entry as you added in step 5.
after that execute the below script
ansible-playbook -i inventory/cloudsbaba/hosts.ini scale.yml --become -vvv
Congratulations…. you follow all the steps and deploy k8s with kubespray
References:-
- https://kubernetes.io/docs/concepts
- https://www.redhat.com/sysadmin/kubespray-deploy-kubernetes
- https://github.com/kubernetes-sigs/kubespray