top of page

Understanding Kubernetes Pods: Types, Pod YAML Structure, Lifecycle

 
 
Understanding Kubernetes Pods: Types, YAML Structure, and Lifecycle - A comprehensive guide to Kubernetes pods, their types, lifecycle phases, and how to define them using YAML.

Overview

Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. In Kubernetes, a Pod is the smallest deployable unit that represents a single instance of a running process in the cluster. Pods can host one or more containers, and they provide an abstraction over the container to manage and control them efficiently.


In this blog, we will dive into the details of Kubernetes Pods, their types, lifecycle, and the structure of a Pod YAML file, which is used to define the configuration for deploying a pod.

What is Kubernetes Pod?

In Kubernetes, a pod represents the smallest deployable unit. It can be thought of as a logical host for one or more applications. A pod is inherently transient, meaning that if it fails, Kubernetes will automatically create a new instance to replace it, ensuring uninterrupted service. Pods can house one or more containers, depending on the application’s needs, and these containers can include Docker containers. Furthermore, Kubernetes pods manage environmental dependencies such as persistent storage volumes, which are durable and accessible across the entire cluster, as well as configuration data needed to run the containers within the pod.

What Exactly a Kubernetes Pod Do?

In Kubernetes, pods can be compared to small work units in a larger system. Each pod is responsible for executing a specific process or task within the cluster. They are assigned unique identifiers to facilitate communication with other pods, possess storage for data, and contain the necessary configurations to perform their designated role. While many pods consist of a single container, others may contain multiple containers that work together to achieve a common objective.

How Kubernetes Pods Work?

A Kubernetes pod is like a small, self-contained unit that runs an application or part of an application. Think of it as a "box" where one or more containers live, and it takes care of everything the container needs to run, like network access and storage. For example, if you have a web application, the pod could contain one container running the web server and another one handling database tasks. These containers in the pod work together and can share resources like memory or disk space, which makes them efficient. If something goes wrong with a pod, Kubernetes automatically replaces it to keep everything running smoothly without you having to do anything.

Types of Kubernetes Pods?

  • Single Container Pod

  • Multi-Container Pod

  • Static Pods


Pods can be categorized into two types: Single-Container Pods and Multi-Container Pods, depending on how many containers they hold. A Single-Container Pod, as the name implies, contains only one container, while a Multi-Container Pod hosts multiple containers. The choice between these pod types depends on the specific requirements and use cases. Additionally, the process of creating these pods differs based on their configuration. For a detailed explanation of static pods, check out our dedicated blog on the topic here: Static Pods. The diagram below illustrates a multi-container pod.

multi container pod in kubernetes

Containers within a Kubernetes pod share several key resources:

  • Network namespace: All containers communicate with each other through localhost.

  • IPC namespace: Containers within a pod share the same interprocess communication space.

  • UTS namespace: They all share the same hostname.


However, not everything is shared:

  • PID namespace: By default, containers do not share process IDs, though Kubernetes allows this with the shareProcessNamespace option.

  • Mount namespace: Each container has its own private filesystem and directories. However, volumes mounted at the pod level are shared across containers within the pod.

Kubernetes Pods Overview

In the diagram below, you'll notice cube-shaped figures representing containers, each housing a single application or process. The cylinder-shaped structures are volumes, which provide persistent storage for the data generated by the containers. The circles represent pods, which are the fundamental building blocks in Kubernetes. Pods serve as the primary unit of management, with Kubernetes overseeing their operation. In turn, the pods manage the containers within them, ensuring smooth and efficient execution.

kubernetes pods overview

In a Kubernetes cluster, a pod represents an active process, which can host one or multiple containers. All containers within a pod share the same IP address and have access to the pod’s storage, network, and other resources. By grouping containers together, pods simplify the management and movement of containers across the cluster, ensuring seamless operation and coordination.

Pod YAML Structure

Now that we've covered the basics of a pod, let's dive into how to define one. A pod is a core Kubernetes object, and to create it, you'll need to specify its requirements in YAML format. Alternatively, you can use the kubectl imperative command to create a pod.


Here's an example of a Pod YAML configuration that creates a Nginx web server pod. This YAML represents the desired state of the pod in a declarative manner.

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  namespace: stage
  labels:
    app: nginx
    environment: stage
  annotations:
    description: This pod runs the nginx web server
spec:
  containers:
  - name: nginx-container
    image: nginx:1.27
    ports:
    - containerPort: 80

Let’s break down the key components of a Kubernetes pod object:

  • apiVersion: Specifies the API version of the pod. For this example, it’s v1.

  • kind: Denotes the type of object. In this case, it’s a pod.

  • metadata: Provides essential information to uniquely identify and describe the pod:

    • labels: Key-value pairs used to tag the pod, similar to cloud environment tagging. Labels help organize and group objects.

    • name: The name assigned to the pod.

    • namespace: The namespace the pod belongs to.

    • annotations: Additional metadata in key-value format, offering extra information about the pod.

  • spec: Defines the desired state of the pod, outlining the specifications for the containers inside it.

    • containers: Specifies the containers within the pod, including details like the container image, exposed ports, and other settings.


Here’s a visual representation of the key pod details displayed by the describe command.

kubernetes pods YAML structure

Creating a Pod

