Deploy kubernetes cluster with kubeadm

Deploying a Kubernetes cluster with kubeadm involves a series of steps to configure both the control plane and worker node. Here’s a step-by-step guide to help you set up your cluster:

Prerequisites
  • Two Instances: You need two instances (VMs or physical machines) running a compatible version of Linux.
  • Network Connectivity: Both instances should be able to communicate with each other.
  • Root Access: Ensure you have root access or sudo privileges on both instances.
  • Disable Swap: Kubernetes requires that swap be disabled on both instances.
Step 1: Set Up Your Environment
  • Disable Swap:
  • sudo swapoff -a
    sudo sed -i '/ swap / s/^/#/' /etc/fstab
  • Update Packages:
  • sudo apt-get update
  • Install Docker:
  • sudo apt-get install -y docker.io
    sudo systemctl enable docker
    sudo systemctl start docker
  • Install kubeadm, kubelet, and kubectl:
  • sudo apt-get update && sudo apt-get install -y apt-transport-https ca-certificates curl
    sudo curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
    sudo add-apt-repository "deb https://apt.kubernetes.io/ kubernetes-xenial main"
    sudo apt-get update
    sudo apt-get install -y kubelet kubeadm kubectl
    sudo apt-mark hold kubelet kubeadm kubectl
Step 2: Initialize the Control Plane
  • On the Control Plane Node:
  • sudo kubeadm init --pod-network-cidr=192.168.0.0/16
  • Set Up kubectl for Your User:
  • mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config
  • Install a Pod Network Add-On:
  • For example, using Calico:

    kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
Step 3: Join the Worker Node to the Cluster
  • On the Control Plane Node, Get the Join Command:
  • kubeadm token create --print-join-command
  • On the Worker Node, Run the Join Command:
  • Use the command you obtained from the control plane node. It will look something like this:

    sudo kubeadm join "control-plane-ip":6443 --token "token" --discovery-token-ca-cert-hash sha256:"hash"
Step 4: Verify the Cluster
  • On the Control Plane Node:
  • kubectl get nodes

    You should see both the control plane node and the worker node listed.

Step 5: Deploy an Application to Verify the Cluster
  • Deploy a Simple Application:
  • kubectl create deployment nginx --image=nginx
  • Expose the Deployment:
  • kubectl expose deployment nginx --port=80 --type=NodePort
  • Verify the Application is Running:
  • kubectl get pods
    kubectl get svc

This should give you a basic working Kubernetes cluster with one control plane node and one worker node. You can now scale your cluster by adding more worker nodes as needed using the kubeadm join command.