DEV Community

Jayshri Landge
Jayshri Landge

Posted on

Installing a Kubernetes Cluster on CentOS 9

Introduction
Kubernetes (K8s) is an open-source platform that helps manage containerized applications by automating tasks like deployment, scaling, and monitoring. It makes sure your apps run smoothly, scale up or down as needed, and recover automatically if something goes wrong. Kubernetes handles all the complex parts of managing applications, so developers can focus more on building features. It’s widely used for cloud applications because it makes apps more reliable and easier to manage.

What is Kubernetes?
Kubernetes is a tool that helps you manage and run containerized applications. It automates the tasks of deploying, scaling, and managing these apps, making sure everything runs smoothly. Kubernetes works by grouping containers into clusters, handling things like fixing broken parts, balancing traffic, and updating apps without downtime. It makes it easier to manage large applications in different environments.

Why Kubernetes?
Kubernetes makes managing containerized apps easier by taking care of complex tasks like scaling and making sure apps stay up and running. It helps you use resources efficiently and makes sure your apps are always available. Kubernetes also works well with tools that help in automating app updates and deployments, making it a great choice for running modern apps in the cloud.

Prerequisites
Before beginning the installation process, ensure you have the following prerequisites in place:

  • Internet connectivity to download necessary packages and repositories.
  • A minimum of three nodes (one master and two worker nodes) running on CentOS 9.
  • Each node should have a minimum of 2GB of RAM and 2 CPU cores to ensure smooth Kubernetes operation.
  • You should have root access to all nodes (master and worker nodes) to perform system-level installations and configurations.

Step-by-Step Installation Guide

Step 1: Update /etc/hosts on Master Node
I used this command to add a new entry to the /etc/hosts file and Start by updating the /etc/hosts file on the master node to ensure proper name resolution. This will help the Kubernetes nodes communicate with each other by their hostname.

echo "192.168.221.135 k8-master" >> /etc/hosts
Enter fullscreen mode Exit fullscreen mode

In this step, you're associating the IP address 192.168.221.135 with the hostname k8-master. This ensures that the master node can be reached by other nodes in the cluster using the hostname.
Image description

Step 2: Install Kernel Headers on Master Node
Install the kernel headers for the current kernel version. This is essential for ensuring that the necessary kernel modules are available for Kubernetes components to function properly.

sudo dnf install kernel-devel-$(uname -r)
Enter fullscreen mode Exit fullscreen mode

Image description

Step 3: Load Kernel Modules on Master Node
Kubernetes requires certain kernel modules for proper networking and container management. These modules need to be loaded both on the master and worker nodes.

sudo modprobe br_netfilter
sudo modprobe ip_vs
sudo modprobe ip_vs_rr
sudo modprobe ip_vs_wrr
sudo modprobe ip_vs_sh
sudo modprobe overlay
Enter fullscreen mode Exit fullscreen mode

These commands load essential kernel modules needed for Kubernetes. The br_netfilter module enables network bridge support, various ip_vs modules support different IP Virtual Server load balancing methods, and the overlay module is for container storage support.
Image description

sudo tee /etc/modules-load.d/kubernetes.conf << EOF
br_netfilter
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
overlay
EOF
Enter fullscreen mode Exit fullscreen mode

This commands creates a configuration file and write the necessary kernel modules for kubernetes into it.This ensures that the modules are automatically loaded at boot time.
Image description

Step 4: Configure Sysctl on Master Node
Enable IP forwarding and configure the system for Kubernetes networking.

sudo tee /etc/sysctl.d/kubernetes.conf > /dev/null << EOF
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
Enter fullscreen mode Exit fullscreen mode

This command creates a configuration file /etc/sysctl.d/kubernetes.conf and writes network settings into it. These settings enable IPv4 forwarding and ensure that bridged IPv4 and IPv6 traffic is processed by iptables, which is necessary for Kubernetes networking.
Image description
This ensures proper handling of network traffic between pods and services in Kubernetes cluster.

sudo sysctl --system
Enter fullscreen mode Exit fullscreen mode

This command applies the changes to kernel parameters, ensuring that the network settings for Kubernetes, such as IP forwarding and iptables handling, are properly configured to manage network traffic between pods and services in the cluster.
Image description

Step 5: Disable Swap on Master Node
Kubernetes requires swap to be disabled for optimal performance. Disable swap and comment out the swap entry

sudo sed -e '/swap/s/^/#/g' -i /etc/fstab
sudo swapoff -a
Enter fullscreen mode Exit fullscreen mode

Image description

Step 6: Install Containerd on Master Node
Containerd is the container runtime used by Kubernetes to manage containers. Install and configure containerd to use systemd as the cgroup driver.

sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Enter fullscreen mode Exit fullscreen mode

This command adds the Docker repository to the system’s package manager, allowing you to install Docker from the official Docker repository on CentOS.
Image description

sudo dnf makecache
Enter fullscreen mode Exit fullscreen mode

This command updates the package manager's cache by downloading metadata from all enabled repositories, ensuring that the system has the latest information about available packages.
Image description

sudo dnf -y install containerd.io
Enter fullscreen mode Exit fullscreen mode

This command installs the containerd.io package, which is the container runtime used by Docker and Kubernetes, allowing the system to run and manage containers.
Image description

sudo sh -c "containerd config default > /etc/containerd/config.toml"
Enter fullscreen mode Exit fullscreen mode

This command generates the default configuration file for containerd and saves it to /etc/containerd/config.toml, allowing you to customize the container runtime settings if needed.
Image description

sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml
Enter fullscreen mode Exit fullscreen mode

This command modifies the containerd configuration file by changing the SystemdCgroup setting from false to true, enabling systemd to manage cgroups, which is required for better integration with Kubernetes.
Image description

sudo systemctl enable --now containerd.service
Enter fullscreen mode Exit fullscreen mode

This command enables the containerd service to start automatically at boot and starts the service immediately, allowing the container runtime to run on the system.
Image description

Step 7: Configure Firewall and SELinux on Master Node
Disable the firewall and set SELinux to permissive mode to prevent potential conflicts with Kubernetes.

systemctl stop firewalld && systemctl disable firewalld
Enter fullscreen mode Exit fullscreen mode

This command stops the firewalld service and disables it from starting automatically on boot, effectively turning off the firewall on the system.
Image description

sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
Enter fullscreen mode Exit fullscreen mode

This command modifies the SELinux configuration file to set the SELinux mode to permissive, which allows the system to log security violations without enforcing restrictions.
Image description

Step 8: Add Kubernetes Repository on Master Node
Add the Kubernetes repository to install the required components for Kubernetes (kubelet, kubeadm, and kubectl).

cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://pkgs.k8s.io/core:/stable:/v1.31/rpm/
enabled=1
gpgcheck=1
gpgkey=https://pkgs.k8s.io/core:/stable:/v1.31/rpm/repodata/repomd.xml.key
exclude=kubelet kubeadm kubectl cri-tools kubernetes-cni
EOF
Enter fullscreen mode Exit fullscreen mode

Image description

Step 9: Install Kubernetes Components on Master Node
Install kubelet, kubeadm, and kubectl. These are the main components required to run and manage your Kubernetes cluster.

sudo dnf makecache
Enter fullscreen mode Exit fullscreen mode

Image description

sudo dnf install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
Enter fullscreen mode Exit fullscreen mode

Image description

Step 10: Set Up SSH Key-Based Authentication
Generates an SSH key pair for secure communication between nodes.

ssh-keygen
Enter fullscreen mode Exit fullscreen mode

This command generates a new SSH key pair (public and private keys) for secure authentication when connecting to remote systems via SSH
Image description
Copies the generated SSH public key to the master node for passwordless SSH access.

ssh-copy-id root@k8-master
Enter fullscreen mode Exit fullscreen mode

Image description

Step 11: Start and Enable Kubelet on Master Node
Enable and start the kubelet service, which is essential for the cluster node to function as part of Kubernetes.

systemctl restart kubelet && systemctl enable kubelet
Enter fullscreen mode Exit fullscreen mode

Image description

Step 12: Initialize the Kubernetes Cluster on Master Node
Initialize the Kubernetes cluster using kubeadm. This sets up the master node with all the required components.

sudo kubeadm init --pod-network-cidr=192.168.221.135/24
Enter fullscreen mode Exit fullscreen mode

Image description

After successful execution of the kubeadm init command, you will be provided with a kubeadm join command, which you'll use to join the worker nodes to the cluster.
Image description

Step 13: Set up kubectl on Master Node
Set up the local Kubernetes configuration (kubectl configuration file) to interact with your cluster.

mkdir -p $HOME/.kube
Enter fullscreen mode Exit fullscreen mode

This command creates a .kube directory in the user's home directory if it doesn't already exist. This directory is typically used to store Kubernetes configuration files, such as config, for managing cluster connections.
Image description

sudo cp -i /etc/kubernetes/admin.conf  $HOME/.kube/config
Enter fullscreen mode Exit fullscreen mode

This command copies the Kubernetes admin configuration file (admin.conf) to the .kube/config file in the user's home directory, allowing the user to interact with the Kubernetes cluster using kubectl with the appropriate configuration.
Image description

sudo chown $(id -u):$(id -g) $HOME/.kube/config
Enter fullscreen mode Exit fullscreen mode

