Kubernetes, or K8s, is an open-source container orchestration platform developed by Google. The core concept behind Kubernetes is that a user installs it on a server, or more likely a cluster, and deploys various workloads on it. Kubernetes addresses challenges related to container creation, scaling, namespaces, access rights, and more. The primary interaction with the cluster is through YAML configuration files.
This tutorial will guide you through creating and deploying a Kubernetes cluster locally.
We will set up the Kubernetes cluster on two virtual machines: one acting as the master node and the other as a worker node. While deploying a cluster with only two nodes is not practical for real-world use, it is sufficient for educational purposes. If you wish to create a Kubernetes cluster with more nodes, simply repeat the process for each additional node.
Set the node names for the cluster. On the master node, execute the following command:
sudo hostnamectl set-hostname master.local
On the worker node, execute:
sudo hostnamectl set-hostname worker.local
If there are multiple worker nodes, assign each a unique name: worker1.local
, worker2.local
, and so on.
To ensure that nodes are accessible by name, modify the hosts file on each node. Add the following lines:
192.168.43.80 master.local master
192.168.43.77 worker.local worker
Here, 192.168.43.80 and 192.168.43.77 are the IP addresses of each node. To find the IP address, use the ip addr command:
ip addr
Locate the IP address next to inet
. Open the hosts file and make the necessary edits:
sudo nano /etc/hosts
To verify that the VMs can communicate with each other, ping the nodes:
ping 192.168.43.80
If successful, you will receive a response similar to this:
PING 192.168.43.80 (192.168.43.80) 56(84) bytes of data.
64 bytes from 192.168.43.80: icmp_seq=1 ttl=64 time=0.054 ms
Next, install the necessary utilities and packages on each node. These steps should be applied to each node unless specified otherwise. Start by updating the package list and systems:
sudo apt-get update && apt-get upgrade -y
Then install the following packages:
sudo apt-get install curl apt-transport-https git iptables-persistent -y
Kubernetes will not start with an active swap file, so it needs to be disabled:
sudo swapoff -a
To prevent it from reactivating after a reboot, modify the fstab file:
sudo nano /etc/fstab
Comment out the line with #
:
# /swap.img none swap sw 0 0
Load additional kernel modules:
sudo nano /etc/modules-load.d/k8s.conf
Add the following two lines to k8s.conf
:
br_netfilter
overlay
Now, load the modules into the kernel:
sudo modprobe br_netfilter
sudo modprobe overlay
Verify the modules are loaded successfully:
sudo lsmod | egrep "br_netfilter|overlay"
You should see output similar to this:
overlay 147456 0
br_netfilter 28672 0
bridge 299008 1 br_netfilter
Create a configuration file to process traffic through the bridge in netfilter:
sudo nano /etc/sysctl.d/k8s.conf
Add the following two lines:
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
Apply the settings:
sudo sysctl --system
Run the following command to install Docker:
sudo apt-get install docker docker.io -y
For more details on installing Docker on Ubuntu, refer to the official guide.
After installation, enable Docker to start on boot and restart the service:
sudo systemctl enable docker
sudo systemctl restart docker
Add the GPG key:
sudo curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
Next, create a repository configuration file:
sudo nano /etc/apt/sources.list.d/kubernetes.list
Add the following entry:
deb https://apt.kubernetes.io/ kubernetes-xenial main
Update the apt-get
package list:
sudo apt-get update
Install the following packages:
sudo apt-get install kubelet kubeadm kubectl
Installation is now complete. Verify the Kubernetes client version:
sudo kubectl version --client
The output should be similar to this:
Client Version: version.Info{Major:"1", Minor:"24", GitVersion:"v1.24.2"}
Run the following command for the initial setup and preparation of the master node:
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
The --pod-network-cidr
flag specifies the internal subnet address, with 10.244.0.0/16 being the default value.
The process will take a few minutes. Upon completion, you will see the following message:
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.43.80:6443 --token f7sihu.wmgzwxkvbr8500al \
--discovery-token-ca-cert-hash sha256:6746f66b2197ef496192c9e240b31275747734cf74057e04409c33b1ad280321
Save this command to connect the worker nodes to the master node.
Create the KUBECONFIG
environment variable:
export KUBECONFIG=/etc/kubernetes/admin.conf
Install the Container Network Interface (CNI):
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
On the worker node, run the kubeadm join command obtained during the master node setup. After this, on the master node, enter:
sudo kubectl get nodes
The output should be:
NAME STATUS ROLES AGE VERSION
master.local Ready control-plane,master 10m v1.24.2
worker.local Ready <none> 79s v1.24.2
The cluster is now deployed and ready for operation.
Setting up a Kubernetes cluster involves several steps, from creating and configuring virtual machines to installing and configuring the necessary software components. This tutorial provided a step-by-step guide to deploying a basic Kubernetes cluster on a local environment. While this setup is suitable for educational purposes, real-world deployments typically involve more nodes and more complex configurations. Kubernetes provides powerful tools for managing containerized applications, making it a valuable skill for modern IT professionals. By following this guide, you've taken the first steps in mastering Kubernetes and its ecosystem.