Kubernetes Pods are the smallest and simplest unit of deployment in Kubernetes, a container orchestration platform. A Pod is a group of one or more containers that are deployed together on the same host, share the same network namespace, and can communicate with each other via local host. Pods enable containers to work together as a cohesive unit, providing a way to run multiple tightly coupled applications or microservices within a single environment.

Each Pod is designed to support a single application or service, but it can host multiple containers that need to share resources like storage volumes, network interfaces, or environment variables. Pods are temporary, meaning they don’t live forever they are created, scheduled, and managed by Kubernetes as part of a broader deployment or service.

One key feature of Pods is that they are always co-located and co-scheduled, meaning the containers inside a Pod will always run on the same node within the Kubernetes cluster. This close coupling of containers helps improve the performance and reliability of distributed applications. Pods can also be scaled, updated, or replaced easily, providing a flexible and resilient way to manage containerized workloads in a Kubernetes-based environment.

What are Kubernetes Pods?

Kubernetes Pods are the fundamental units of deployment in Kubernetes, an open-source container orchestration platform. A Pod is a group of one or more containers that are deployed together on the same node in a Kubernetes cluster. These containers share the same network namespace, meaning they can communicate with each other using local hostnames or IP addresses, and they also share storage resources such as volumes.

In Kubernetes, Pods allow for the co-location and co-scheduling of tightly coupled application components that need to work together. For example, a Pod might contain a main application container and a helper container that manages logs or data persistence. While Pods can contain multiple containers, it's common for a Pod to run a single container, with additional containers included when specific functionality is required.

Pods are temporary, meaning they are created, run, and terminated by Kubernetes as part of higher-level constructs like Deployments, StatefulSets, or DaemonSets. When a Pod is terminated, Kubernetes ensures that the necessary replacements are made to keep the application running. Overall, Kubernetes Pods provide a way to manage, scale, and orchestrate containerized applications with flexibility and efficiency, ensuring that related containers operate seamlessly within the same environment.

Types of Kubernetes Pods 

In Kubernetes, Pods are the fundamental unit of deployment, but there are several ways to configure and manage them based on different use cases. While there is no explicit "type" of Pod in the strictest sense, Kubernetes supports various Pod configurations and deployment patterns to address different application needs. Here are some common "types" of Pods in terms of deployment scenarios:

1. Single-container Pods

The most basic and common Pod type typically consists of a single container.

Use Case: This is ideal for simple applications or microservices that only need one container. While Kubernetes is designed to support multi-container Pods, many applications are simple enough to run as a single container in a Pod.

Example: A basic web server or a database running in isolation.

2. Multi-container Pods

Pods that contain two or more containers running in the same network namespace and sharing the same storage.

Use Case: When you need to run related containers that need to share resources. Containers inside a Pod can communicate with each other via localhost, which makes it easier for tightly coupled components to work together.

Example: A web server container and a sidecar container that handles logging, monitoring, or data persistence.

3. Init Containers

Init containers are specialized containers that run and are completed before the main application containers in the Pod start. They are useful for initialization tasks like setting up the environment, downloading configuration files, or preparing databases.

Use Case: When an application requires some preparatory steps before the main container can begin running.

Example: Running a container that initializes a database schema or downloads configuration data from a remote server.

4. HostNetwork Pods

Pods that use the host node’s network namespace instead of having their isolated network namespace. This means the Pod will share the node’s IP address and port space.

Use Case: Useful when a Pod needs to expose a service directly on the node’s IP address or requires low-level networking access.

Example: A Pod that runs a network service like a firewall or DNS that needs direct access to the node's networking stack.

5. DaemonSet Pods

Pods that are deployed to every node in a Kubernetes cluster (or to a subset of nodes, depending on the configuration). DaemonSets are used for running services that need to run on every node, such as log collectors, monitoring agents, or network proxies.

Use Case: Ensuring that specific workloads are distributed across all nodes in the cluster.

Example: A logging agent like Fluentd or a monitoring agent like Prometheus node exporter.

6. StatefulSet Pods

Pods managed by a StatefulSet are designed for stateful applications that require persistent storage and stable network identities. Unlike other Pods, StatefulSet Pods have stable, unique network names and persistent storage.

Use Case: When running applications that require persistence, such as databases or applications that need consistent storage and network identity.

Example: A database like MySQL or a distributed application like Apache Cassandra that requires persistent volumes.

7. ReplicaSet Pods

Pods controlled by a ReplicaSet ensure that a specified number of identical Pods are always running in the cluster. ReplicaSets are typically used in combination with Deployments for scaling applications.

Use Case: To maintain a consistent number of Pods for high availability and load balancing across your application.

