Install kubernetes on Ubuntu Step by Step

In this article, we are going to deploy Kubernetes Cluster on 2 virtual machines. One is used for the Master node and the other is the Work node.

Prerequisites

  • 2 Ubuntu 20.04 LTS Virtual Machines with each at least 2 GB RAM, 2 CPUs.

Install Steps

Step1 ) Enable IPv4 packet forwording for each VMs.

  • Modify net.ipv4.ip_forward
# sysctl params required by setup, params persist across reboots
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.ipv4.ip_forward = 1
EOF

# Apply sysctl params without reboot
sudo sysctl --system
  • Verify that net.ipv4.ip_forward is set to 1 with:
sysctl net.ipv4.ip_forward

Reference link

Step2 ) Install containerd service for each VMs.

Here we choose containerd as the underlying container runtime.

  • Add docker repository to APT sources:
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
  • Install service
    sudo apt-get install containerd.io

Reference link

Step3 ) Set cgroup for containerd service for each VMs.
On Linux, control groups are used to constrain resources that are allocated to processes.

  • Edit service config file /etc/containerd/config.toml, remove all the contents in it and past the following config to it.
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
SystemdCgroup = true
  • Save /etc/containerd/config.toml
  • Apply the changes by restarting service
    sudo systemctl restart containerd

Reference link

Step4 ) Install kubeadm, kubelet and kubectl for each VMs.

  • Install related packages and add Kubernets APT repositories
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
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.30/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.30/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
  • Install kubelet, kubeadm and kubectl, and pin their version:
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

Step5 ) Initial Kubernets on Master node

  • Initial Master node by specifying pod networ CIDR
kubeadm init --pod-network-cidr=10.128.0.0/16
  • Output will be link below, follow the steps showing there.

Reference link

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

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

You should now deploy a Pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
/docs/concepts/cluster-administration/addons/

You can now join any number of machines by running the following on each node
as root:

kubeadm join <control-plane-host>:<control-plane-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>

Step6 ) Join the Work node to Master node
The previous steps show the joining command in the output. Run the command on Work node.

kubeadm join <control-plane-host>:<control-plane-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>

We can also geneate one by

kubeadm token create --print-join-command

Step7 ) Deploy network plugin
You must deploy a Container Network Interface (CNI) based Pod network add-on so that your Pods can communicate with each other. Cluster DNS (CoreDNS) will not start up before a network is installed.

We choose weave as our network add-on, run the following command on Master node to deploy it.

kubectl apply -f https://reweave.azurewebsites.net/k8s/v1.29/net.yaml

Replace v1.29 with the version on Kubernetes on your cluster.

Reference link

Step8 ) Testing

  • Let’s deploy a test pod by the command
    kubectl run nginx --image=nginx
  • Check the pod status we created
    kubectl get all -A

End

Welcome to leave a comment if you encounter any issue while going through the steps.

Leave a Comment