A Blog by Shadab Shamsi discussing thoughts, ideas, tricks and facts related to Computers and Technologies.

Responsive Ads Here

Sunday, 21 November 2021

Kubernetes cluster installation using kubeadm


On Both Master and Slave Nodes

  1. Install docker following Official Docker Installation Guide

  2. Switch to root user

    sudo su -
  3. Change docker cgroup driver

    cat > /etc/docker/daemon.json <<EOF
      "exec-opts": ["native.cgroupdriver=systemd"],
      "log-driver": "json-file",
      "log-opts": {
        "max-size": "100m"
      "storage-driver": "overlay2"
    systemctl daemon-reload
    systemctl restart docker

  4. Update the apt package index and install packages needed to use the Kubernetes apt repository:

    sudo apt-get update
    sudo apt-get install -y apt-transport-https ca-certificates curl
  5. Download the Google Cloud public signing key:

    sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
  6. Add the Kubernetes apt repository:

    echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
  7. Update apt package index, 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

The kubelet is now restarting every few seconds, as it waits in a crashloop for kubeadm to tell it what to do.

Setup cgroup (On Master only)

8. Create a kubeadm config file

nano kubeadm-config.yaml

A minimal example of configuring the field explicitly:

# kubeadm-config.yaml
kind: ClusterConfiguration
apiVersion: kubeadm.k8s.io/v1beta3
kubernetesVersion: v1.21.0
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
cgroupDriver: systemd

Note: Change the kubernetesVersion to appropriate version. To find kubernetesVersion execute

kubelet --version

Start Kubernetes cluster (On Master only)

9. Above configuration file can then be passed to the kubeadm command:

kubeadm init --config kubeadm-config.yaml

Make a record of the kubeadm join command that kubeadm init outputs. You need this command to join nodes to your cluster.

If we have forgotten to save the above received kubeadm join command, then you can create a new token and use it for joining worker nodes to the cluster.

 $ kubeadm token create --print-join-command

Setup kubectl permission (On Master only)

10. To make kubectl work for your non-root user, run these commands, which are also part of the kubeadm init output:

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

Setup CNI

11. Now we need to setup Container Network. We can use any of the CNIs listed here
List of supported CNIs

However, We are going to use Weave CNI

kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"

Join Master (On Worker Node only)

12. We can now join any number of machines by running the join command we noted in step 9 on each worker node

as root:

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

No comments:

Post a Comment