Quick Guide to Install Kubernetes Cluster 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:

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 RHEL 7.5 for this demo.

Below steps are to be followed for installation of Kubernetes

Master Server  steps:

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

#hostnamectl set-hostname 'k8s-master'

cat /etc/hosts k8s-master worker-1

2. Disable Selinux

# setenforce 0

# vi /etc/selinux/config

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 the swap memory entry.

6. Create the repository kubernetes.repo using below

cat <<EOF > /etc/yum.repos.d/kubernetes.repo

7. Install Docker and kubeadm 

yum install docker kubeadm -y 

8. Enable and start the docker and kubelet service.

 systemctl restart docker
 systemctl enable docker

 systemctl  restart kubelet
 systemctl enable kubelet
9.  Create Cluster using below command

We are using flannel network for this demo

Run below command to create cluster along with CIDR

kubeadm init --pod-network-cidr=
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

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

10.  Use below command to apply the flannel network 

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

[root@k8s-master ~]# kubectl get cs
NAME                 STATUS    MESSAGE              ERROR
scheduler            Healthy   ok
controller-manager   Healthy   ok
etcd-0               Healthy   {"health": "true"}
[root@k8s-master ~]#

[root@k8s-master ~]# kubectl get nodes
k8s-master   NotReady   master   5m47s   v1.12.3

[root@k8s-master ~]# kubectl get pods --all-namespaces
NAMESPACE     NAME                                 READY   STATUS    RESTARTS   AGE
kube-system   coredns-576cbf47c7-8ndcd             1/1     Running   0          44m
kube-system   coredns-576cbf47c7-rqd2l             1/1     Running   0          44m
kube-system   etcd-k8s-master                      1/1     Running   0          7s
kube-system   kube-apiserver-k8s-master            1/1     Running   0          9s
kube-system   kube-controller-manager-k8s-master   1/1     Running   0          11s
kube-system   kube-proxy-tjn29                     1/1     Running   0          44m
kube-system   kube-scheduler-k8s-master            1/1     Running   0          12s
kube-system   weave-net-wv64p                      2/2     Running   0          36s
[root@k8s-master ~]#

Adding nodes to Cluster 

Use the same steps as done in Master node and install docker and kubeadm to worker node/VM

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.

[root@k8s-master ~]# kubectl get nodes
k8s-master   Ready      master   2d18h   v1.12.3
worker-1     NotReady   <none>   27s     v1.12.3
[root@k8s-master ~]#


Post a comment