Pods in Kubernetes: The Building Blocks of Your Scalable Applications

About The Author

Nikhil-KhandelwalNikhil Khandelwal VP Engineering
LinkedIn|31 May 2024

According to a 2022 survey, 70% of IT leaders said their organizations use Kubernetes. It is 2024 now and the number has dramatically increased over the past two years.  

It is safe to say that Kubernetes has transformed the way apps are deployed and managed in the cloud.  

When we talk about Kubernetes, how can we forget about the heart of this powerful orchestration system— Pods.  

For the uninitiated, Pods are the fundamental building blocks that encapsulate your containerized workloads.  

Let us dive into the blog to understand the meaning of pods, their importance, types, and the right way to manage a pod in Kubernetes.  

What is a Pod in Kubernetes? 

A Pod is the smallest unit in the Kubernetes object model.  

Pods are the primary building blocks of Kubernetes applications, which offer a self-contained environment with shared resources such as storage and networking.  

They make sure that containers within them are co-located as well as sharing the same lifecycle. This, in turn, helps them to communicate as well as work together efficiently.  

Every Pod has a unique IP address that enables smooth interaction between containers and services within the Kubernetes ecosystem. 

How Does a Pod Work in Kubernetes? 

In Kubernetes, a Pod encapsulates one or more tightly coupled containers that share the same network and storage resources.  

As already mentioned in earlier section, every Pod also is assigned a unique IP address, which certainly makes sure seamless internal communication among containers.  

Pods manage the entire lifecycle of the containers within them, ensuring they start, stop, and restart as needed.  

In simpler words, Pods support replication and scaling through Kubernetes controllers like Deployments, which manage multiple Pod instances for high availability and scalability.  

Plus, Pods enable health checks and rolling updates, making sure there is a bare minimum disruption during application updates and maintaining overall application resilience and efficiency. 

Why are Pods important in Kubernetes? 

Pods are a crucial aspect of Kubernetes. It is pods that enhance the flexibility and scalability of the platform to enable the deployment of containerized applications in a distributed system.  

Multi-container pods running in a single Pod help Kubernetes eliminate as many pods as possible to run a specific app. This, in turn, cuts down the overhead to manage as well as orchestrate containers. 

Pods also enable Kubernetes to implement different scheduling and scaling strategies. Say, for instance, Kubernetes can schedule several replicas of a Pod onto many nodes in a cluster, enabling the platform to scale the application horizontally.  

Types of Pods in Kubernetes

 Types of Pods in Kubernetes

Here are mainly two ways Pods in a Kubernetes cluster are utilized: 

  • Single-Container Pods:  

It is as if you have one container packaged cleanly in a Pod. Rather than managing individual containers, you have the option to manage these pods and each Pod contains only one container. Obviously, this keeps everything well organized. 

  • Multi-Container Pods:  

As the name suggests, these are special pods that can hold more than one container. The containers inside the pod work closely together, just like an ideal team that is working towards the same goal.  

For example, one container will be responsible for showing some info to the public, while the other one ensures it is update-to-date. It is as if you own a cozy home and have a group of cousins (containers) live in, working together. They share items such as storage and a designated network connection.  

Lifecycle of a Pods in Kubernetes  

In the Kubernetes world, the lifecycle of the Pods is managed by controllers according to their status.  

Controllers are the kind of manager who makes sure there are no mistakes. Each Kubernetes Pod has a status field where it indicates to everyone how it is doing. All this status is stored in a cool thing under the name “Phase” field, which is a summary of how things are going with the Pod. 

All that said, here are the different states of a Pod in Kubernetes: 

  • Pending 

First things first, the Pod is created; but some of its containers are completely ready and smoothly running till now. It is just like the Pod is waiting for others to get ready.  

  • Running 

Now, the Pod is all happy on a node. All its containers are seamlessly doing their tasks. They are either creating, running, or just restarting.  

  • Succeeded 

Each container in the Pod has completed its operations in the best way possible. Once it is completed, now the Pod will not restart again—the job is done and dusted.   

All the containers in the Pod have finished their tasks perfectly. Once done, the pod won’t restart; it’s like a job well done. 

  • Failed 

One of the containers inside the Pod failed to finish its duty or is just simply not leaving. Here, all the good Pods must give a zero status after completing. If not, it will be considered a failure.  

  • Unknown 

There are times when the controller gets confused, unable to figure out what is happening with the Pod. It becomes extremely mysterious to crack it.  

Plus, the PodStatus has a thing known as PodConditions. PodConditions is basically extra information that shows why the Pod is in its current status.  

Additionally, the PodStatus has something called PodConditions. These are like additional details showing why the pod is in its current state. PodConditions has:   

  • Type 

