Kubernetes is a free and open-source platform to manage the containerized applications. It helps you to manage, scale, and automatically deploy your containerized applications in a clustered environment. It was developed by Google and maintained by the Cloud Native Computing Foundation. It's becoming the most popular platform for deploying and managing software in the cloud. Kubernetes uses master-slave architecture, where the master node is responsible for managing and controlling all workers.
In this tutorial, we'll show you how to deploy a two-node Kubernetes cluster on CentOS 8.
Prerequisites
- Two servers running on CentOS 8 with a minimum 4 GB of RAM.
- A root password configured on both servers.
Getting Started
To begin with, you'll need to set up hostname resolution on both master and worker nodes so both nodes can communicate with each other using the hostname.
Edit the /etc/hosts file on both nodes:
# nano /etc/hosts
Add the following lines:
your-masternode-ip master
your-workernode-ip worker
Save and close the file when you're finished.
Next, you will need to disable the Swap partition in order to initialize the Kubernetes cluster. You can disable it with the following command:
# swapoff -a
Install Docker
Next, you'll need to install Docker on both servers. By default, Docker is not included in the CentOS 8 default repository.
So, add the Docker repository with the following command:
# dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo
Once the repository is added, install the Docker CE with the following command:
# dnf install docker-ce --nobest
After installing Docker, start Docker and enable it to start at system reboot:
# systemctl enable --now docker
To go on, verify Docker's status using the following command:
# systemctl status docker
You should get the following output:
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2021-06-12 01:37:05 EDT; 10s ago
Docs: https://docs.docker.com
Main PID: 2209 (dockerd)
Tasks: 10
Memory: 47.1M
CGroup: /system.slice/docker.service
└─2209 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Next, check the Docker version with the following command:
# docker --version
You should get the following output:
Docker version 20.10.7, build f0df350
At this point, Docker has been installed on both nodes.
Install Kubernetes on Master and Worker Nodes
Next, you'll need to install Kubernetes on both nodes.
First, create a repository for Kubernetes with the following command:
# nano /etc/yum.repos.d/kubernetes.repo
And then add the following lines:
[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
exclude=kubelet kubeadm kubectl
Save and close the file, then upgrade the repo with the following command:
# dnf upgrade -y
Next, install all Kubernetes tools with the following command:
# dnf install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
At this point, Kubernetes is installed on both Master and Worker nodes.
Warning
If you're using a Clouding server to follow this tutorial, this part isn't necessary, since Clouding's image includes a desactivated firewall by default.
Configure Firewall
Next, you will need to allow all required ports through firewalld on both nodes.
You can allow all required ports with the following command:
# firewall-cmd --zone=public --permanent --add-port=6443/tcp
# firewall-cmd --zone=public --permanent --add-port=2379-2380/tcp
# firewall-cmd --zone=public --permanent --add-port=10250/tcp
# firewall-cmd --zone=public --permanent --add-port=10251/tcp
# firewall-cmd --zone=public --permanent --add-port=10252/tcp
# firewall-cmd --zone=public --permanent --add-port=10255/tcp
Next, reload the firewall to apply the changes:
# firewall-cmd --reload
Next, set up the bridge configuration with the following command:
# modprobe br_netfilter
# echo '1' > /proc/sys/net/bridge/bridge-nf-call-iptables
Configure Kubernetes Cluster on Master Node
First, configure kubeadm with the following command:
# kubeadm config images pull
You should see the following output:
[config/images] Pulled k8s.gcr.io/kube-apiserver:v1.21.1
[config/images] Pulled k8s.gcr.io/kube-controller-manager:v1.21.1
[config/images] Pulled k8s.gcr.io/kube-scheduler:v1.21.1
[config/images] Pulled k8s.gcr.io/kube-proxy:v1.21.1
[config/images] Pulled k8s.gcr.io/pause:3.4.1
[config/images] Pulled k8s.gcr.io/etcd:3.4.13-0
[config/images] Pulled k8s.gcr.io/coredns/coredns:v1.8.0
Next, initialize the Kubernetes cluster with the following command:
# kubeadm init --pod-network-cidr=192.168.0.0/16
You should get the following output:
Your Kubernetes control-plane has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Alternatively, if you are the root user, you can run:
export KUBECONFIG=/etc/kubernetes/admin.conf
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 45.58.32.52:6443 --token 95maq6.dbn7ooiz50eol73y \
--discovery-token-ca-cert-hash sha256:d705a3ab93e6cc3c38585bfa33717f6369f99340a09e03a4030aa4479db7ee1e
Note down the join token as shown in the above output. You will need to run this on the Worker node.
Next, create the following directory and configuration file:
# mkdir -p $HOME/.kube
# cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
# chown $(id -u):$(id -g) $HOME/.kube/config
Next, deploy a pod network with the following command:
# kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
Wait for some time and check your Kubernetes cluster with the following command:
# kubectl get nodes
You should see that your Master node is enabled and running:
NAME STATUS ROLES AGE VERSION
master Ready control-plane,master 2m51s v1.21.1
Configure Kubernetes on Worker Node
Kubernetes cluster has one or more Worker nodes. So you'll need to go to the Worker node and join it to the Master node.
Run the following command to join the Worker node to the Kubernetes cluster:
# kubeadm join 45.58.32.52:6443 --token 95maq6.dbn7ooiz50eol73y --discovery-token-ca-cert-hash sha256:d705a3ab93e6cc3c38585bfa33717f6369f99340a09e03a4030aa4479db7ee1e
You should get the following output:
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.
Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
Now, go back to the Master node and verify the Kubernetes cluster:
# kubectl get nodes
You should see two nodes with "Ready" status:
NAME STATUS ROLES AGE VERSION
master Ready control-plane,master 5m54s v1.21.1
worker Ready 59s v1.21.1
If you want to reset all the configuration and start the process again, run the following command on both Master and Worker node:
# kubeadm reset
# rm -rf /etc/cni/net.d
Conclusion
Congratulations! You've successfully deployed Kubernetes cluster on CentOS 8.
You can now deploy your applications in the clustered environment.