Installing Kubernetes on CentOS 7 – What to Do?

Photo of author

By admin

Why and How to Install Kubernetes on CentOS7

Today is the age of Kubernetes. Its popularity has increased steadily since its inception. Open-source tool Kubernetes has made container management a lot easier and more efficient. K8s, also known as Kubernetes, has a constantly growing ecosystem. Plus, its services, tools, etc are easily accessible. K8s and its tooling are now essential for anyone or any company seeking to manage containers and containerized apps in an effortless manner. Many first-timers, however, struggle to install Kubernetes Clusters locally. That’s only natural. This guide is for you if you are one of those people. In this section, we will show you how to install Kubernetes on CentOS 7. Read on.

Why install Kubernetes Cluster on CentOS 7 anyway?

Now, the question is why you should opt for Kubernetes. Well, Kubernetes is an amazing innovation. It provides its users with many cool features that are hard to ignore. Here are those features:

  • Kubernetes is known for its convenient storage orchestration. In addition, it permits its users to auto-mount storage systems of their preference.
  • Kubernetes is capable of exposing containers using the DNS name or their IP address. In case the container traffic is gigantic, Kubernetes can load balance and dispense that network traffic in a way that results in a stable deployment.
  • Self-healing is another great feature of K8s. If a container fails, Kubernetes reboots them. If needed, replace some containers. Kubernetes also eradicates containers unresponsive to health checkups defined by you. Plus, it never shows those containers to the clients until they’re ready.
  • Kubernetes also offers its users the facility to auto-rollbacks and rollouts. This facility enables them to define a desirable condition of your K8s-deployed containers. And, it is capable of gradually modifying the real condition to the desirable condition.
  • The software also comes equipped with the auto-bin packing feature. Kubernetes users can specify the amount of CPU or RAM each container requires or give it a cluster to run containerized tasks on. Then, Kubernetes can place those containers into your nodes, capitalizing on your existing resources.
  • Kubernetes also allows the users to stow and manage their sensitive info, including their SSH keys and OAuth tokens. One can perform deployments and update secrets, and configure apps. That too, without reconstructing their container images or revealing secrets in their stack setups.

Steps to follow for installing Kubernetes on CentOS 7

Now coming to the article’s core, here are the steps you need to follow for installing Kubernetes on CentOS 7.

Prerequisites

  • 3 CentOS-installed servers
  • Root permission

Step 1. Install Kubernetes

The foremost task to perform here is to install Kubernetes (K8s) on all three servers. So, we got to make them ripe for it. You can make them ready for K8s installation by modifying the current setups on the servers and installing packages like Docker CE and then Kubernetes itself.

First, utilize the vim editor to edit your hosts file:

vim /etc/hosts

Copy-paste this host:

10.0.15.10 k8s-master

10.0.15.21 node01

10.0.15.22 node02

Once you’re done, save and quit the vim editor.

Next, you have to disable SELinux. This is a key step, so take heed. Execute the following command to disable SELinux:

setenforce 0

sed -i –follow-symlinks ‘s/SELINUX=enforcing/SELINUX=disabled/g’ /etc/sysconfig/selinux

After that, facilitate the br_netfilter module. This module has to be in an active state for installing Kubernetes. Enabling br_netfilter kernel module will allow the packets crossing the bridge to go through processing by iptables. The iptables help process these packets for port forwarding and filtering. It also permits the K8s pods on the cluster to make them communicate with one another.

Execute the following command for enabling the br_netfilter module:

modprobe br_netfilter

echo ‘1’ > /proc/sys/net/bridge/bridge-nf-call-iptables

Once it’s enabled, focus on disabling SWAP to allow a smooth K8s installation. Utilize the below command to do so:

swapoff -a

After that, take the etc/fstab file and make some edits:

vim /etc/fstab

Comment the SWAP line’s universally unique identifier like this:

# /dev/mapper/centos-swap swap swap defaults 0 0

Once finished, let’s proceed to install the latest version of Docker Community Edition (CE) from your Docker repository.

Install the package dependencies for Docker Community Edition using this command:

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

Attach the Docker repository to your system and Docker CE utilizing the following command:

yum-config-manager –add-repo https://download.docker.com/linux/centos/docker-ce.repo

yum install -y docker-ce

Cool your heels, allowing the Docker CE installation to get finished.

Next, you got to install the Kubernetes repository to your CentOS 7 system to run the below command:

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

After that, install kubeadm, kubectl, kubelet K8s packages utilizing the following yum command:

yum install -y kubelet kubeadm kubectl

After these packages are installed, reboot them:

sudo reboot

Sign in again to your server and begin both the kubelet and docker services:

systemctl start docker && systemctl enable docker

systemctl start kubelet && systemctl enable kubelet

It is also necessary to change your cgroup-driver. While installing Kubernetes to your CentOS 7 PC, you must ensure the usage of the same cgroup by both Kubernetes and Docker CE.

