Kubeadm, Kubelet, Kubectl

kubeadmkubectl, and kubelet are core components in the Kubernetes ecosystem, but they serve different purposes. They all work together to create and manage a kubernetes cluster efficiently. Here's a breakdown of their roles,

1. kubeadm
  • Purpose It is a tool used for setting up and configuring kubernetes clusters.
  • Function
    • Helps bootstrap the control plane (master node components like the API server, controller manager, scheduler) and worker nodes in a cluster.
    • Primarily used for setting up and joining nodes to a Kubernetes cluster.
    • Provides commands like kubeadm init (to set up a master node) and kubeadm join (to add worker nodes to the cluster).
    • It doesn’t manage the cluster once it’s set up, but it helps with initial configuration and installation.
  • Pros
    • Simplicity: It streamlines the process of setting up a cluster without requiring deep expertise.
    • Production-ready: Used widely in production environments.
    • Customizable: You can customize various components and networking solutions.
  • When to use
    • Setting up a small to medium-sized kubernetes cluster
    • As part of a CI/CD pipeline: Can be used to automatically bootstrap and tear down clusters for testing.
  • Commands
    • Initialize master node (This command sets up the control plane, API server, etcd, and other components on the master node)
      kubeadm init
    • Join worker nodes to cluster
      kubeadm join <master-ip>:<port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>
    • Create/Recreate a token
      kubeadm token create

      Creates a token valid for 2 hours.

      kubeadm token create --ttl 2h 

      List existing tokens that can be used for nodes to join the cluster

      kubeadm token list
    • Reset a node (master or worker)
      This command is used to reset all Kubernetes components on a node. It will remove all configurations, certificates, and components set up by kubeadm init or kubeadm join

      kubeadm reset

    • Displays the available versions of Kubernetes to upgrade
      kubeadm upgrade plan
    • To perform the upgrade
      kubeadm upgrade apply <version>
    • Shows the current configuration of the cluster, including API server and networking settings
      kubeadm config view
    • Download required container images for kubernetes components (API server, etcd, controller-manager, etc.) based on the Kubernetes version
      kubeadm config images pull
    • Renews all certificates generated by kubeadm
      kubeadm alpha certs renew all
    • Generates a default configuration file that you can modify to customize your cluster initialization
      kubeadm config print init-defaults
    • Inspect cluster cealth

      kubeadm cluster-info 

 

👉 It's important to note that kubeadm is only one part of the cluster setup, focusing on bootstrapping. It doesn’t provide a solution for setting up a fully managed Kubernetes service, but it integrates well with other tools like kubectl and kubelet to form a complete Kubernetes environment. 

2. kubectl

  • PurposeIts a command line tool to interact with cluster and allow you to manage resources such as pods, services, deployments.
  • Function

    • It communicates with the kubernetes API server (API server is a service in master node) and sends instructions to perform actions like creating or deleting resources.
    • Supports various operations, including creating and managing Pods, services, and deployments.
    • You use commands like kubectl apply, kubectl get pods, kubectl logs, etc., to manage Kubernetes resources.

  • Commands
    • Check cluster info
      kubectl cluster-info
    • Get a list of all nodes
      kubectl get nodes
    • List all pods in the default namespace
      kubectl get pods
    • Get detailed information about a pod, If something goes wrong
      kubectl describe pod <pod-name>
    • View logs from a pod
      kubectl logs <pod-name>
    • Stream logs from a pod
      kubectl logs -f <pod-name>
    • Run a shell in a running pod
      kubectl exec -it <pod-name> -- /bin/bash
    • Delete a pod
      kubectl delete pod <pod-name>
    • View deployments
      kubectl get deployments
    • Scale a deployment (e.g., to 5 replicas)
      kubectl scale deployment <deployment-name> --replicas=5
    • Apply configuration from a file
      kubectl apply -f <config-file.yaml>
    • List all namespaces
      kubectl get namespaces
    • Create a namespace
      kubectl create namespace <namespace-name>
    • Set the default namespace
      kubectl config set-context --current --namespace=<namespace-name>
    • Deploy an application using a YAML file
      kubectl apply -f deployment.yaml
    • Check the status of the deployment
      kubectl get deployments
    • Delete the deployment
      kubectl delete deployment <deployment-name> 