Example: A web server application where you need multiple identical Pods to handle traffic evenly.

8. Job and CronJob Pods

Pods managed by a Job or CronJob are used for batch processing or scheduled tasks. Jobs ensure that a task is completed successfully, and CronJobs allows for the scheduling of Pods to run at specified intervals.

Use Case: Useful for batch jobs, one-time tasks, or periodic scheduled tasks.

Example: A batch data processing task or a database backup that runs nightly via a CronJob.

9. Pod with Affinity and Anti-Affinity

Not a "type" of Pod per se, but a configuration that allows you to define where Pods should or should not be scheduled based on the node's characteristics or other Pods' location in the cluster.

Use Case: When you want to ensure Pods are scheduled in specific ways, such as running together on the same node or spread out across different zones for high availability.

Example: A high-performance computing application where Pods should only run on nodes with specific GPUs or a service that should not run on the same node as another service for resource isolation.

How to Create a Pods in Kubernetes? 

Creating a Pod in Kubernetes is a straightforward process, and there are multiple ways to do it. The most common method is using a YAML manifest file to define the configuration of the Pod, followed by applying that configuration to the Kubernetes cluster using the Kubectl command-line tool. Below, we'll walk through the steps to create a simple Pod using a YAML file and using the kubectl command directly.

1. Create a Pod Using a YAML Manifest

A Kubernetes Pod is typically defined using a YAML configuration file that specifies the Pod's desired state, including the containers it runs and other configuration details. Below is an example of a simple YAML file for creating a Pod.

Example: pod. yaml

apiVersion: v1
kind: Pod
Metadata:
  name: my-pod
Spec:
  Containers:
  - name: nginx-container
    image: nginx: latest
    Ports:
    - containerPort: 80

2. Create a Pod Using the Kubectl run Command

You can also create a Pod directly from the command line using the kubectl run command. This method is faster for quick, simple Pods but provides a different level of flexibility and configuration than a YAML file.

Example: Create a Pod with Nginx

kubectl run my-pod --image=nginx --port=80

3. Check the Pod's Status

Once the Pod is created, you can check its status to see if it's running correctly.

View Pod details:

kubectl describes pod my-pod


  • This provides detailed information about the Pod's status, including events, container status, and resource usage.

Check logs: If you're using a Pod with a container running a service, you can also check the container logs to ensure the service is working correctly.

kubectl logs my-pod

4. Delete the Pod

When you're done with the Pod or need to clean up, you can delete the Pod using the kubectl delete command:

kubectl delete pod my-pod

This will remove the Pod from the cluster.

How to View a Kubernetes Pod? 

Viewing a Kubernetes Pod involves checking its status, logs, and detailed information about the containers inside it. You can do this using various kubectl commands to gather insights into your Pods. Here's a step-by-step guide on how to view a Kubernetes Pod:

List all Pods:

kubectl get pods


  • This shows all Pods in the current namespace with their status, restarts, and age.

Get detailed information about a Pod:

kubectl describe pod <pod-name>


  • Provides detailed info about the Pod, including container statuses, events, and volumes.

View Pod logs:

kubectl logs <pod-name>


Shows the logs of the first container in the Pod. To see logs of a specific container:

kubectl logs <pod-name> -c <container-name>


Watch Pod status in real-time:

kubectl gets pods -w


  • Continuously updates Pod status as it changes.

Access a Pod’s shell:

kubectl exec -it <pod-name> -- /bin/bash


  • Opens an interactive shell inside the Pod (if bash is available).

These commands help monitor and troubleshoot Pods in your Kubernetes cluster.

How to Implement Kubernetes Pod Policy?

Implementing Kubernetes Pod Policies involves using various mechanisms to control and enforce the behavior of Pods in your cluster. Kubernetes provides several tools and features to define rules, security settings, and scheduling behaviors that Pods must adhere to.

These include Pod Security Policies (PSP) (deprecated in Kubernetes 1.21), Pod Disruption Budgets (PDB), Pod Affinity/Anti-Affinity, and Resource Requests and Limits. Here's an overview of how to implement some of these Pod policies:

1. Pod Security Policies (PSP) (Deprecated in Kubernetes 1.21)

While Pod Security Policies are deprecated in Kubernetes 1.21 and removed in 1.25, they were used to control the security features that Pods could or could not use (e.g., privileged containers host network access). Instead, you can now use Pod Security Admission or other security tools to enforce security policies.

Example (PSP configuration): Before PSP was deprecated, you could define a policy like this:

apiVersion: policy/v1beta1
kind: PodSecurityPolicy
Metadata:
  name: restricted-psp
