Kubernetes Cluster: Installation, Configuration, and Management
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.
Creating Virtual Machines Copy link
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.
- We will use Oracle's VirtualBox to create virtual machines, which you can download from this link. After installation, proceed to create the virtual machines.
- For the operating system, we will use Ubuntu Server, which can be downloaded here. After downloading, open VirtualBox.
- Click "Create" in VirtualBox to create a new virtual machine. The default settings are sufficient, but allocate 3 GB of RAM and 2 CPUs for the master node (which manages the Kubernetes cluster) and 2 GB of RAM for the worker node. Kubernetes requires a minimum of 2 CPUs for the master node. Create two virtual machines this way.
- After creating the virtual machines, create a boot image with the Ubuntu Server distribution. Go to "Storage" and click "Choose/Create a Disk Image."
- Click "Add" and select the Ubuntu Server distribution. Then, start both machines and install the operating system by selecting "Try or Install Ubuntu." During installation, create users for each system and choose the default settings.
- After installation, shut down both virtual machines and go to their settings. In the "Network" section, change the connection type to "Bridged Adapter" for each system so that the virtual machines can communicate with each other over the network.
System Preparation Copy link
Network Configuration Copy link
Set the node names for the cluster. On the master node, execute the following command:
sudo hostnamectl set-hostname master.localOn the worker node, execute:
sudo hostnamectl set-hostname worker.localIf 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 workerHere, 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 addrLocate the IP address next to inet. Open the hosts file and make the necessary edits:
sudo nano /etc/hostsTo verify that the VMs can communicate with each other, ping the nodes:
ping 192.168.43.80If 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 msYou can enforce pod-to-pod isolation by applying the patterns in Kubernetes Cluster Network Policies tutorial—defining ingress and egress rules, using podSelector and namespace selectors to restrict traffic between master and worker nodes.
Updating Packages and Installing Additional Utilities Copy link
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 -yThen install the following packages:
sudo apt-get install curl apt-transport-https git iptables-persistent -ySwap File Copy link
Kubernetes will not start with an active swap file, so it needs to be disabled:
sudo swapoff -aTo prevent it from reactivating after a reboot, modify the fstab file:
sudo nano /etc/fstabComment out the line with #:
# /swap.img none swap sw 0 0Kernel Configuration Copy link
Load additional kernel modules:
sudo nano /etc/modules-load.d/k8s.confAdd the following two lines to k8s.conf:
br_netfilter
overlayNow, load the modules into the kernel:
sudo modprobe br_netfilter
sudo modprobe overlayVerify 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_netfilterCreate a configuration file to process traffic through the bridge in netfilter:
sudo nano /etc/sysctl.d/k8s.confAdd the following two lines:
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1Apply the settings:
sudo sysctl --systemDocker Installation Copy link
Run the following command to install Docker:
sudo apt-get install docker docker.io -yFor 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 dockerKubernetes Installation Copy link
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.listAdd the following entry:
deb https://apt.kubernetes.io/ kubernetes-xenial mainUpdate the apt-get package list:
sudo apt-get updateInstall the following packages:
sudo apt-get install kubelet kubeadm kubectlInstallation 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"}Cluster Configuration Copy link
Master Node Copy link
Run the following command for the initial setup and preparation of the master node:
sudo kubeadm init --pod-network-cidr=10.244.0.0/16The --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:6746f66b2197ef496192c9e240b31275747734cf74057e04409c33b1ad280321Save this command to connect the worker nodes to the master node.
Create the KUBECONFIG environment variable:
export KUBECONFIG=/etc/kubernetes/admin.confInstall the Container Network Interface (CNI):
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.ymlWorker Node Copy link
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 nodesThe 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.2The cluster is now deployed and ready for operation.
Conclusion Copy link
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.