Creating a Kubernetes Cluster

Creating a Kubernetes Cluster

Overview:-

In this Blog, we will create a Kubernetes Cluster.

Creating Master and Worker nodes:-

  • At first, we will need 2 nodes(Master and Worker nodes) and connect both with our system.

  • I will use AWS EC2 to create Master and Worker nodes and connect them with our system using SSH.

  • Sign into your AWS Console and Search for EC2.

  • Click on EC2 and Click on Launch Instance which is in the Orange box.

  • Name it I will name it Kubernetes-Master-Node and from the Application and OS Images(Amazon Machine Image) select Ubuntu.

  • Coming down from the Instance type select t2.medium as Kubernetes Cluster requires some more Inputs.

  • Now in the Key pair(login) choose any Key. I have already one I will select it.

  • Now in the Network settings choose all three SSH, HTTPs and HTTP traffic.

  • From the right side write 2 in the Number of instances as we need Master and Worker nodes and then click on Launch instance.

  • Now our 2 instances will be launched with the same name so we have to rename one instance as Kubernetes-Worker-Node.

  • We will Connect both the Servers with our system using SSH. So open two different terminals in your system.

  • I will show one server process the same for the other one.

  • Check any one Instance and click on Connect from the top right side.

  • After clicking on Connect we will see a box like this:

  • Now in the SSH client section copy the line in the Example.

  • Open your terminal and navigate to the path where your key pair is present. In most cases, it's in the Downloads.

Run the Command:

cd Downloads/
  • After Navigating to Downloads paste your Example line with sudo at the beginning. It should look like this:

  • This will connect your Server with the System Using SSH.

  • Do the same for another server.

Set up Kubernetes cluster using Kubeadm:-

  • We have both the Master and Worker nodes connected to our system using SSH.

  • Run these commands together in both the nodes:

      sudo apt update
      sudo apt-get install -y apt-transport-https ca-certificates curl
      sudo apt install docker.io -y
    
      sudo systemctl enable --now docker 
    
      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
      echo 'deb https://packages.cloud.google.com/apt kubernetes-xenial main' | sudo tee /etc/apt/sources.list.d/kubernetes.list
    
      sudo apt update 
      sudo apt install kubeadm=1.20.0-00 kubectl=1.20.0-00 kubelet=1.20.0-00 -y
    

We have to run these commands in both the nodes Master and Worker.

Initializing Kubernetes Master node(Master Server):-

  • Now coming in the Master node we will set some things.

  • At first, run the command:

      sudo kubeadm init
    

  • Then to set local KubeConfig run the commands:

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

  • To install CNI run the command:

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

  • We will generate a token for our worker node to get connected with the master node for this run the command:

      sudo kubeadm token create --print-join-command
    

  • This command will give us a token for connection.

Make sure to Expose PORT 6443 in the Security group in the Master node.

Connecting Worker Node with Master node:-

  • Now we have to connect the Worker node with the master node.

  • Af first runs this command in the Worker node:

sudo kubeadm reset pre-flight checks

  • Now the token that we got before pasted it with --v=5 at the end. This will connect both the nodes and our Kubernetes Cluster will be completed.

  • For verification in the Master node run the command:

      kubectl get nodes
    

Thanks.