The Magic of Creating a Pod in Kubernetes: What Really Happens?
Demystifying Pod Creation in Kubernetes
In Kubernetes, pods represent the smallest deployable units. Creating a pod sets off a series of background processes. We are going to learn what really happends under the hood during the pod creation.
When a user initiates the creation of an object in Kubernetes, several steps occur behind the scenes to ensure the object is created and managed correctly. In this case, we will examine the process of creating a pod.
Kubernetes Pods
Kubernetes Pods are the fundamental building blocks of applications running on a Kubernetes cluster. They are the smallest and most basic deployable units within the Kubernetes object model. A Pod can host one or more containers, which work together as a cohesive unit.
Containers: Each Pod encapsulates one or more containers that run the application code. Containers within a Pod share resources and dependencies, allowing them to operate together seamlessly.
Shared Storage: Pods enable containers to share storage volumes, providing a common file system for data exchange and storage.
Networking: In Kubernetes, each Pod is assigned a unique IP address, and all containers within the Pod share the same network namespace. This setup allows containers within the same Pod to communicate using localhost. The Pod's IP address is used to communicate with containers in other Pods.
Lifecycle: Pods have a well-defined lifecycle with states like Pending, Running, Succeeded, and Failed. The kubelet runs on each node in the cluster, managing the Pods' lifecycle on its node.
Controllers: Higher-level Kubernetes objects, such as Deployments, ReplicaSets, and StatefulSets, manage the creation, scaling, and updates of Pods according to their specifications. For example, a ReplicaSet ensures the desired number of Pod replicas are running, creating new Pods or removing existing ones as needed.
Impermanence: Pods are designed to be ephemeral ( temporary) and disposable. If a Pod is terminated or fails, it is not restarted automatically. Instead, controllers like Deployments and ReplicaSets create new Pods to replace the failed ones, maintaining the desired state of the application.
What happens when you create a Pod in Kubernetes?
The user runs the
kubectl create
command to create the desired object (in this case, a pod) and thekubectl
communicates with the API Server to request the creation of the objectThe API Server stores the object's information in the etcd data store, which acts as the single source of truth for the Kubernetes cluster.
The request will be acknowledged and a response will be sent to the user.
The Scheduler detects that a new pod has been created but has not yet been assigned to a node. It is responsible for determining the most suitable node for running the pod.
The Pod state will be stored in the etcd data store and taking into account various constraints such as labels and taints, the Scheduler determines the best node for the pod. Labels and taints define affinity (rules for running groups of pods together) and anti-affinity (rules for avoiding the scheduling of certain pods together) constraints to ensure optimal pod distribution across the cluster.
The kubelet watches for new pods that are bound to its node by continuously communicating with the API server. The kube-controller-manager detects that the pod's desired state does not match its current state. It then communicates with the API Server, and the appropriate controller is invoked to make the necessary changes
The API Server triggers an API call to the kubelet running on the selected node, instructing it to create the actual pod.
The kubelet is responsible for setting up the pod using the container runtime. The kubelet interacts with the container runtime (e.g., Docker, containerd, or CRI-O) to create and manage containers that form a pod. It works in conjunction with the kube-proxy to make the required networking changes at the operating system level. This involves configuring the package filtering layer, which ensures that the pod can communicate with other components within the cluster.
Throughout this process, various components in the Kubernetes architecture work together to create and manage the pod, taking into consideration factors such as resource availability, node affinity, and networking requirements. This orchestrated approach ensures that the pod is deployed efficiently and operates seamlessly within the cluster.