- 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) andkubeadm 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
2. kubectl
- Purpose: Its 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.
- 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.
- Starting kubelet service
- Starting kubelet service
Typically, Kubelet is started as a service on each node via systemd.
sudo systemctl start kubelet
- Stopping kubelet
- Stopping kubelet
sudo systemctl stop kubelet
- Checking kubelet status
- Checking kubelet status
sudo systemctl status kubelet
- Restarting kubelet
- Restarting kubelet
sudo systemctl restart kubelet
- Specifying the kubernetes API server
- Specifying the kubernetes API server
kubelet --api-servers=http://<api-server-url>:8080
- Configuring the pod manifest path
- Configuring the pod manifest path
kubelet --pod-manifest-path=/etc/kubernetes/manifests
- Specifying the Container Runtime (e.g., Docker, containerd, CRI-O)
- 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.
- 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
- Setting the Node IP address
kubelet --node-ip=<ip-address>
- Enable debugging via server (start kubelet with a debugging endpoint)
- Enable debugging via server (start kubelet with a debugging endpoint)
kubelet --healthz-port=10248 --read-only-port=10255
- Check the logs generated by Kubelet
- 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.
Step2: Install docker, kubeadm, kubelet, and kubectl on all nodes of cluster.
sudo apt install docker.io -y
sudo systemctl enable docker
sudo systemctl start docker
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
kubectl apply -f calico.yaml
#Verify installation
kubectl get pods -n kube-system
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
You can get <Worker-Node-IP> with command 'kubectl get nodes -o wide' and <Service-Port> with command 'kubectl get services' on 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.