There are two main ways to create a pod:

  1. Using the kubectl imperative command: This method is quick and often used for learning or testing. However, it comes with certain limitations.

  2. Declarative approach with YAML manifest: This is the preferred method for project deployments, as it provides greater control and consistency when defining the desired state of the pod.


Let’s explore both methods by creating a NGINX pod with the following specifications:

  • Pod name: nginx-pod

  • Labels: app: nginx and environment: stage

  • Annotations: A descriptive annotation for the pod

  • Container image: nginx:1.27

  • Expose container on port 80


Option-1: Create a Pod Using Kubectl Imperative Command

Instead of creating a YAML file, we can directly use the kubectl command to create the pod.

kubectl run nginx-pod --image=nginx:1.27 --port=80 --namespace=stage

Option-2: Create a Pod Using Declarative YAML

In real-world projects, the declarative approach is typically used to create pods. Let’s walk through the process of creating a pod using a YAML manifest. First, create a file called nginx.yaml with the following content:

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  namespace: stage
  labels:
    app: nginx
    environment: stage
  annotations:
    description: This pod runs the nginx web server
spec:
  containers:
  - name: nginx-container
    image: nginx:1.27
    ports:
    - containerPort: 80

Now, to deploy the manifest, you need to execute the following kubectl command with the file name.

kubectl create -f nginx.yaml

Is it necessary to remember every parameter to create the YAML? Not at all. You can use the --dry-run flag to generate the YAML file automatically.


Here’s an example.

kubectl run nginx-pod --image=nginx:1.27 --dry-run=client -o yaml

You can save the YAML output by redirecting the dry-run output to a file.

kubectl run nginx-pod --image=nginx:1.27 --dry-run=client -o yaml > nginx.yaml

Now that we have a running pod with the Nginx web server, the goal is to deploy and access the application inside the pod.


To achieve this, kubectl provides a port-forward command, which allows you to access running pods in the Kubernetes cluster from your local machine.


We have a pod named nginx-pod running. Let’s use the port-forward command to access it.

kubectl port-forward pod/nginx-pod 8080:80

You can now access the application from your local network by navigating to http://localhost:8080 in your web browser.


Here’s what happens when you run kubectl port-forward:

  • Kubectl binds the specified local port (in this case, 8080) to your system.

  • It then communicates with the Kubernetes cluster API to create a tunnel—essentially a single HTTP connection—to the appropriate node, and from there, to the specified pod and container port (in this case, port 80).

The kubectl port-forward command is primarily a debugging tool. To properly expose an application running in a pod, you should use the Kubernetes Service object.

Pod Lifecyle

An important concept to understand about a pod is its lifecycle. A pod is usually managed by a controller, such as a ReplicaSet or Deployment. However, when you create a single pod using YAML, it is not controlled by any controller. Regardless of how it is created, a pod goes through various lifecycle phases.


Here are the key pod lifecycle phases:

  • Pending: The pod creation request has been received, but it is still being scheduled. For example, the system may be downloading the container image.

  • Running: The pod is active and functioning as expected. For instance, it is handling client requests.

  • Succeeded: All containers in the pod have terminated successfully. This could happen after a task like a CronJob completes without issues.

  • Failed: All containers are terminated, but at least one container has failed. For example, the application inside the pod might not start due to a configuration error, causing the container to exit with a non-zero code.

  • Unknown: The pod’s status cannot be determined. This could happen if the cluster is unable to monitor the pod’s state.


This is illustrated in the diagram below.

Kubernetes pods lifecycle

Conclusion

In this blog, we've explored the fundamental concepts of Kubernetes Pods, including their types, structure, and lifecycle. Understanding Pods is crucial for managing containerized applications effectively in a Kubernetes environment. We discussed how Pods serve as the smallest deployable units, how they can host one or more containers, and how to define them using YAML manifests. Additionally, we walked through the different lifecycle phases of a Pod, from creation to termination, and highlighted the role of controllers in managing Pods at scale. By mastering Pods, you’re one step closer to leveraging Kubernetes for scalable and efficient application deployment and management.

References

If you found this blog helpful, don't forget to like 👍, comment 💬, and subscribe 🔔 for more insightful content! Your feedback means a lot and helps us improve. Feel free to share your thoughts, questions, or any topics you'd like to see covered next. Happy learning! 😊

1 Comment

Rated 0 out of 5 stars.
No ratings yet

Add a rating
Guest
Feb 01
Rated 5 out of 5 stars.

Great breakdown of Kubernetes Pods! I especially appreciated the detailed explanation of the lifecycle phases and how to define Pods using YAML. Understanding how Pods work is key to optimizing container orchestration in Kubernetes. This blog made complex concepts much easier to grasp. Looking forward to more insightful posts on Kubernetes and container management!

Like
average rating is 4 out of 5, based on 150 votes, Recommend it

Subscribe For Updates

Stay updated with the latest cloud insights and best practices, delivered directly to your inbox.

91585408_VEC004.jpg
Collaborate and Share Your Expertise To The World!
Ananta Cloud welcomes talented writers and tech enthusiasts to collaborate on blog. Share your expertise in cloud technologies and industry trends while building your personal brand. Contributing insightful content allows you to reach a broader audience and explore monetization opportunities. Join us in fostering a community that values your ideas and experiences.
business-professionals-exchanging-handshakes.png
bottom of page