Setting up kubernetes using Kubeadm

Setting up kubernetes using Kubeadm

·

3 min read

Pre-requisites:

  1. t2.medium AWS instance for master node

  2. t2.micro AWS instance for worker node

  3. Docker

Installation steps:

Follow the below steps on both the master and worker node

All the commands to be executed in super user mode(with root privileges)

Docker installation

  1. We will use docker to containerize the application, so let's first download and install docker on both the master and worker node. To do this login as root user

     sudo su
     apt update -y
     apt-get install docker.io -y
     systemctl start docker
     systemctl enable docker
    

Downloading the required Kubernetes configuration

Follow the below steps on both master and worker nodes with root user privilege

  1. Download the Google Cloud public signing key:

     curl -fsSL "https://packages.cloud.google.com/apt/doc/apt-key.gpg" | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/kubernetes-archive-keyring.gpg
    
  2. Add the Kubernetes apt repository:

     echo 'deb https://packages.cloud.google.com/apt kubernetes-xenial main' > /etc/apt/sources.list.d/kubernetes.list
    
  3. Update apt package index, install kubelet, kubeadm and kubectl, and pin their version:

     apt update -y
     apt install kubeadm=1.20.0-00 kubectl=1.20.0-00 kubelet=1.20.0-00 -y
    

Setting up Kubernetes configuration

Master node configuration

Execute below commands on only the master node with root user privileges

  1. Initialise kubeadm

     kubeadm init
    
  2. Create .kube directory in the user's home path, which is needed to store Kubernetes configuration

     mkdir -p $HOME/.kube
    
  3. Copy the Kubernetes cluster configuration file admin.conf to the .kube directory. The sudo prefix grants the necessary administrative privileges to access the /etc/kubernetes/ directory.

     sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    
  4. Change the ownership of the copied config file to the current user. This ensures the user has permission to access and modify the Kubernetes configuration.

     sudo chown $(id -u):$(id -g) $HOME/.kube/config
    

Setting upWeave Network CNI

This step also should be done on the master

  1. Use kubectl to apply the Weave Network CNI configuration from the specified URL. The Weave Network CNI enables efficient communication between containers running on different nodes within the Kubernetes cluster.

     kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml
    

Generating token

This step also should be done on master with root user privileges

  1. Generate the token that we will use to join a worker node

     kubeadm token create --print-join-command
    

    Copy the output of this command as we will be using this command to create a cluster

Creating worker node

This step also should be done on worker

  1. Log in as the root user and pass the below commands

      sudo su
      kubeadm reset pre-flight checks
    
  2. Open port no 6443 in the master's security group

  3. Paste the join command on the worker node and append --v=5 at end

     kubeadm join 172.31.12.68:6443 --token 8v4u6j.zc7tuyp6gnr1wdqe     --discovery-token-ca-cert-hash sha256:c81d4fea2a6d61261723f45dd217b318101f86f2428b2c29572a666290a148ae --v=5
    

AND WE ARE JUST ONE STEP AWAY FROM COMPLETING THE SETUP

Now when you type kubectl get nodes in master, you can see that the Kubeadm setup is complete

Creating nginx pod:

Execute the below command in the master node to create an nginx pod

kubectl run nginx --image=nginx --restart=Never