Set up Kubernetes Cluster using kubeadm on RHEL 7 / Centos 7

Leave a Comment

Problem with Monolithic Application:

Integration and Deployment are not easy as there are lot of inter dependency, if all the services are running on  a single OS, then there might be conflicting libraries versions and application components. Even if Virtual Machines are used for applications, still there will be conflict, but what if each process could somehow be built to  made to run  on its own with its libraries and dependencies packaged within, thus each of these micro service can be debugged, deployed individually without causing harm to entire project. This is achieved using containers. However containers alone are not sufficient to manage containers in production at scale, where Orchestration system like Kubernetes eases the task.


Kubernetes is an open source Orchestration system for containers. It handles scheduling onto nodes in a compute cluster and actively manages them.

  • Kubernetes is inspired by Borg that is internal system used by Google 
  • Abbreviated as K8s
  • It is given to Open Source in June 2014 with apache license and having thousand of contributors worldwide.
  • Google donated K8s to Linux Foundation in 2015 under Cloud Native Computing Foundation.
  • Runs anywhere
  • There are lot of companies using and contributing to Kubernetes:

Architecture of  Kubernetes :

Below is the architecture of Kubernetes :




Kubernetes Pod:

Pod is group of one or more containers that are always co related, co scheduled and run in shared context, that makes the app.

Kubernetes doesn’t run containers directly; instead it is combination of one or more containers that is higher-level structure called pod.

Each Pod is isolated by

  • Process ID namespace
  • Network namespace
  • Unix Time sharing namespace
  • Inter Process communication namespace
kubectl get pods                                                                  - Get information about all running pods
kubectl describe pod <pod>                                                - Describe one pod
kubectl expose pod <pod> --port=444  --name=frontend   - Expose the port of a pod
kubectl port-forward <pod>  8080                                      - Port forward the exposed pod port to your local machine.
kubectl attach pod <podname> -i                                        - Attach to the pod
kubectl exec <pod> --command                                          - Execute a command on pod
kubectl label pods <pod> mylabel=bravo                           - Add a label to pod



Installation steps:

Updated on 8th January 2021

In this blog, i will list down the steps and commands for setting up a two node Kubernetes Cluster with one master and a worker node.

We need two Virtual Machine for this demo, one for Master node and one for worker node.

POD communication will be done using flannel network.

Create two Virtual Machine with RHEL or Centos 7 installed on them, I have used Centos 7.9 for this demo. You may install on Physical machines or Virtual machines.


Below steps are to be followed for installation of Kubernetes


Master Server  steps:

Prerequisites (Step 1 to 7)


1. Make entry of master and worker VM/node in /etc/hosts file. 



#hostnamectl set-hostname 'k8s-master'



[root@k8s-master ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.56.2 k8s-master
192.168.56.3 worker-node01
[root@k8s-master ~]#

2. Disable Selinux


# setenforce 0

# vi /etc/selinux/config
SELINUX=disabled




3. Stop and Disable firewalld 

# systemctl stop firewalld
# systemctl disable firewalld

4. Create the /etc/sysctl.d/k8s.conf file and add the following:


net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
sysctl -p /etc/sysctl.d/k8s.conf
Run above command to make the kernel parameters persistent. In case you are getting any error after running  sysctl -p command, make sure bridge modules is loaded in the kernel using lsmod command. Use modprobe to load the missing modules

5.  Disable Swap memory by editing /etc/fstab file and commenting or removing the swap memory entry and run below command.

swapoff -a

6. Verify that the br_netfilter module is loaded. 

lsmod | grep br_netfilter 

To load it explicitly run below command

sudo modprobe br_netfilter





7. Create the repository kubernetes.repo using below


cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-\$basearch
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
exclude=kubelet kubeadm kubectl
EOF

Install Container Runtime (Step 8)

we are using docker as container runtime for this installation and cluster setup.
 
8. Install Docker 

yum install docker -y 

Enable and start the docker service.

 systemctl restart docker
 systemctl enable docker

Installing Kubeadm, Kubelet and Kubectl (Step 9)


9.  Intstall kubeadm, kubelet and kubectl

sudo yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes

Enable the kubelet service
 systemctl restart kubelet
 systemctl enable kubelet


Please note : You need to perform step 1 to 9 on the worker nodes as well.


Creating Cluster with Kubeadm


10.  Create Cluster using below command


We are using flannel network for this demo

Run below command to create cluster along with CIDR on the master cluster node 

kubeadm init --pod-network-cidr=10.244.0.0/16

Note : Using --apiserver-advertise-address option during master initialization.

In our virtual machine, we have 2 or more IP address, one with IP address 192.168.1.* series that is dynamic IP via the bridged network and other with static IP 192.168.56.2 on host only network.


We have static ip address for the internal communication in the cluster. when we initialize a Kubernetes  master  using the kubeadm init command, it actually configures the a master api server to listen to a particular interface and here we can advertise the static IP in the master initialize command itself as shown below.

kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.56.2 
Output of above command




Note :  Make a note of the kubeadm join command that will be used for adding the worker nodes to the cluster.

Once, it is done, you need to run below commands.

Your Kubernetes master has initialized successfully!
To start using your cluster, you need to run the following as a regular user:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf



11.  Use below command to apply the flannel network 

This varies as per your Kubernetes version.  Run kubeadm version to check Kubernetes version. 

Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/
For Kubernetes v1.17+ run below command,

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

If  you execute, kubectl get pods --all-namespaces command, you will see a set of services being spun up by Kubernetes.




Adding  worker nodes to Cluster 

Make sure (Steps 1 to 9) are performed on worker node before joining the node to the cluster.

Get the  Node join command from the master node and run the same on the worker nodes. In case you don't have join command same can be retrieved using below

kubeadm token create --print-join-command





Once  worker node  is added to cluster, run below command to check the cluster status.

You can add as many worker nodes you want using same join command.

# kubectl get nodes



That completes the setup of  Kubernetes Cluster using kubeadm tool on RHEL 7 or Centos 7 machines.

0 comments:

Post a Comment