Setting Up Kubernetes Cluster from Scratch

Setting Up Kubernetes Cluster from Scratch

What is Kubernetes?

Kubernetes, often abbreviated as K8s, is an open-source container orchestration platform designed to automate the deployment, scaling, and management of containerized applications. It provides a robust and scalable framework for managing containerized workloads and services.

Kubernetes Architecture

Control Plane Components:

  1. etcd: A distributed key-value store that stores the configuration data of the cluster, representing the overall state of the system.

  2. API Server: Acts as the front-end for the Kubernetes control plane. It exposes the Kubernetes API, which is used to manage the cluster.

  3. Controller Manager: Watches for changes in the cluster state and makes updates to bring the actual state closer to the desired state.

  4. Scheduler: Assigns a node to newly created pods based on resource requirements, policies, and other constraints.
    Certainly! Let's provide an introduction to Kubernetes, including its architecture, components, and prerequisites, before delving into the installation commands.

    Node Components:

    1. Kubelet: Ensures that containers are running in a Pod on the node. It communicates with the API server and manages the containers on the node.

    2. Kube Proxy: Maintains network rules on nodes, allowing communication to and from containerized applications within a pod.

    3. Container Runtime: The software responsible for running containers, such as Docker or containerd.

      Components of Kubernetes

Prerequisites for Setting up a Kubernetes Cluster

Before setting up a Kubernetes cluster, ensure you have the following prerequisites:

  1. Linux Nodes: The machines where Kubernetes will be installed should be running a compatible Linux distribution.

  2. Container Runtime: Choose a container runtime such as Docker or containerd to run and manage containers.

  3. Network Connectivity: Ensure network connectivity between cluster nodes for communication.

  4. Kernel Modules: Load necessary kernel modules like overlay and br_netfilter for container networking.

  5. Package Manager: A package manager (e.g., apt for Ubuntu) to install required dependencies.

    Now, let's proceed with the step-by-step commands to set up a Kubernetes cluster.

Commands on Master Node:

  1. Kernel Modules and System Configuration:

     cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
     overlay
     br_netfilter
     EOF
    
     sudo modprobe overlay
     sudo modprobe br_netfilter
     sudo sysctl --system
    
  2. Docker Installation and Configuration:

     curl -fsSL https://get.docker.com -o get-docker.sh
     sudo sh get-docker.sh 
     sudo systemctl restart docker
     sudo systemctl enable docker
     sudo docker ps
    
  3. Containerd Configuration:

     sudo vi /etc/containerd/config.toml 
     ### enable_plugins[containerd]
     sudo systemctl restart docker
     sudo systemctl restart containerd
    
  4. Kubernetes Components Installation:

     sudo apt-get update
     sudo apt-get install -y apt-transport-https ca-certificates curl
    
     curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-archive-keyring.gpg
    
     echo "deb [signed-by=/etc/apt/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
    
     sudo apt-get update
     sudo apt-get install -y kubelet kubeadm kubectl
    
  5. Kubernetes Initialization:

     sudo kubeadm init 
     # this will print
    
  6. kubectl Configuration:

     mkdir -p $HOME/.kube
     sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
     sudo chown $(id -u):$(id -g) $HOME/.kube/config
    
  7. To Print Join Cammand for Worker Node

     sudo kubeadm token create --print-join-command
    

Commands on Worker Node:

  1. On the worker node, execute all master node commands up to the 4th step of Kubernetes Components Installation. After that, there is no need to run 'kubeadm init.' Proceed directly to the next step by running the join command provided by the following command:

    sudo kubeadm token create --print-join-command.

  2. Join the Worker Node to the Cluster:

    Use the kubeadm join command you obtained from the master node:

     sudo kubeadm join <MASTER_IP>:<MASTER_PORT> --token <TOKEN> --discovery-token-ca-cert-hash <HASH>
     # cammand lookslike this
    

    On the Master Node (After Worker Node Join):

    • After the worker node has successfully joined the cluster, you may need to apply a network overlay for pod communication. Common choices include Calico, Flannel, or Weave.

    • After running 'kubectl get nodes,' if the nodes in the cluster are not ready, you need to apply a network overlay. Execute the following command to add the WeaveNet overlay

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

      After applying the network overlay, check the status of the nodes again using 'kubectl get nodes.' The status should now be 'Ready.'"

Remember to follow these steps sequentially, and your Kubernetes cluster should be up and running with the master and worker nodes properly configured.

You did it! Your Kubernetes cluster is up and running. From configuring the master to adding worker nodes, you've nailed the basics Cheers to smooth orchestrations and thriving clusters. Happy container orchestrating!

💡
My Kubernetes Cluster :-

Thank you for reading the Blog here.