Friday, January 22, 2021

Set up a Kubernetes Cluster using Kubeadm


Goal: Setup a 3-Nodes Cluster with 1 master and 2 worker nodes



Pre-requisites:

  • A Minimum of 2 GM of RAM available to all VMs

  • A minimum of 2 CPUs are recommended 

  • Network connectivity between VMs

  • Ubuntu 16+


* You can still create the cluster even if you don’t meet the above CPU and memory recommendation, you have to force the Kube cluster to force ignore the pre-requisites check.


Steps (Use root account):

  1. Provision the VMs by using any solution like Vagrant, AWS, or Azure.

  2. Install br_netfilter on each VM if it’s not installed already.

*Run on all the nodes in the cluster

To check:

lsmod | grep br_netfilter

To Install: 

sudo modprobe br_netfilter

Run the following command

    cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf

br_netfilter

EOF


cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf

net.bridge.bridge-nf-call-ip6tables = 1

net.bridge.bridge-nf-call-iptables = 1

EOF

sudo sysctl --system


  1. Installing container runtime (Docker) on all the nodes/VMs.

*Run on all the nodes in the cluster


Docker Installation:

## Set up the docker repository:

sudo apt-get update && sudo apt-get install -y \

apt-transport-https ca-certificates curl 

software-properties-common gnupg2


# Add Docker's official GPG key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key --keyring /etc/apt/trusted.gpg.d/docker.gpg add -



# Add the Docker apt repository:

sudo add-apt-repository \

  "deb [arch=amd64] https://download.docker.com/linux/ubuntu \

  $(lsb_release -cs) \

  stable"


Install Docker CE

sudo apt-get update && sudo apt-get install -y \

  containerd.io=1.2.13-2 \

  docker-ce=5:19.03.11~3-0~ubuntu-$(lsb_release -cs) \

  docker-ce-cli=5:19.03.11~3-0~ubuntu-$(lsb_release -cs)


## Create /etc/docker

sudo mkdir /etc/docker


# Set up the Docker daemon

cat <<EOF | sudo tee /etc/docker/daemon.json

{

  "exec-opts": ["native.cgroupdriver=systemd"],

  "log-driver": "json-file",

  "log-opts": {

    "max-size": "100m"

  },

  "storage-driver": "overlay2"

}

EOF


# Create /etc/systemd/system/docker.service.d

sudo mkdir -p /etc/systemd/system/docker.service.d


# Restart Docker

sudo systemctl daemon-reload

sudo systemctl restart docker


# Enable docker to start at boot

sudo systemctl enable docker



  1. Install Kubeadm, Kubectl and Kubelet

*Run on all the nodes in the 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


  1. Initialize the Master Control-Plane on the Master node

*Run on the master node only


kubeadm init --pod-network-cidr 10.244.0.0/16 --apiserver-advertise-address=<ipaddressOfmasternode>


Add the following option if you don’t meet the memory & CPU requirement, it will force ignore the check.


--ignore-preflight-errors=all


You can give any cider range for the pod network, just make sure it’s not the same cider ranges as your VMs.


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



  1. 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


  1. Install the Pod Network Add-on so that nodes can communicate with each other in the Kubernetes cluster. We are using the Weavenet network add-on here.


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


  1. Run the following Command on Work Nodes only to join the Kube Master node.


Run the Kubeadm Join command that you got from Kubeadm Init command output in the

step 5.


Example only:

kubeadm join 10.0.0.11:6443 --token lkjxey.hl6v0iuhtq76s3py     --discovery-token-ca-cert-hash sha256:eedaaa9b1e1094ef29ba9c41a0db1be3a5baa27d49ac43d467dcab4583627ff3


Add the following option if your join command is failing due to not meeting the memory & CPU etc. requirement, it will force ignore the check.


--ignore-preflight-errors=all


  1. Run the following command to test if all nodes are ready in your cluster.


kubectl get nodes -o wide


  1. If you see all node status as Ready, your Kubernetes cluster is ready to be used.


Pat on your back!