3. kubelet 

  • Purpose: kubelet is the node-agent (a service) that runs on every node in the Kubernetes cluster, including the master and worker nodes. kubelet constantly communicates with the Kubernetes API server to receive tasks (pod specifications) and ensures that the containers described in those pod specs are running on the node.
  • Function
    • Ensures that containers described by the Kubernetes API (in the form of Pods) are running properly.
    • Communicates with the Kubernetes master to receive commands and execute them.
    • It monitors the state of containers and reports back to the Kubernetes control plane (master node).
    • Manages the lifecycle of containers by pulling images, starting, stopping, and restarting containers as needed.
  • Commands: Kubelet is primarily managed through configuration files, but it also supports a number of command-line options and subcommands that are useful for debugging or administrative tasks.

    • Starting kubelet service

              Typically, Kubelet is started as a service on each node via systemd.

      sudo systemctl start kubelet

    • Stopping kubelet

               sudo systemctl stop kubelet

    • Checking kubelet status

      sudo systemctl status kubelet

    • Restarting kubelet

      sudo systemctl restart kubelet

    • Specifying the kubernetes API server

      kubelet --api-servers=http://<api-server-url>:8080

    • Configuring the pod manifest path

      kubelet --pod-manifest-path=/etc/kubernetes/manifests

    • Specifying the Container Runtime (e.g., Docker, containerd, CRI-O)

      kubelet --container-runtime=docker

    • Specifying a Kubeconfig file: The kubeconfig file is used by Kubelet to authenticate with the Kubernetes API server.

      kubelet --kubeconfig=/etc/kubernetes/kubelet.conf

    • Setting the Node IP address

      kubelet --node-ip=<ip-address>

    • Enable debugging via server (start kubelet with a debugging endpoint)

      kubelet --healthz-port=10248 --read-only-port=10255

    • Check the logs generated by Kubelet

      journalctl -u kubelet


Summary

  • kubeadm: A tool for setting up and configuring kubernetes clusters.
  • kubectl: A CLI tool for interacting with the Kubernetes API server for deploying and managing applications on the cluster.
  • kubelet: An agent that runs on nodes to manage containerized applications.

They all work together to create and manage a Kubernetes cluster efficiently.



Example:  Setting up a multinode kubernetes cluster using kubeadm, kubelet, and kubectl. We will than deploy a simple NGNIX service into it.

Step1: We setup two nodes kubernetes cluster (Two AWS EC2 instances one for master node and one for worker node)


Note: Allow port '6443' traffic for EC2 nodes by adding it into security group. it is the port of master node where worker nodes will send request to join the cluster 

Step2: Install docker, kubeadm, kubelet, and kubectl on all nodes of cluster.

sudo apt update -y
sudo apt upgrade -y

sudo apt install docker.io -y
sudo systemctl enable docker
sudo systemctl start docker
docker -v

sudo apt-get update -y
sudo apt-get install -y apt-transport-https ca-certificates curl gpg

curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.31/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.31/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list

sudo apt-get update -y
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

sudo systemctl enable --now kubelet


Step3: Initialize the master node using 'kubeadm init' command

sudo kubeadm init --pod-network-cidr=192.168.0.0/16

The --pod-network-cidr flag is used to specify the CIDR for the Pod network. This is required by certain networking solutions like Weave and Calico.

Save output of this command. It will have a join command for worker node to join the cluster.



Step4: Configure kubectl on the master node

Note: After the 'kubeadm init' completes, follow the instructions printed in the terminal to set up kubectl on your master node for cluster administration

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

You should now deploy a pod network to the cluster (for network communication between pods)

#Download calico.yaml
curl https://projectcalico.docs.tigera.io/v3.19/manifests/calico.yaml -O

#I've updated calico.yaml file after downloading and changed variable "apiVersion: policy/v1beta1" to "apiVersion: policy/v1" because I was facing an issue while running following apply command.

#Apply configuration
kubectl apply -f calico.yaml
#Verify installation
kubectl get pods -n kube-system

You should see pods related to Calico, such as calico-kube-controllers and calico-node, running.

Note: At this point, the master node should be up and running. The kubelet service will now manage containers on this node, but there are no worker nodes yet.


Step5: Join the worker node to the cluster

This command was generated for 'kubeadm init' command which we run on the master node. 

You may need to install 'socat' package using 'apt-get -y install socat' before running following command.

sudo kubeadm join <MASTER_IP>:6443 --token <TOKEN> --discovery-token-ca-cert-hash sha256:<HASH>

The kubelet service on the worker node will now run and communicate with the master node. The worker node will also be ready to run containers.


Step6: Manage the cluster with kubectl command on master node

Check the Status of Nodes

kubectl get nodes

Deploy an example NGINX application

kubectl create deployment nginx --image=nginx

Check the status of the deployment

kubectl get deployments

Expose the deployment as a service

To make the NGINX deployment accessible via a NodePort service

kubectl expose deployment nginx --type=NodePort --port=80

Get the service details

kubectl get services

You can get <Worker-Node-IP> with command 'kubectl get nodes -o wide' and <Service-Port> with command 'kubectl get serviceson master node

You can now access the deployed NGINX service with a curl command on worker node

curl http://<Worker-Node-IP>:<Service-Port>


Summary of the example

  • kubeadm was used to initialize the cluster (master node) and join worker nodes.
  • kubectl was used to check node status, deploy an application, and manage the cluster.
  • kubelet was running in the background on each node (master and worker) to manage container runtimes and ensure pods are running.

This workflow demonstrates how kubeadm, kubelet, and kubectl work together to set up and manage a Kubernetes cluster.