Spec:
  privileged: false
  Volumes:
    - 'emptyDir'
  host network: false
  host PID: false
  host IPC: false
  runAsUser:
    rule: 'MustRunAsNonRoot'
  SELinux:
    rule: 'RunAsAny'

2. Pod Disruption Budgets (PDB)

A Pod Disruption Budget (PDB) ensures that a minimum number or percentage of Pods are available during voluntary disruptions like node maintenance or cluster upgrades.

Steps to create a PDB:

  • Define the minimum number of Pods that should remain available.

Example:

apiVersion: policy/v1
kind: PodDisruptionBudget
Metadata:
  name: my-app-pdb
Spec:
  in available: 2
  Selector:
    matchLabels:
      app: my-app

This PDB ensures that at least 2 Pods with the label app=my-app are always available, even during disruptions.

3. Pod Affinity and Anti-Affinity

Pod Affinity and Anti-Affinity allow you to control where Pods are scheduled based on the labels of other Pods in the cluster.

  • Pod Affinity: Makes sure that Pods are scheduled on nodes that have certain labels or that they are scheduled together with other Pods based on specific rules.
  • Pod Anti-Affinity: Ensures that Pods are not scheduled on nodes with certain labels or near specific Pods.

Example (Pod Affinity):

This example ensures that a Pod will be scheduled on a node that already has a Pod with the label app=web.

apiVersion: v1
kind: Pod
Metadata:
  name: my-pod
Spec:
  Affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - label selector:
          matchLabels:
            app: web
        topology: kubernetes.io/hostname
  Containers:
  - name: my-container
    image: nginx

Example (Pod Anti-Affinity):

This example ensures that a Pod won't be scheduled on the same node as another Pod with the label app=web.

apiVersion: v1
kind: Pod
Metadata:
  name: my-pod
Spec:
  Affinity:
    podAntiAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - label selector:
          matchLabels:
            app: web
        topology: kubernetes.io/hostname
  Containers:
  - name: my-container
    image: nginx

How to Destroy Kubernetes Pod?

Destroying or deleting a Kubernetes Pod is a simple task, and you can do it using the Kubectl command-line tool. When you delete a Pod, Kubernetes removes it from the cluster. However, depending on how the Pod was created (e.g., as part of a Deployment, StatefulSet, or ReplicaSet), Kubernetes might automatically recreate the Pod to maintain the desired state.

Here are the methods to destroy or delete a Kubernetes Pod:

1. Delete a Pod by Name

To delete a Pod, use the kubectl delete pod command followed by the Pod's name. This will immediately remove the Pod from the cluster.

Command:

kubectl delete pod <pod-name>

Example:

If you have a Pod named my-pod:

kubectl delete pod my-pod

After running this command, the specified Pod will be deleted, and you will see the output like this:

Pod "my-pod" deleted

2. Delete Pods in a Specific Namespace

If the Pod is in a non-default namespace, you need to specify the --namespace flag:

Command:

kubectl delete pod <pod-name> --namespace=<namespace-name>

Example:

If the Pod is in the dev namespace:

kubectl delete pod my-pod --namespace=dev

Pod update and replacement 

In Kubernetes, Pods are immutable, meaning they cannot be updated directly. Instead, to update or replace a Pod, you modify the controller (like a Deployment, StatefulSet, or DaemonSet) that manages the Pods. Kubernetes then automatically handles the replacement of the old Pods with new ones based on the updated configuration.

Key Points:

1. Updating Pods: You update the controller (e.g., Deployment or StatefulSet), not the Pod itself. When updated, Kubernetes replaces old Pods with new ones.some text

  • Deployment Update: Use kubectl edit deployment <deployment-name> or kubectl to set the image to update the container or other settings.

2. Pod Replacement: When a Pod is updated, the old Pod is deleted, and a new one is created automatically by the controller to reflect the changes.

3. Rolling Updates: For zero-downtime updates, rolling updates are used. Kubernetes replaces Pods gradually, ensuring that the app remains available.

4. Manual Pod Replacement: If not using controllers, you can delete the Pod and recreate it manually using a YAML file or Kubectl application.

In short, Pod updates in Kubernetes are done by updating the associated controller, which triggers the replacement of Pods while maintaining the desired application state.

Kubernetes Pods Best Practices for Security, Reliability, and Resource Requests

Here are examples of Kubernetes Pod best practices for security, reliability, and resource requests, each with a single YAML code block:

1. Security Best Practice: Run Containers as Non-Root User

apiVersion: v1
kind: Pod
Metadata:
  name: my-pod
Spec:
  Containers:
    - name: my-container
      image: nginx: latest
      Security context:
        runAsUser: 1000        # Non-root user ID
        runAsNonRoot: true     # Ensure the container runs as a non-root user

