How to Install Kubernetes: A Step-by-Step Guide
Launch 3 VMs
Kube-Master
Kube-Worker1
Kube-Worker2
Add Port: 0 - 65535
Allow All Traffic for Demo
Master Nodes
- 6443, 2379-2380, 10250, 10259, 10257
Worker Nodes
- 10250, 30000-32767(NodePort Range)
Run below commands in both Master and Worker nodes
sudo -i apt update -y
Set the appropriate hostname for each machine.
sudo hostnamectl set-hostname "kmaster-node" exec bash reboot sudo hostnamectl set-hostname "worker-node1" exec bash reboot sudo hostnamectl set-hostname "worker-node2" exec bash reboot
To allow kubelet to work properly, we need to disable swap on both machines.
sudo swapoff -a sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
Install Docker
sudo apt install docker.io -y
Install containerd - Is a CRI
Load the br_netfilter module required for networking.
sudo modprobe overlay sudo modprobe br_netfilter cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf overlay br_netfilter EOF
To allow iptables to see bridged traffic, as required by Kubernetes, we need to set the values of certain fields to 1.
sudo tee /etc/sysctl.d/kubernetes.conf<<EOF net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 EOF
Apply the new settings without restarting.
sudo sysctl --system
Install curl.
sudo apt install curl -y
Get the apt-key and then add the repository from which we will install containerd.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Update and then install the containerd package.
sudo apt update -y sudo apt install -y containerd.io
Set up the default configuration file.
sudo mkdir -p /etc/containerd sudo containerd config default | sudo tee /etc/containerd/config.toml
Next up, we need to modify the containerd configuration file and ensure that the cgroupDriver is set to systemd. To do so, #edit the following file:
sudo vi /etc/containerd/config.toml
Scroll down to the following section:
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
And ensure that value of SystemdCgroup is set to true Make sure the contents of your section match the following:
#[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options] # BinaryName = "" # CriuImagePath = "" # CriuPath = "" # CriuWorkPath = "" # IoGid = 0 # IoUid = 0 # NoNewKeyring = false # NoPivotRoot = false # Root = "" # ShimCgroup = "" ## SystemdCgroup = true
Finally, to apply these changes, we need to restart containerd.
sudo systemctl restart containerd sudo systemctl status containerd
Install Kubernetes
With our container runtime installed and configured, we are ready to install Kubernetes.
Add the repository key and the 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 ca-certificates curl gpg
If the directory
/etc/apt/keyrings
does not exist, it should be created before the curl command, read the note below.sudo mkdir -p -m 755 /etc/apt/keyrings mkdir /etc/apt/keyrings
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
This overwrites any existing configuration 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.29/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list sudo apt-get update sudo apt-get install -y kubelet kubeadm kubectl
Finally, enable the kubelet service on both systems so we can start it.
sudo systemctl enable kubelet
On Master Node:
Setting up the cluster
With our container runtime and Kubernetes modules installed, we are ready to initialize our Kubernetes cluster.
Run the following command on the master node to allow Kubernetes to fetch the required images before cluster initialization:
sudo kubeadm config images pull
Initialize the cluster
sudo kubeadm init --pod-network-cidr=10.244.0.0/16 sudo kubeadm init --pod-network-cidr=10.244.0.0/16 --ignore-preflight-errors=NumCPU --ignore-preflight-errors=Mem
Run below command to ignore CPU error for demo
The initialization may take a few moments to finish. Expect an output similar to the following:
Your Kubernetes control-plane has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
su - <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
Deploy a pod network to our cluster. This is required to interconnect the different Kubernetes components.
kubectl apply -f https://github.com/coreos/flannel/raw/master/Documentation/kube-flannel.yml
Preserve the join command displayed in the terminal
On Worker Nodes
Use join command in worker nodes as root user
On Master node
We can check the worker nodes connected to the master node
kubectl get nodes
We can also check that default pods are running
kubectl get pods --all-namespaces
If a join token is lost we can create a new join token
sudo kubeadm token create --print-join-command