How to setup Kubernetes on Centos & RedHat

setup Kubernetes on Centos RedHat

Kubernetes is a system designed to manage containerized applications built within Docker containers in a clustered environment. It provides basic mechanisms for deployment, maintenance and scaling of applications on public, private or hybrid setups, means, it handles the entire life cycle of a containerized application. It also has intelligence of self-healing features where containers can be auto provisioned, restarted or even replicated.

Kubernetes Components

Kubernetes works in server-client concept, where, it has a master that provide centralized control for an all minions(agent). We will be deploying one Kubernetes master with two minions and we will also have a workspace machine from where we will run all installation scripts.

Kubernetes has several components:

etcd – A highly available key-value store for shared configuration and service discovery.

flannel – An etcd backed network fabric for containers.

kube-apiserver – Provides the API for Kubernetes orchestration.

kube-controller-manager – Enforces Kubernetes services.

kube-scheduler – Schedules containers on hosts.

kubelet – Processes a container manifest so the containers are launched according to how they are described.

kube-proxy – Provides network proxy services.

Read More: What is Kubernetes, its basics and components


Make sure all the machines you gonna use kubernetes should disable SElinux and swap and Enable br_netfilter.

Disable SELinux

# setenforce 0
# sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux

Disable Swap

# swapoff –a

Enable Br-netfilter

# modprobe br_netfilter
# echo '1' > /proc/sys/net/bridge/bridge-nf-call-iptables

Installing dependencies

Before start to kubernetes, we must install the docker tools, to install use following commands,

# yum install -y yum-utils device-mapper-persistent-data lvm2

Now, add the Docker-ce repository with the command:

# yum-config-manager --add-repo

Install Docker-ce with the command:

# yum install -y docker-ce

Once that completes, start and enable the Docker service with the commands

# systemctl start docker
# systemctl enable docker

Installing Kubernetes

Installing the necessary components for Kubernetes is simple. Again, what we’re going to install below must be installed on all machines that will be joining the cluster.

First add a repository by creating the file

# vim /etc/yum.repos.d/kubernetes.repo 

Save and close that file. Install Kubernetes with the following commands:

# yum update –y && yum install -y kubelet kubeadm kubectl
Cgroup changes

Now we need to ensure that both Docker-ce and Kubernetes belong to the same control group (cgroup). By default, Docker should already belong to cgroupfs (you can check this with the command docker info | grep -i cgroup). To add Kubernetes to this, issue the command:

# sed -i 's/cgroup-driver=systemd/cgroup-driver=cgroupfs/g' /etc/systemd/system/kubelet.service.d/10-kubeadm.conf

Restart the systemd daemon and the kubelet service with the commands:

# systemctl daemon-reload
# systemctl restart kubelet

 Initialize your master

With everything installed, go to the machine that will serve as the Kubernetes master and issue the command:

# kubeadm init --node-name master

When this completes, you’ll be presented with the exact command you need to join the nodes to the master. This command gives Node joining details. make note of it.

Before you join a node, you need to issue the following commands (as a regular user):

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

Deploying a pod network

You must deploy a pod network before anything will actually function properly. I’ll demonstrate this by installing the Flannel pod network. This can be done with two commands (run on the master):

# kubectl apply -f
# kubectl apply -f

Issue the command

#  kubectl get pods --all-namespaces     ##to see that the pod network has been deployed

Joining a node

With everything in place, you are ready to join the node to the master. To do this, go to the node’s terminal and issue the command:

# kubeadm join --token TOKEN SERVER_MASTER_IP:6443

Where TOKEN is the token you were presented after initializing the master and SERVER_MASTER_IP is the IP address of the master.

Once the node has joined, go back to the master and issue the command

#  kubectl get nodes         ## to check the nodes has successfully joined.

Deploying a service

At this point, you are ready to deploy a service on your Kubernetes cluster. To deploy an NGINX service (and expose the service on port 80), run the following commands (from the master):

# kubectl run --image=nginx nginx-app --port=80 --env="DOMAIN=cluster"
# kubectl expose deployment nginx-app --port=80 --name=nginx-http

If you go to your node and issue the command

#  docker ps
CONTAINER ID        IMAGE                                                                                                                            COMMAND                  CREATED             STATUS                  PORTS               NAMES
ef5dc06ea900                                                                                         "/pause"                 1 second ago        Up Less than a second                       k8s_POD_kube-dns-6f4fd4bdf-w96mz_kube-system_f962af8b-109b-11e8-87a6-0aadd08ef4fc_3331
78e06af38cc8                                                                                         "/pause"                 59 minutes ago      Up 59 minutes                               k8s_POD_kube-flannel-ds-rc9ns_kube-system_1cb1fb4c-109c-11e8-87a6-0aadd08ef4fc_0
7d5cc73e6d77                "/usr/local/bin/ku..."   About an hour ago   Up About an hour                            k8s_kube-proxy_kube-proxy-2s4xk_kube-system_f97abe3a-109b-11e8-87a6-0aadd08ef4fc_0
6476dd427279                                                                                         "/pause"                 About an hour ago   Up About an hour                            k8s_POD_kube-proxy-2s4xk_kube-system_f97abe3a-109b-11e8-87a6-0aadd08ef4fc_0
237d44afd3b9                      "etcd --listen-cli..."   About an hour ago   Up About an hour                            k8s_etcd_etcd-kubetest_kube-system_7278f85057e8bf5cb81c9f96d3b25320_0
53cdbeb3eaea            "kube-scheduler --..."   About an hour ago   Up About an hour                            k8s_kube-scheduler_kube-scheduler-kubetest_kube-system_6502dddc08d519eb6bbacb5131ad90d0_0
f2d8873460d6            "kube-apiserver --..."   About an hour ago   Up About an hour                            k8s_kube-apiserver_kube-apiserver-kubetest_kube-system_eeada3a9cee6a5f9ae6930474adcd2f1_0
a953d5cda2e9   "kube-controller-m..."   About an hour ago   Up About an hour                            k8s_kube-controller-manager_kube-controller-manager-kubetest_kube-system_4244b3d987e87af59b2266bff0744c14_0
818279ca4b6b                                                                                         "/pause"                 About an hour ago   Up About an hour                            k8s_POD_kube-apiserver-kubetest_kube-system_eeada3a9cee6a5f9ae6930474adcd2f1_0
51d070ac8e1c                                                                                         "/pause"                 About an hour ago   Up About an hour                            k8s_POD_kube-scheduler-kubetest_kube-system_6502dddc08d519eb6bbacb5131ad90d0_0
9a10f918db81                                                                                         "/pause"                 About an hour ago   Up About an hour                            k8s_POD_etcd-kubetest_kube-system_7278f85057e8bf5cb81c9f96d3b25320_0
42a46caa7324                                                                                         "/pause"                 About an hour ago   Up About an hour                            k8s_POD_kube-controller-manager-kubetest_kube-system_4244b3d987e87af59b2266bff0744c14_0