K8s cluster setup
INTRODUCTION
In this blog we will see how to setup a Kubernetes cluster for V-1.28 with Kubeadm as agent. This blog will explain detail setup of k8's cluster with step-by-step process. we know that from V-1.24 docker as container runtime has been depreciated, so we have to follow as per Kubernetes official documentation to install container run time.
STEP-1 : Create virtual machines and login into those ( ex - AWS EC2)
Here, first create virtual machines, for example if you are into any public cloud like AWS, launch multiple instances as per your requirement and login to those machines. keep one of the machine as master and others as nodes.
Command to login through SSH
ssh -i "example.pem"
ubuntu@ip.compute.amazonaws.com
STEP-2: Update packages
Once we login to our virtual machines, we have to update packages which will be a good practice.
sudo apt update -y
Now, we have to login as root user for sudo privileges, simultaneously disable firewall and swap
On both master and node
Login as root
user
sudo su -
Perform all the commands as root user unless otherwise specified
Disable Firewall
ufw disable
Disable swap
swapoff -a; sed -i '/swap/d' /etc/fstab
STEP-3: Install packages
Before setting up our K8's cluster we have install all the dependencies like packages and configs to enable our cluster properly. so run all these commands in all machines including master and nodes.
3.1 - Create a configuration file for Containerd
We’ll begin by creating a configuration file for Containerd, a container runtime that Kubernetes uses. This file defines the modules required by Containerd.
cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF
3.2 - Load the modules for Networking
Next, we load the required kernel modules for Overlay and Bridge networking.
modprobe overlay
modprobe br_netfilter
3.3 - Set System Configurations for Kubernetes Networking
We set some sysctl configurations to ensure proper networking for Kubernetes.
cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF
Apply the new settings:
sysctl --system
3.4 Install Containerd
Lets now install containerd for V-1.28
#Install dependencies
apt install -y curl gnupg software-properties-common apt-transport-https ca-certificates
#Enable docker repo
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/docker.gpg
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
#install containerd
apt-get update && sudo apt-get install -y containerd.io
3.5 Create the Default Configuration File for Containerd
We configure containerd so that it starts using systemd as cgroup
containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1
sudo sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml
3.6 Restart and Enable Containerd and check status
Now, we will restart and enable containerd
systemctl restart containerd
systemctl enable containerd
systemctl status containerd
3.7 Update Dependency Packages
Update the apt
package index and install packages needed to use the Kubernetes apt
repository:
sudo apt-get update
# apt-transport-https may be a dummy package; if so, you can skip that package
sudo apt-get install -y apt-transport-https curl gpg
3.8 Download the public signing key for the Kubernetes package repositories
The same signing key is used for all repositories so you can disregard the version in the URL
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
3.9 Add the appropriate Kubernetes apt repo
# This overwrites any existing configuratiKubernetes apt repositoryon in /etc/apt/sources.list.d/kubernetes.list
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.28/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
3.10 Update the apt
package index, install kubelet, kubeadm and kubectl, and pin their version
sudo apt-get update
FYI - In releases older than Debian 12 and Ubuntu 22.04,/etc/apt/keyrings
does not exist by default; you can create it by runningsudo mkdir -m 755 /etc/apt/keyrings
3.11 Install K8's packages and turnoff automatic updates
sudo apt-get install -y kubelet=1.28.0-00 kubeadm=1.28.0-00 kubectl=1.28.0-00
sudo apt-mark hold kubelet kubeadm kubectl
STEP-4 Initialize the Cluster
Now that we have all the prerequisites in place, it’s time to initialize the Kubernetes cluster on the control plane node using kubeadm
.
FYI - Run this commands only on master machine
kubeadm init --apiserver-advertise-address=master@ip --pod-network-cidr=192.168.0.0/16 --ignore-preflight-errors=all
4.1 Deploy Calico network
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
4.2 Set kubectl Access
To interact with the cluster, we need to configure kubectl
. Create the .kube
directory, copy the admin configuration, and adjust permissions.
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
4.3 Run cluster join command
kubeadm token create --print-join-command
4.4 Join the nodes machines into cluster
After running the token create command, now copy the token or output of the above command and run it in the node machines
Example token looks like this,
kubeadm join 172.31.2.76:6443 --token 248gw3.4ok7mzcwuzib9n3r
--discovery-token-ca-cert-hash sha256:0c94a1c49d7fa5884810b2367f0d3f48063f8170f91643690a883ce0d07d1a0d
STEP-5 Verifying the cluster (On master)
kubectl get nodes
Get component status
kubectl get cs