Kubernetes 101: How does Kubernetes create a Pod?

Photo by Growtika on Unsplash

Kubernetes 101: How does Kubernetes create a Pod?

Creating a Pod in Kubernetes involves several components working together to ensure that applications are run in a distributed and fault-tolerant manner. Here's a step-by-step process involving etcd, the Scheduler, API Server, Kubelet, and the Control Manager:

  1. API Server:

    • The process begins when you submit a Pod definition to the Kubernetes cluster, typically using the kubectl command-line tool or a CI/CD pipeline. This Pod definition is a YAML or JSON file that describes the Pod's desired state, including the container images, volumes, network settings, and other specifications.

    • This request is received by the API Server, which acts as the front end for the Kubernetes control plane. The API Server authenticates and validates the request. If the request is valid, the API Server writes the Pod's desired state to the distributed key-value store, etcd.

  2. etcd:

    • etcd is a consistent and highly-available key-value store used as Kubernetes' backing store for all cluster data. Once the Pod's desired state is stored in etcd, the information becomes available to other components of the Kubernetes control plane.
  3. Scheduler:

    • The Scheduler watches for new Pods that do not yet have a node assigned to them. The Scheduler selects a node for the Pod to run on based on several scheduling criteria, including resource availability, constraints, affinity specifications, and others. Once a node is selected, the Scheduler updates the Pod's information in the API Server with the node assignment.
  4. API Server (again):

    • The API Server receives the update from the Scheduler and writes the information back to etcd. The state of the cluster, including the newly assigned Pod, is now updated in etcd.
  5. Control Manager:

    • The controller manager, which consists of multiple controllers, constantly monitors the state of the cluster. One of the controllers, the replication controller, specifically watches for Pods. If a Pod goes down and the user has set the restart policy to "always," the replication controller will create a new Pod to replace the failed one.
  6. Kubelet:

    • Each node in the cluster runs an agent called Kubelet, which is responsible for ensuring that containers are running in a Pod.

    • Kubelet watches the API Server for Pods that have been assigned to its node. When it finds a new Pod, it reads the Pod's specification and ensures the containers described in the Pod are started and running.

    • The Kubelet downloads the container images, creates and starts the containers, and sets up the Pod's networking. It continually monitors the state of the containers and reports back to the API Server, which updates etcd with the current state of the newly created Pod.

  7. Container Runtime:

    • It's worth mentioning that the actual running of containers is handled by the container runtime (e.g., Docker, containerd) on each node, which is invoked by Kubelet to manage the container's lifecycle on the node.
  8. Kube-proxy:

    • kube-proxy plays a crucial role in the networking within a Kubernetes cluster, facilitating communication to and from Pods. It operates at the network level, managing the IP routing and forwarding rules to ensure that the traffic to services is directed to the correct Pods.

    • In the context of the request flow described earlier, kube-proxy's primary role is to ensure that when a client sends a request to a Service (using the Service's Virtual IP), the request is efficiently routed to one of the backend Pods that constitute the Service. The choice of backend Pod can be influenced by the Service's session affinity configuration and load balancing policy.

Through this process, Kubernetes ensures that the desired state of the cluster, as defined by the user, is achieved and maintained. This involves creating Pods on appropriate nodes, monitoring their state, and automatically handling failures or necessary rescheduling, all while keeping the user-defined specifications in mind.

References:

  1. https://t.me/devops_orbit

  2. YouTube IBM: How does Kubernetes create a Pod?

  3. Kubernetes Scheduler

  4. The Kubernetes API

  5. kubelet

  6. kube-controller-manager