Kubernetes

How to Install a Kubernetes Cluster on Red Hat CentOS 8

3 Mar 2020 12:00pm, by

If you’ve migrated over from Red Hat’s CentOS 7 to CentOS 8, you’ve probably noticed a lot of changes have taken place. Many of those changes have caused admins to approach their tasks differently. Such is the case with installing Kubernetes. If you’re looking to deploy a Kubernetes cluster on CentOS 8, the changes made to the operating system will directly affect you.

But how? Although they don’t change the way you deploy the actual cluster, getting everything in place for the deployment is quite different.

Fear not, I’m going to walk you through the process of installing Kubernetes on CentOS 8, so you can then get back to the business of deploying your cluster and managing your containers.

What You’ll Need

In order to successfully install Kubernetes (and create a cluster), you’ll need at least two machines. I’ll be demonstrating on two CentOS 8 servers, running on IP addresses:

  • kubemaster – 10.34.1.139
  • kubenode – 10.34.1.161

You’ll also need a user account with sudo privileges and access to the root user account.

With all of that in hand, let’s get to work.

Unless otherwise noted, you’ll take care of everything below on both the master and nodes.

Hostnames

The first thing to do is set the hostnames of your servers and then modify the /etc/hosts file. First set the hostname with the command:

sudo hostnamectl set-hostname HOSTNAME

Where HOSTNAME is the hostname to be used (such as kubemaster or kubenode). Once you’ve set the hostname, log out and log back in.

Next, open the hosts file for editing with the command:

sudo nano /etc/hosts

In that file, add something like this at the bottom (modifying to match your hostnames and IP addresses):

Save and close the file.

Installing Docker-CE

At the moment, Kubernetes cannot work with Podman (which is now the default container engine for both RHEL and CentOS). Because of this, you’ll need to install the docker engine. There is a bit of trickery to be had for this process.

Here’s how.

The first thing to do is to install an older version of docker-ce with the command:

sudo dnf install docker-ce-3:18.09.1-3.el7

Next, you need to open up the proper firewall ports with the following commands:

sudo firewall-cmd --permanent --add-port=6443/tcp

sudo firewall-cmd --permanent --add-port=2379-2380/tcp

sudo firewall-cmd --permanent --add-port=10250/tcp

sudo firewall-cmd --permanent --add-port=10251/tcp

sudo firewall-cmd --permanent --add-port=10252/tcp

sudo firewall-cmd --permanent --add-port=10255/tcp

sudo firewall-cmd –reload

sudo modprobe br_netfilter

sudo echo '1' > /proc/sys/net/bridge/bridge-nf-call-iptables

Start and enable the Docker daemon with the command:

sudo systemctl enable --now docker

Now you can add your user to the docker group with the command:

sudo usermod -aG docker $USER

Once you’ve added the user, log out and log back in so the user can work with the docker engine without having to make use of sudo (for security purposes).

Next, install the containerd.io package with the command:

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

After that completes, install the latest version of docker-ce with the command:

sudo dnf install docker-ce

You’re good to continue on.

Install Kubernetes

Now we can install Kubernetes on CentOS. First, we must create a new repository file with the command:

sudo nano /etc/yum.repos.d/kubernetes.repo

In that file, paste the following:

Save and close the file. Install the necessary Kubernetes packages with the command:

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

Start and enable the service with the command:

sudo systemctl enable --now kubelet

Now we’re going to have to su to the root user and then create a new file (to help configure iptables) with the command:

nano /etc/sysctl.d/k8s.conf

In that file, paste the following contents:

Save and close the file. Load the new configuration with the command:

sysctl --system

Exit out of the root user with the exit command.

Disable Swap

Next we must disable swap. Do that with the command:

sudo swapoff -a

That will temporarily disable swap. To permanently disable swap (so it’s not re-enabled upon reboot), open the fstab for editing with the command:

sudo nano /etc/fstab

In that file comment out (add a # character at the beginning of a line) the line that begins with:

/dev/mapper/cl-swap

Save and close the file.

Create a Daemon File

Finally, we have to create a daemon file. Do that by first issuing the command su and then creating the new daemon file with the command:

nano /etc/docker/daemon.json

In that file, paste the following:

Save and close the file.

Create a new systemd directory with the command:

mkdir -p /etc/systemd/system/docker.service.d

Reload and restart the Docker daemon with the commands:

Exit out of the root user with the exit command.

Initialize the Cluster

This is only done on the Kubernetes master. To initialize the cluster, issue the command:

sudo kubeadm init

The above will initialize the cluster and report to you the necessary command used to join the nodes to your master.

And that is the current method for installing Kubernetes and initializing a cluster on CentOS 8. At the moment, there’s no telling if that will change once Podman evolves and matures. Until that time, however, this is one of the methods I’ve had success with. Give it a try and see if it doesn’t become your go-to path for getting Kubernetes up and running on CentOS 8.

Red Hat is a sponsor of The New Stack.

A newsletter digest of the week’s most important stories & analyses.