This command changes the ownership of the config file in the $HOME/.kube/ directory to the current user and group, allowing the user to read and modify the file without requiring superuser privileges.
Image description

cat /etc/hosts
Enter fullscreen mode Exit fullscreen mode

This command displays the contents of the /etc/hosts file, which maps IP addresses to hostnames for local network communication on the system.
Image description

Step 14: Install Flannel Network Plugin on Master Node

kubectl get node -w
Enter fullscreen mode Exit fullscreen mode

Image description
Install a network plugin (Flannel in this case) to enable communication between the pods in your Kubernetes cluster.

sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
Enter fullscreen mode Exit fullscreen mode

Image description

Step 15: Verify Cluster Status on Master Node
Check the status of your nodes to ensure everything is running as expected.

kubectl get node -w
Enter fullscreen mode Exit fullscreen mode

Image description

kubectl get node
Enter fullscreen mode Exit fullscreen mode

This command displays the list of nodes in the Kubernetes cluster along with their status, roles, age, and version. It provides a snapshot of the cluster's nodes at the time of execution.
Image description

Configuring Worker Node (k8-node)

On the worker node (e.g., k8-node), the process involves setting up the system to join the Kubernetes cluster as a node.

Step 1: Check IP Address

ip a
Enter fullscreen mode Exit fullscreen mode

This command displays the network interfaces and IP addresses of the node.
Image description
Update the Hosts File:

echo "192.168.221.135 k8-master" >> /etc/hosts
Enter fullscreen mode Exit fullscreen mode

This command adds the master node's IP address and hostname to the hosts file for name resolution.
Image description

Step 2:Install Kernel Development Tools

sudo dnf install kernel-devel-$(uname -r)
Enter fullscreen mode Exit fullscreen mode

This command installs the kernel development package for the current kernel version.
Image description

Step 3:Load Required Kernel Modules

sudo modprobe br_netfilter
Enter fullscreen mode Exit fullscreen mode

Loads the bridge netfilter module for network bridge support.
Image description

sudo modprobe ip_vs
Enter fullscreen mode Exit fullscreen mode

Loads the IP Virtual Server module for load balancing.
Image description

sudo modprobe ip_vs_rr
Enter fullscreen mode Exit fullscreen mode

Loads the round-robin scheduling module for IPVS.
Image description

sudo modprobe ip_vs_wrr
Enter fullscreen mode Exit fullscreen mode

Loads the weighted round-robin scheduling module for IPVS.
Image description

sudo modprobe ip_vs_sh
Enter fullscreen mode Exit fullscreen mode

Loads the source hashing scheduling module for IPVS.
Image description

sudo modprobe overlay
Enter fullscreen mode Exit fullscreen mode

Loads the overlay filesystem module for container storage.
Image description
These commands load the necessary kernel modules for Kubernetes networking and container support.

Step 4: Configure Modules to Load at Boot

sudo tee /etc/modules-load.d/kubernetes.conf << EOF
br_netfilter
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
overlay
EOF
Enter fullscreen mode Exit fullscreen mode

This command creates a configuration file to load the necessary modules at boot time.
Image description

Step 5: Set System Parameters for Kubernetes

sudo tee /etc/sysctl.d/kubernetes.conf > /dev/null << EOF
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
Enter fullscreen mode Exit fullscreen mode

These commands configure sysctl settings required by Kubernetes and apply them.
Image description

sudo sysctl --system
Enter fullscreen mode Exit fullscreen mode

This command reloads all system configuration files for kernel parameters, applying changes made to settings like network configurations or other system settings that are defined in /etc/sysctl.conf or /etc/sysctl.d/ directories.
Image description

Step 6: Disable Swap

sudo sed -e '/swap/s/^/#/g' -i /etc/fstab
Enter fullscreen mode Exit fullscreen mode

This command comments out any line related to swap in the /etc/fstab file by adding a # at the beginning of the line. This effectively disables swap on the system, which is required for certain Kubernetes configurations.
Image description

sudo swapoff -a
Enter fullscreen mode Exit fullscreen mode

These commands disable swap, which is required for Kubernetes to function properly.
Image description

Step 7: Add Docker Repository and Containerd

sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Enter fullscreen mode Exit fullscreen mode

This command adds the official Docker repository to the system's package manager, enabling the installation of Docker CE (Community Edition) on a CentOS system.
Image description

sudo dnf makecache
Enter fullscreen mode Exit fullscreen mode

This command updates the package manager's cache by downloading metadata from all enabled repositories, ensuring that the system has the latest information about available packages for installation.
Image description

sudo dnf -y install containerd.io
Enter fullscreen mode Exit fullscreen mode