This ensures that the container runs as a non-root user, which is a fundamental security practice to reduce the impact of potential exploits.

2. Reliability Best Practice: Use Readiness and Liveness Probes

apiVersion: v1
kind: Pod
Metadata:
  name: my-pod
Spec:
  Containers:
    - name: my-container
      image: nginx: latest
      livenessProbe:
        HTTP get:
          path: /health
          port: 8080
        initialDelaySeconds: 5
        period seconds: 10
      Readiness probe:
        HTTP get:
          path: /readiness
          port: 8080
        initialDelaySeconds: 5
        period seconds: 10

This YAML configures liveness and readiness probes to monitor the health of the Pod, ensuring it’s healthy and ready to serve traffic before it starts handling requests.


3. Resource Management Best Practice: Set Resource Requests and Limits

apiVersion: v1
kind: Pod
Metadata:
  name: my-pod
Spec:
  Containers:
    - name: my-container
      image: nginx: latest
      Resources:
        Requests:
          memory: "64Mi"      # Minimum memory required
          CPU: "250m"         # Minimum CPU required
        Limits:
          memory: "128Mi"     # Maximum memory allowed
          CPU: "500m"         # Maximum CPU allowed


This ensures that Kubernetes will reserve a minimum amount of resources (requests) for the container and limit its maximum usage, preventing resource overconsumption and ensuring fair resource allocation.

Pods with multiple containers

In Kubernetes, a Pod can contain one or more containers that share the same network namespace, storage, and other resources. Pods with multiple containers are typically used when the containers are tightly coupled and need to work together.

These containers are deployed together in the same Pod, ensuring that they can communicate efficiently and share resources such as storage volumes.

Example: Pod with Multiple Containers

Below is an example of a Pod with two containers: one running a web server (Nginx) and another running a sidecar container (a logging agent like Fluentd).

apiVersion: v1
kind: Pod
Metadata:
  name: multi-container-pod
Spec:
  Containers:
    - name: nginx-container
      image: nginx: latest
      Ports:
        - containerPort: 80
      Volume mounts:
        - name: shared-volume
          mountPath: /usr/share/nginx/HTML
    - name: fluentd-container
      image: fluent/fluent:v1.12-1
      Volume mounts:
        - name: shared-volume
          mountPath: /fluent/log
  Volumes:
    - name: shared-volume
      emptyDir: {}

Conclusion

Kubernetes Pods are the smallest deployable units in Kubernetes, consisting of one or more containers that share the same network and storage resources. They enable efficient communication and resource sharing between containers. Pods are managed by controllers like Deployments, ensuring scalability, reliability, and high availability in Kubernetes environments.

FAQ's

👇 Instructions

Copy and paste below code to page Head section

A Kubernetes Pod is the smallest and simplest deployable unit in Kubernetes, which can hold one or more containers. Containers in a Pod share the same network IP, storage, and other resources, making them closely related.

A Pod is a higher-level abstraction that may contain one or more containers. Containers inside a Pod share the same network and storage but are isolated from other Pods. Pods provide a way to manage and deploy containers together with shared resources.

Yes, a Pod can contain multiple containers that share the same network, storage, and namespace. These containers are often tightly coupled and work together (e.g., sidecar or ambassador patterns).

Multiple containers in a Pod are used when they need to share resources or work together closely. For example, one container might run an application while another runs a logging or monitoring agent.

If a container in a Pod crashes, Kubernetes will attempt to restart the container according to the configured liveness and readiness probes. If the entire Pod fails, Kubernetes may recreate the Pod, depending on the deployment settings.

Pods are scheduled by the Kubernetes scheduler, which places them on the appropriate node based on available resources, affinity rules, and constraints (e.g., node labels, resource requests/limits).

Ready to Master the Skills that Drive Your Career?
Avail your free 1:1 mentorship session.
Thank you! A career counselor will be in touch with you shortly.
Oops! Something went wrong while submitting the form.
Join Our Community and Get Benefits of
💥  Course offers
😎  Newsletters
⚡  Updates and future events
a purple circle with a white arrow pointing to the left
Request Callback
undefined
a phone icon with the letter c on it
We recieved your Response
Will we mail you in few days for more details
undefined
Oops! Something went wrong while submitting the form.
undefined
a green and white icon of a phone
undefined
Ready to Master the Skills that Drive Your Career?
Avail your free 1:1 mentorship session.
Thank you! A career counselor will be in touch with
you shortly.
Oops! Something went wrong while submitting the form.
Get a 1:1 Mentorship call with our Career Advisor
Book free session