How to Install and Configure Kubernetes Cluster (k8s) on CentOS 8/RHEL 8

Kubernetes is an open source container orchestration tool for deploying applications. In Kubernetes cluster setup, we have one master and multiple worker nodes or Minion. From the master node, we manage the cluster and its nodes using kubeadm and kubectl utility. In CentOS 8/RHEL 8. docker has now been replaced by podman and buildah tools from Redhat. So docker package has been removed from the default package repository. In this guide, we will learnt how to Install and configure Kubernetes Cluster (k8s) on CentOS 8/RHEL 8.

Kubernetes can be installed and deployed on various platforms using following methods,

  • Minikube ( single node kubernetes cluster)
  • Kops ( Multi node kubernetes setup on AWS )
  • Kubeadm ( On-premise Multi Node kubernetes Cluster )

Prerequisite

  • Swap should be disabled in order to run “kubeadm init“ command. Disable Swap in all nodes using “swapoff -a” command and remove or comment out swap partitions from fstab file.
  • Three machines running CentOS 8 – 1 Master Node and 2 Worker Nodes.
  • It is recommended to have at least 2 CPUs with 2GB RAM or more per machine.
  • All nodes should be able to connect to one another, either on a private or public network.
  • Root or sudo user  privileges required.
  • br_netfilter module should be enabled on all machines.
$ lsmod | grep br_netfilter

$ sudo modprobe br_netfilter

Read Also : Kubernetes Tutorial – Advanced Overview of K8s

Installing and Configuring Kubernetes Cluster on CentOS 8/RHEL 8

We require multiple machines to form a cluster in kubernetes where one or more machine can be master which controls the cluster and one or more machine can be worker nodes where pods resides and used to deploy applications. 

The following instructions will be performed on centos 8 in order to establish Kubernetes cluster.

Setup hostname, disable SELinux & configure firewall rules

Login to kubernetes master node and disable selinux first using below commands. in below configuration file, make SELinux permissive from enforcing and then reboot the machine.

# hostnamectl set-hostname 'k8s-master'
# setenforce 0
# sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
#sudo systemctl reboot

Next setup the following firewall rules.

# firewall-cmd --permanent --zone=public --add-masquerade

# firewall-cmd --permanent --zone=public --add-port=6443/tcp --add-port=2379-2380/tcp --add-port=10250/tcp 
--add-port=10251/tcp  --add-port=10252/tcp --add-port=10255/tcp --add-port=30000-32767/tcp 
--add-port=2375-2377/tcp --add-port=7946/udp --add-port=4789/udp

# firewall-cmd --reload

# firewall-cmd --list-all

Now load the br_netfilter module,

# cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
>net.bridge.bridge-nf-call-ip6tables = 1
>net.bridge.bridge-nf-call-iptables = 1
>EOF

# modprobe br_netfilter 
# cat /proc/sys/net/bridge/bridge-nf-call-iptables
# sysctl --system

Setup Docker-CE and Kubernetes Repositories

Here, you need to add the docker repository in the default package using the below dnf config-manager command.

# dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo

Next, install containerd.io package which is available as a daemon that will manages the complete container lifecycle,

# dnf install https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm

Afterwards, you need to add Kubernetes repositories manually as they don’t come installed by default on CentOS 8.

# 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

Install Docker-CE

Next install the current supportable version of docker-ce package.

# dnf erase podman buildah -y

# dnf install docker-ce --nobest -y

# usermod -aG docker $USER

# newgrp docker

Now you can install docker-compose,

# curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Now provide executable permissions,
# chmod +x /usr/local/bin/docker-compose
# ln -s /usr/local/bin/docker-compose  /usr/bin/docker-compose
# docker version
# docker-compose version

You should enable and start the docker service now.

# systemctl restart docker

# systemctl enable --now docker

Install Kubernetes

Kubeadm helps you bootstrap a minimum viable Kubernetes cluster that confirms the best practice. Kubeadm also supports other cluster lifecycle functions, such as upgrades, downgrade, and managing bootstrap tokens. Kubeadm is also integration-friendly with other orchestration tools like Ansible and Terraform.

With the package repo now ready, you can go ahead and install kubeadm package.

# dnf install -y kubeadm kubelet kubectl --disableexcludes=kubernetes

When the installation finishes  successfully, enable and start the service.

# systemctl enable --now kubelet

Swap must disabled in order to run kubelet command,

# sudo swapoff -a
# systemctl daemon-reload
# systemctl restart kubelet

Kubernetes master acts as the control plane for the cluster runs a few critical services necessary for the cluster. As such, the initialization process will do a series of prechecks to ensure that the machine is ready to run Kubernetes. These prechecks expose warnings and exit on errors. kubeadm init then downloads and installs the cluster control plane components.

Initialization of kubernetes master is a completely automated process that is controlled by the “kubeadm init“ command as shown.

# kubeadm init --apiserver-advertise-address=192.168.72.131 --pod-network-cidr=10.5.0.0/16

Once Kubernetes initialized successfully, you must enable your user to start using the cluster. In our scenario, we will be using the root user. You can also start the cluster using sudo user as shown.

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

Now you have to enable Kubernetes cluster and use flannel to get the config in yaml. And this should be running only on Master node,

# wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

# kubectl apply -f kube-flannel.yml

Now confirm that the kubectl command is activated.

# kubectl get pod --all-namespaces

k8s pods status

Initially you will see the status of the master node is “NotReady”. This is because we are yet to deploy the pod network to the cluster. The pod Network is the overlay network for the cluster, that is deployed on top of the present node network. It is designed to allow connectivity across the pod.

Conclusion

In this guide you saw how to how to Install and configure Kubernetes Cluster (k8s) on CentOS 8/RHEL 8. Please feel free to ask any questions or queries in comment box below.

Read Also : How to Install Docker Engine on Ubuntu 18.04/20.04

Read Also : Docker Tutorial – An Advanced Overview of Docker

Share on:

Ravindra Kumar

Hi, This is Ravindra. I am founder of TheCodeCloud. I am AWS Certified Solutions Architect Associate & Certified in Oracle Cloud as well. I am DevOps and Data Science Enthusiast.

Recommended Reading..

Leave a Comment