This indicates scheduling, readiness, initialization, or being unschedulable. 

  • Status 

This indicates if things are running good (True), bad (False), or the controller is not sure (Unknown).

Pods in Kubernetes-CTA1

How to Create Pods in Kubernetes? 

You can manage a Pod either with an imperative or declarative approach. For the uninformed, the imperative approach uses kubectl command for Pod management. On the other hand, declarative approach utilizes YAML or JSON manifest files. 

How to Create a Pod in an Imperative Way? 

  • Create a Pod  

Use the kubectl run command to create a Pod named nginx using the nginx image and label it with app=server: 

root@k8: ~# kubectl run nginx --image=nginx -l app=server 

pod/nginx created

 

This command directly instructs Kubernetes to create a Pod with the specified parameters. 

  • Check Pod Status  

To see if the Pod has been created, use the kubectl get pods command: 

root@k8: ~# kubectl get pods 

NAME    READY   STATUS    RESTARTS   AGE 

nginx   1/1     Running   0          102s 

 

This command lists all Pods in the default namespace, showing their status and other basic information. 

  • Get Detailed Pod Information  

To get more details about the Pod, such as its IP address and the node it is running on, use the -o wide flag: 

root@k8: ~# kubectl get pods -o wide 

NAME    READY   STATUS    RESTARTS   AGE     IP            NODE           NOMINATED NODE   READINESS GATES 

nginx   1/1     Running   0          3m29s   10.244.0.214  default-xjlt9 <none>           <none> 

 

This offers a clearer view of the Pods. 

  • Describe the Pod  

For an in-depth view of the Pod, including its state and recent events, use the kubectl describe pod <pod-name> command: 

root@k8: ~# kubectl describe pod nginx 

 

This command provides detailed information about the Pod, including its configuration, status, events, and more. 

Finally, this will be the output like below:

root@k8: ~# kubectl describe pod nginx 

Name:         nginx 

Namespace:    default 

Priority:     0 

Node:         default-xjlt9/192.168.1.2 

Start Time:   Wed, 22 May 2024 10:00:00 +0000 

Labels:       app=server 

Annotations:  <none> 

Status:       Running 

IP:           10.244.0.214 

Containers: 

  nginx: 

    Container ID:   docker://1234567890abcdef 

    Image:          nginx 

    Image ID:       docker-pullable://nginx@sha256:abcdef1234567890 

    Port:           <none> 

    Host Port:      <none> 

    State:          Running 

      Started:      Wed, 22 May 2024 10:01:00 +0000 

    Ready:          True 

    Restart Count:  0 

    Limits:         <none> 

    Requests:       <none> 

    Environment:    <none> 

    Mounts: 

      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-7x24d (ro) 

Conditions: 

  Type           Status 

  Initialized    True 

  Ready          True 

  ContainersReady True 

  PodScheduled   True 

Volumes: 

  kube-api-access-7x24d: 

    Type:    Projected (a volume that contains injected data from multiple sources) 

    SecretName:  <none> 

QoS Class:       BestEffort 

Node-Selectors:  <none> 

Tolerations:     <none> 

Events: 

  Type    Reason     Age    From               Message 

  ----    ------     ----   ----               ------- 

  Normal  Scheduled  4m30s  default-scheduler  Successfully assigned default/nginx to default-xjlt9 

  Normal  Pulling    4m20s  kubelet            Pulling image "nginx" 

  Normal  Pulled     4m10s  kubelet            Successfully pulled image "nginx" in 10s 

  Normal  Created    4m5s   kubelet            Created container nginx 

  Normal  Started    4m5s   kubelet            Started container nginx 

 

How to Create a Pod in Declarative Way? 

In the declarative approach, you define the desired state of your Kubernetes resources using YAML or JSON manifest files and apply these files using kubectl.  

Here is an example: 

Create a YAML Manifest  

First, create a YAML file named nginx-pod.yaml with the following content: 

apiVersion: v1 

kind: Pod 

metadata: 

  name: nginx 

  labels: 

    app: server 

spec: 

  containers: 

  - name: nginx 

    image: nginx 

 

This file describes a Pod named nginx using the nginx image and labels it with app=server. 

  • Apply the YAML Manifest 

Use the kubectl apply command to create the Pod from the YAML file: 

root@k8:~# kubectl apply -f nginx-pod.yaml 

pod/nginx created 

This command tells Kubernetes to create the resources defined in the manifest file. 

  • Check Pod Status  

Like the imperative approach, you can check the status of the Pod using the kubectl get pods command: 

root@k8: ~# kubectl get pods 

NAME    READY   STATUS    RESTARTS   AGE 

nginx   1/1     Running   0          42s 

 

  • Get Detailed Pod Information  

