Setup Kubernetes Cluster on Ubuntu 20.04 using kubeadm

Kubeadm is a tool built to provide best-practice “fast paths” for creating Kubernetes clusters. It performs the actions necessary to get a minimum viable, secure cluster up and running in a user friendly way.

Kubernetes is an open-source container-orchestration system for automating computer application deployment, scaling, and management.

Before starting, ensure your software is up to date

sudo apt apdate
sudo apt upgrade

Install

Install the following packages on all of your machines:

  • kubeadm: the command to bootstrap the cluster.
  • kubelet: the component that runs on all of the machines in your cluster and does things like starting PODs and containers.
  • kubectl: the command line util to talk to your cluster.
sudo apt-get update && sudo apt-get install -y apt-transport-https curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

Confirm installed versions

kubectl version --client

kubeadm version

Output:

vagrant@ub-kubeadm:~$ kubectl version --client
Client Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.0", GitCommit:"c2b5237ccd9c0f1d600d3072634ca66cefdf272f", GitTreeState:"clean", BuildDate:"2021-08-04T18:03:20Z", GoVersion:"go1.16.6", Compiler:"gc", Platform:"linux/amd64"}

vagrant@ub-kubeadm:~$ kubeadm version
kubeadm version: &version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.0", GitCommit:"c2b5237ccd9c0f1d600d3072634ca66cefdf272f", GitTreeState:"clean", BuildDate:"2021-08-04T18:02:08Z", GoVersion:"go1.16.6", Compiler:"gc", Platform:"linux/amd64"}

Turn off swap.

sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
sudo swapoff -a

Configure sysctl.

sudo modprobe overlay
sudo modprobe br_netfilter

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

sudo sysctl --system

Install Containerd Container runtime

sudo apt-get install -y containerd

Configure containerd and start service

sudo mkdir -p /etc/containerd
sudo containerd config default > /etc/containerd/config.toml

Enable the service to start on boot and start

sudo systemctl enable --now containerd

# check status
sudo systemctl status containerd

Service should be running

containerd.service - containerd container runtime
     Loaded: loaded (/lib/systemd/system/containerd.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2021-08-04 20:27:51 UTC; 7min ago
       Docs: https://containerd.io
   Main PID: 8159 (containerd)
      Tasks: 12
     Memory: 23.3M
     CGroup: /system.slice/containerd.service
             &#x2514;&#x2500;8159 /usr/bin/containerd

Aug 04 20:27:51 ub-kubeadm containerd[8159]: time="2021-08-04T20:27:51.784407330Z" level=info msg="loading plugin \"io.containerd.grpc>
Aug 04 20:27:51 ub-kubeadm containerd[8159]: time="2021-08-04T20:27:51.785605392Z" level=info msg=serving... address=/run/containerd/c>
Aug 04 20:27:51 ub-kubeadm containerd[8159]: time="2021-08-04T20:27:51.785639559Z" level=info msg=serving... address=/run/containerd/c>
Aug 04 20:27:51 ub-kubeadm containerd[8159]: time="2021-08-04T20:27:51.787585658Z" level=info msg="containerd successfully booted in 0>
Aug 04 20:27:51 ub-kubeadm containerd[8159]: time="2021-08-04T20:27:51.801146245Z" level=info msg="Start subscribing containerd event"
Aug 04 20:27:51 ub-kubeadm containerd[8159]: time="2021-08-04T20:27:51.804843058Z" level=info msg="Start recovering state"
Aug 04 20:27:51 ub-kubeadm containerd[8159]: time="2021-08-04T20:27:51.805094249Z" level=info msg="Start event monitor"
Aug 04 20:27:51 ub-kubeadm containerd[8159]: time="2021-08-04T20:27:51.805193238Z" level=info msg="Start snapshots syncer"
Aug 04 20:27:51 ub-kubeadm containerd[8159]: time="2021-08-04T20:27:51.805282655Z" level=info msg="Start cni network conf syncer"
Aug 04 20:27:51 ub-kubeadm containerd[8159]: time="2021-08-04T20:27:51.805369362Z" level=info msg="Start streaming server"

Initialize master node

Enable and start kubelet service.

sudo systemctl enable --now kubelet

Pull Required container images:

sudo kubeadm config images pull

Set cluster endpoint DNS name or add record to /etc/hosts file.

192.168.20.7 k8s.citizix.local

Create cluster:

sudo kubeadm init \
  --pod-network-cidr=192.168.0.0/16 \
  --control-plane-endpoint=k8s.citizix.local

When that is done, the k8s should be up and running. Obtain the kubeconfig admin file from this path /etc/kubernetes/admin.conf

mkdir ~/.kube
sudo cp /etc/kubernetes/admin.conf ~/.kube/admin.conf
sudo chown ${USER}.${GROUP} ~/.kube/admin.conf
chmod 400 ~/.kube/admin.conf

Check cluster status:

kubectl cluster-info

Confirm master node is ready:

kubectl get nodes -o wide

Additional Master nodes can be added using the command in installation output:

kubeadm join k8s-cluster.computingforgeeks.com:6443 --token sr4l2l.2kvot0pfalh5o4ik \
    --discovery-token-ca-cert-hash sha256:c692fb047e15883b575bd6710779dc2c5af8073f7cab460abd181fd3ddb29a18 \
    --control-plane

Add worker nodes

With the control plane ready you can add worker nodes to the cluster for running scheduled workloads.

If endpoint address is not in DNS, add record to /etc/hosts.

$ sudo vim /etc/hosts
192.168.20.7 k8s.citizix.local

The join command that was given is used to add a worker node to the cluster.

kubeadm join k8s-cluster.computingforgeeks.com:6443 \
  --token sr4l2l.2kvot0pfalh5o4ik \
  --discovery-token-ca-cert-hash sha256:c692fb047e15883b575bd6710779dc2c5af8073f7cab460abd181fd3ddb29a18

Run below command on the control-plane to see if the node joined the cluster.

kubectl get nodes

Deploy application on cluster

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy