The Magic of Creating a Pod in Kubernetes: What Really Happens?

The Magic of Creating a Pod in Kubernetes: What Really Happens?

A Deep Dive into the Intricate Processes and Components That Bring Pods to Life

Introduction

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.

  1. 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.

  2. Shared Storage: Pods enable containers to share storage volumes, providing a common file system for data exchange and storage.

  3. 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.

  4. 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.

  5. 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.

  6. 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?

  1. The user runs the kubectl create command to create the desired object (in this case, a pod) and the kubectl communicates with the API Server to request the creation of the object

  2. The API Server stores the object's information in the etcd data store, which acts as the single source of truth for the Kubernetes cluster.

  3. The request will be acknowledged and a response will be sent to the user.

  4. 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.

  5. 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.

  6. 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

  7. The API Server triggers an API call to the kubelet running on the selected node, instructing it to create the actual pod.

  8. 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.

Did you find this article valuable?

Support sharon sahadevan by becoming a sponsor. Any amount is appreciated!