You can use the same commands (kubectl get pods -o wide and kubectl describe pod nginx) to get detailed information about the Pod. 

root@k8: ~# kubectl get pods -o wide 

NAME    READY   STATUS    RESTARTS   AGE     IP            NODE           NOMINATED NODE   READINESS GATES 

nginx   1/1     Running   0          1m      10.244.0.215  default-xjlt9 <none>           <none> 

root@k8: ~# kubectl describe pod nginx 

By using the declarative approach, you can maintain your infrastructure as code, making it easier to track changes and manage configurations across environments.

root@k8: ~# kubectl describe pod nginx 

Name:         nginx 

Namespace:    default 

Priority:     0 

Node:         default-xjlt9/192.168.1.2 

Start Time:   Wed, 22 May 2024 10:05:00 +0000 

Labels:       app=server 

Annotations:  <none> 

Status:       Running 

IP:           10.244.0.215 

Containers: 

  nginx: 

    Container ID:   docker://abcdef1234567890 

    Image:          nginx 

    Image ID:       docker-pullable://nginx@sha256:1234567890abcdef 

    Port:           <none> 

    Host Port:      <none> 

    State:          Running 

      Started:      Wed, 22 May 2024 10:06:00 +0000 

    Ready:          True 

    Restart Count:  0 

    Limits:         <none> 

    Requests:       <none> 

    Environment:    <none> 

    Mounts: 

      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-7x24d (ro) 

Conditions: 

  Type           Status 

  Initialized    True 

  Ready          True 

  ContainersReady True 

  PodScheduled   True 

Volumes: 

  kube-api-access-7x24d: 

    Type:    Projected (a volume that contains injected data from multiple sources) 

    SecretName:  <none> 

QoS Class:       BestEffort 

Node-Selectors:  <none> 

Tolerations:     <none> 

Events: 

  Type    Reason     Age    from               Message 

  ----    ------     ----   ----               ------- 

  Normal  Scheduled  2m30s  default-scheduler  Successfully assigned default/nginx to default-xjlt9 

  Normal  Pulling    2m20s  kubelet            Pulling image "nginx" 

  Normal  Pulled     2m10s  kubelet            Successfully pulled image "nginx" in 10s 

  Normal  Created    2m5s   kubelet            Created container nginx 

  Normal  Started    2m5s   kubelet            Started container nginx 

If you still find all this complicated or a bit confusing, it is always a smart move to hire DeVops engineers who are well-experienced in this field and have a clear understanding of the Kubernetes Pods.

Pods in Kubernetes-CTA2

Hire DevOps Engineers with VLink  

VLink understands the importance of Pods and how they can improve flexibility and scalability of Kubernetes. To help you make the most of both Pods and Kubernetes, we will help you hire the best DevOps engineers within 48 hours (about 2 days).  

Contact our team to get a 7–day free trial before onboarding a DevOps professional.  

FAQs
Frequently Asked Questions
How do Pods enable communication between containers?

Pods share the same network namespace. This, in turn, helps containers in a Pod to communicate across localhost. This eventually eases up coordination as well as data exchange. 

What is the difference between a Pod and a Deployment in Kubernetes?

A Pod is the basic unit. On the other hand, deployment is a higher-level abstraction that manages a number of Pods. Deployments also provide features such as rolling updates as well as rollbacks for more control.  

Can I run multiple copies of the same Pod for scalability?

The answer is yes. Kubernetes helps to replicate Pods with the help of controllers such as Deployments. Every replica makes sure continuous as well as scalable performance of the app.

How do Pods contribute to application resilience in Kubernetes?

The whole purpose of Pods is reliability. If in case a Pod fails, Kubernetes auto-restart it or build a whole new one. This, in turn, contributes to the app’s resilience.

Related Posts

How Much Does It Cost to Build an App Like GoPuff
23
Dec
How Much Does It Cost to Build an On-Demand Liquor Delivery App Like GoPuff?

Discover the complete guide to GoPuff app development costs. Learn key factors, features, and budgeting tips to build your alcohol delivery app efficiently. 

17 minute
122 views
Cost to Build a Last Mile Delivery App Like Onfleet
19
Dec
Cost to Build a Last Mile Delivery App Like Onfleet

Want to build a successful last-mile delivery app like Onfleet? Learn about the costs, challenges, and future of developing a feature-rich last-mile delivery app. 

17 minute
122 views
How AI Is Transforming Claims Processing in the Insurance Industry _ Banner
17
Dec
How AI Is Transforming Claims Processing in the Insurance Industry

Revolutionize your insurance claims process with AI. Learn how AI can automate tasks, improve accuracy, enhance customer experience, and reduce costs. 

18 minute
122 views
image
image
image
image
image
image
Get In Touch!
Phone