Skip to main content

Installing Kubernetes on CentOS

Installing Kubernetes on CentOS can be little different from installing it on Ubuntu because CentOS is an enterprise version of Linux. So there are a couple of steps which Ubuntu takes care for you which needs to be done manually in CentOS.

I have tried to list the detailed steps of installing Kubernetes on CentOS version of 7.4. If you find challenge in any of the steps please do write them below in the comments section and I will try to fix them.

The first few steps are common for working on the Master node and Worker node. There will be a difference of steps once Kubernetes is installed and we just need to join the nodes together.

Common Kubernetes Installation Steps:

1) First login as root user and turn the swap memory off:
sudo su
swapoff -a
vim /etc/fstab
Comment out the swap, /root/swap, save and exit
2) Pull in the latest updates, install & start docker
yum -y update
yum -y install docker
systemctl enable docker
systemctl start docker
3) After docker is installed and started we need to create Kubernetes Repo. This step requires a little bit of typing so its best if you copy paste the below set of lines to create the repo:
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
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
EOF
4) After this we need to disable the SE Linux feature as sometimes it hinders the installation of Kubernetes:
setenforce 0
vim /etc/selinux/config
In /etc/selinux/config file, change SELINUX value from enforcing to permissive. Save and exit.
5) As all things are good for Kubernetes installation now, so execute the following command now:
yum install -y kubelet kubeadm kubectl
systemctl enable kubelet
This step will take some time. After the kubelet is enabled, then execute the following script:
cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
6) To make the changes take effect to execute the following:
sysctl --system

Till here the steps for master and worker are common. After you are done till here, follow the further steps carefully.

For Master:

1) Execute the following step in root login on the VM/server, which you want to make the Master node:
kubeadm init --pod-network-cidr=10.244.0.0/16
The join command for the Worker node to join with Master node is given by during the execution of Step 1. Please keep a note of the command as this is used later on while setting up Worker node.
2) This steps takes care of most of the settings to bring up the Master node and will take some time to complete. 
3) Once step 1 is complete, log out of the root user and execute the following script as a normal user. Usually, these steps are given by the output of the Step 1 script.
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
4) After this, apply the following command for Master node to be up.
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.9.1/Documentation/kube-flannel.yml
5) Execute the get nodes to check the status of the Master node. If you have followed all the steps correctly then the Master node should be in "Ready" state now.
kubectl get nodes

For Worker:

I assume that you have executed the common steps required to install Kubernetes on Master and Worker node.
1) Login to the Worker node as root user and execute the join command which we had noted earlier while setting up the Master node.
2) The join command will vary and the following is one example of the same:
kubeadm join 10.161.103.10:6443 --token ctkw1w.ks13how3li23jt43 --discovery-token-ca-cert-hash sha256:6b29b54f53eb7409d06cf0ce4f5f9a240435f97da9552905ad4a1bf08c5f76dd
3) The output of the join command should give you the correct message that Worker node has been successfully joined with the Master node. This can be confirmed by logging into Master node and get nodes command. The get nodes command should now show the Worker name and the state should be Ready. For any reason, if the state shows "NotReady" then wait for sometime as it takes a minute or two to be in "Ready" state.

This way a simple 1 worker Kubernetes cluster is ready to use. You may add more worker nodes in a similar way and check their status in Master node and create a bigger cluster.

If you have any feedback, please write it in the comments section below.

Comments