❤️ ABS is one of the five Certified Kubernetes Service Providers in India ❤️

Setting up a Kubernetes Cluster on Bare Metal Server

Introduction

Setting up a cluster on bare-metal server typically involves multiple steps like provisioning the servers, configuring the network, and installing cluster management tools. But with proper guidelines it can be made more simple. 

Before you begin, make sure you have the following exact procedure or commands it may vary depending on your operating system and specific cluster software you are using.

Prerequisites:
  • Bare metal server with supported OS installed. (e.g., Ubuntu).
  • You’ll need minimum two or more servers with sufficient CPU, RAM, and storage to handle workload.
  • SSH access to each server and access to log in as a sudo user.
  • A basic understanding of networking to configure. You can use either a public or private network.
  • Required software and dependencies.
Steps to Install Kubernetes Cluster on Bare Metal:
1. Update your Nodes
				
					sudo apt update
sudo apt upgrade

# Install the necessary packages to allow APT sources.
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common

				
			
2. Install Container Runtime on all Nodes

The container runtime should be present on all nodes. You can find installation instructions for your chosen runtime on the official documentation. We will use ContainerD

https://kubernetes.io/docs/setup/production-environment/container-runtimes/#containerd

				
					# Install Containerd
wget https://github.com/containerd/containerd/releases/download/v1.6.16/containerd-1.6.16-linux-amd64.tar.gz

# To extract tar file
tar Cxzvf /usr/local containerd-1.6.16-linux-amd64.tar.gz
wget https://raw.githubusercontent.com/containerd/containerd/main/containerd.service
mkdir -p /usr/local/lib/systemd/system

# To move the file into specific directory
mv containerd.service /usr/local/lib/systemd/system/containerd.service

# Configure the systemd cgroup driver
sudo nano /etc/containerd/config.toml

# Add the following configuration
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
SystemdCgroup = true

# To check or enable the containerd service is running
sudo systemctl daemon-reload
sudo systemctl enable containerd
sudo systemctl status containerd

				
			
Install RunC

RunC is the container runtime for containerd that provides a low-level interface also this tool is responsible for running containers.

				
					wget https://github.com/opencontainers/runc/releases/download/v1.1.4/runc.amd64
sudo install -m 755 runc.amd64 /usr/local/sbin/runc

# To verify the installation
runc --version
				
			
Install CRICTL

Crictl is a tool for interacting with the container runtime interface. You can install it using the following commands.

				
					VERSION="v1.26.0" # Check latest version in releases page

# Download crictl
wget https://github.com/kubernetes-sigs/cri-tools/releases/download/$VERSION/crictl-$VERSION-linux-amd64.tar.gz
sudo tar zxvf crictl-$VERSION-linux-amd64.tar.gz -C /usr/local/bin
rm -f crictl-$VERSION-linux-amd64.tar.gz

cat <<EOF | sudo tee /etc/crictl.yaml
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 2
debug: false
pull-image-on-create: false
EOF

# To verify the installation
crictl --version
				
			
Forwarding IPv4 and letting iptables see bridged traffic
				
					cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF

sudo modprobe overlay
sudo modprobe br_netfilter

cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF

sudo sysctl --system

sysctl net.bridge.bridge-nf-call-iptables net.bridge.bridge-nf-call-ip6tables net.ipv4.ip_forward
modprobe br_netfilter
sysctl -p /etc/sysctl.conf
				
			
3. Disable Swap on all Nodes

Kubernetes requires swap to be disabled on all nodes. You must disable swap for kubelet to work properly. Edit the/etc/fstab” file and comment out the line that reference swap or using the following command.

				
					sudo swapoff -a
				
			
4. Installing kubeadm, kubelet, and kubectl​
				
					# Install the necessary packages
sudo apt-get update && sudo apt-get install -y apt-transport-https curl

# Add the kubernetes repository and GPG key
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list

sudo apt-get update
# Install the required kubernetes components
sudo apt-get install -y kubeadm kubelet kubectl

# To enable and check the status of the service
sudo systemctl enable kubelet
sudo systemctl start kubelet
sudo systemctl status kubelet
				
			
5. Initialize k8s cluster using kubeadm on Master Node

Follow the instructions provided by kubeadm to complete the cluster initialization. If you are using a Private IP use the following command. Replace $IPADDR with the actual ip address of your master node and $POD_CIDR with your chosen pod network CIDR.

				
					sudo kubeadm init --apiserver-advertise-address=$IPADDR  --apiserver-cert-extra-sans=$IPADDR  --pod-network-cidr=$POD_CIDR --node-name $NODENAME --ignore-preflight-errors Swap
				
			
6. Configure kubectl & copy the kubeconfig file

After cluster is initialized, configure kubectl and also you can typically copy the kubeconfig file generated from the output of kubeadm initialization process.

				
					mkdir -p $HOME/.kube
sudo cp /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

				
			
7. Join Worker Node to Master Node

On each worker node, run the kubeadm join command provided in the output when initializing master node. It look something like this.

				
					sudo kubeadm join your_master_ip:6443 --token your_token --discovery-token-ca-cert-hash your_hash
    
    # To create token on master node
    kubeadm token create --print-join-command


				
			
8. Install Calico CNI Plugin for Pod Networking

To allow communication between pods in your cluster, we need to install a network plugin of our choice. You can use popular choices like calico, flannel, or weave.

				
					# For Calico
kubectl apply -f https://docs.projectcalico.org/v3.18/manifests/calico.yaml
				
			
9. Verify Node Status & Test the Setup

On the master node, you can use kubectl to check the status of your cluster.

				
					kubectl get nodes
kubectl cluster-info
				
			
Conclusion

These are the steps to set up kubernetes cluster on bare metal server. Also it is depending on your specifc requirements and additional configuration may be needed. Now that your cluster is setup, you can deploy your applications using kubectl. Moreover, refer the official documentation https://kubernetes.io/docs/home/ for the most up-to-date installation instructions for your specific distribution and kubernetes version.