Examine the cgroup of the Docker utilizing the below command:

docker info | grep -i cgroup

Here, you should see your Docker using cgroupfs as its cgroup-driver.

Next, execute the following command for modifying the cgroup-driver of K8s to cgroupfs and thus, making both alike:

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

Load the systemd system again and reboot your kubelet service:

systemctl daemon-reload

systemctl restart kubelet

Now, we are all set for our K8s cluster.

Step 2. Initialize Kubernetes cluster

Here, we got to initialize our K8s master cluster setup. So, first, relocate the shell to your master server and execute this command to configure your K8s master cluster:

kubeadm init –apiserver-advertise-address=10.0.15.10 –pod-network-cidr=10.244.0.0/16

Once your K8s initialization is finished, you will get a relevant output stating the same.

  • You have to copy-paste the kubeadm join … … … to some text editor. This command will become essential during the registration of new nodes to your K8s cluster.
  • Next, you have to execute a few commands to start using your Kubernetes software.

First, generate a new .kube setup directory and copy the setup admin.conf:

mkdir -p $HOME/.kube

sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

sudo chown $(id -u):$(id -g) $HOME/.kube/config

After doing so, perform the flannel network deployment to the K8s cluster using this command:

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

Now, the flannel network deployment to your K8s cluster is done.

Wait for some time and then examine your K8s nodes and pods by putting the below command to use:

kubectl get nodes

kubectl get pods –all-namespaces

If successful, the output you will receive from this command execution would display the status of the “k8s-master” as “Ready.” Also, it would show the status of all the required pods, including the ‘kube-flannel-ds” as “Running.” These pods are crucial for configuring network pods.

And with that, the initialization and setup of your K8s master cluster is complete.

Step 3. Add both the worker nodes to your cluster

Step 3 is all about adding both the worker nodes to your K8s cluster.

Establish a connection with your first node. Remember the kubeadm join command we copied before? We need to run that command here:

kubeadm join 10.0.15.10:6443 –token vzau5v.vjiqyxq26lzsf28e –discovery-token-ca-cert-hash sha256:e6d046ba34ee03e7d55e1f5ac6d2de09fd6d7e6959d16782ef0778794b94c61e

Now, connect with the second server as well and run the same kubeadm join command again:

kubeadm join 10.0.15.10:6443 –token vzau5v.vjiqyxq26lzsf28e –discovery-token-ca-cert-hash sha256:e6d046ba34ee03e7d55e1f5ac6d2de09fd6d7e6959d16782ef0778794b94c61e

After waiting for a minute or two, verify the status of pods and nodes using this command:

kubectl get nodes

kubectl get pods –all-namespaces

If everything is done right, you’d receive an output stating that both the nodes have become a part of your cluster.

Step 4. Test construct your first pod

It’s best to deploy a demo Nginx pod as a test to your K8s cluster to ensure if everything was fine. Pods are assemblages of one/more containers, for instance, Docker containers, having common storage and network that run on K8s.

Sign in to your Kubernetes master server and deploy a new pod, namely “nginx,” using the below command:

kubectl create deployment nginx –image=nginx

Once deployed, you can see info of your Nginx deployment specs by executing this command:

kubectl describe deployment nginx

Doing so will display the Nginx deployment specs right then.

Now, you have to reveal your Nginx pod attainable online. Thus, for this purpose, you have to generate a new service called NodePort by executing the command below:

kubectl create service nodeport nginx –tcp=80:80

Ensure the flawlessness of the command and then view your Nginx service NodePort and IP address:

kubectl get pods

kubectl get svc

Notice both the port numbers from the output attentively, though we only need NodePort. Usually, it’s 30691.

Run this command from your K8s master server:

curl node01:30691

curl node02:30691

The Nginx pod deployment on your Kubernetes cluster isyea successful and accessible online. Congratulations on a triumphant installation of a K8s cluster on your PC running on CentOS 7.

Conclusion

To conclude, Kubernetes (K8s) is an essential tool for managing clusters residing on different servers. Besides making the deployment process more facile, it also makes it more fruitful. Why struggle with container management when Kubernetes can be your lifesaver? That’s why we decided to guide you so that you too can find the task of installing Kubernetes on CentOS 7 easy. We hope that this write-up suffices as an effective terminus quo for all the first-time users out there.

As you may comprehend by going through the article, the process of installing a Kubernetes cluster itself is nothing fancy. But it can end up puzzling you if you aren’t attentive enough. From installing the Kubernetes tool to create a pod as a demo to ensure that everything works perfectly – follow every step scrupulously. Skipping even a tiny part or forgetting to execute even one command could mess everything up.

Just comply with the stuff mentioned above, install Kubernetes on your local CentOS 7 machine, and bid your container management problems goodbye. All the best…and oh, happy clustering!

Leave a Comment