This command installs the containerd.io package, which is the container runtime used by Docker and Kubernetes, allowing the system to run and manage containers.
Image description

sudo sh -c "containerd config default > /etc/containerd/config.toml"
Enter fullscreen mode Exit fullscreen mode

This command generates the default configuration file for containerd and saves it to /etc/containerd/config.toml, allowing for customization of the container runtime settings if needed.
Image description

sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml
Enter fullscreen mode Exit fullscreen mode

This command modifies the containerd configuration file to set SystemdCgroup to true, enabling systemd to manage control groups (cgroups) for better integration with Kubernetes.
Image description

sudo systemctl enable --now containerd.service
Enter fullscreen mode Exit fullscreen mode

This command enables the containerd service to start automatically at boot and starts it immediately, allowing the container runtime to begin running on the system and configure it to use systemd as the cgroup driver.
Image description

Step 8: Disable Firewall and Set SELinux to Permissive

systemctl stop firewalld && systemctl disable firewalld
Enter fullscreen mode Exit fullscreen mode

This command stops the firewalld service and disables it from starting automatically on boot, effectively turning off the firewall on the system.-uploads.s3.amazonaws.com/uploads/articles/k6c5yr1h9jyol8gve6ch.png)

sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
Enter fullscreen mode Exit fullscreen mode

This command changes the SELinux mode from enforcing to permissive in the /etc/selinux/config file.set SELinux to permissive mode, which are common requirements for Kubernetes.
Image description

Step 9: Add Kubernetes Repository and Install Kubernetes Components

cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://pkgs.k8s.io/core:/stable:/v1.31/rpm/
enabled=1
gpgcheck=1
gpgkey=https://pkgs.k8s.io/core:/stable:/v1.31/rpm/repodata/repomd.xml.key
exclude=kubelet kubeadm kubectl cri-tools kubernetes-ci
EOF
Enter fullscreen mode Exit fullscreen mode

This command creates a Kubernetes repository configuration file at /etc/yum.repos.d/kubernetes.repo and writes the necessary details into it. It specifies the repository URL, enables the repository, and excludes certain Kubernetes packages (kubelet, kubeadm, kubectl, etc.) from being updated.
Image description

sudo dnf makecache
Enter fullscreen mode Exit fullscreen mode

This command updates the package manager's cache by downloading metadata from all enabled repositories, ensuring that the system has the latest package information available for installation or updates.
Image description

sudo dnf install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
Enter fullscreen mode Exit fullscreen mode

These commands add the Kubernetes repository, refresh the package cache, and install Kubernetes components.
Image description

Step 10: Generate SSH Key Pair

ssh-keygen
Enter fullscreen mode Exit fullscreen mode

This command generates an SSH key pair for secure access to the master node.
Image description

Step 11:Update Hosts File and Check Entries

cat /etc/hosts
Enter fullscreen mode Exit fullscreen mode

This command displays the contents of the /etc/hosts file
Image description

vi /etc/hosts
Enter fullscreen mode Exit fullscreen mode

These commands allow you to view and manually edit the hosts file if necessary.'
Image description

Step 12: Copy SSH Key to Master Node

ssh-copy-id root@k8-master
Enter fullscreen mode Exit fullscreen mode

This command copies the SSH key to the master node for passwordless SSH login.
Image description

Copy SSH Key to Worker Node:

ssh-copy-id root@k8-node1
Enter fullscreen mode Exit fullscreen mode

This command copies the SSH key to another worker node for passwordless SSH login.
Image description

Step 13: Restart and Enable Kubelet

systemctl restart kubelet && systemctl enable kubelet
Enter fullscreen mode Exit fullscreen mode

These commands restart the kubelet service and enable it to start on boot.
Image description

Step 14: Join the Worker Node to the Cluster

kubeadm join 192.168.221.135:6443 --token <your-token> --discovery-token-ca-cert-hash sha256:<your-hash>
Enter fullscreen mode Exit fullscreen mode

If you
This command joins the worker node to the Kubernetes cluster using the token and CA certificate hash provided during the master node initialization.
Image description

Step 15: Verify Node Addition on Master Node
Check the status of the worker node after it joins the cluster.

kubectl get node
Enter fullscreen mode Exit fullscreen mode

Image description
By following above steps, I've successfully configure the worker node and join it to Kubernetes cluster on My Virtual Machine.

This completes the configuration of the worker node (k8-node). Now Virtual Machine Kubernetes cluster should be up and running with the master and worker nodes ready to manage and run your containerized applications on my virtual machine.

Conclusion
I've successfully set up a Kubernetes cluster on CentOS 9 with both master and worker nodes. With this setup, so now I can manage containerized applications efficiently and scale them based on demand.

Top comments (0)