What Are Pods in Kubernetes? A Quick Explanation (2023)

According to the CNCF's 2020 survey, Kubernetes is the most popular container orchestration platform, with 91% of respondents using it in production. It is made up of various components that work together to provide a complete container orchestration solution. Some of the key components of Kubernetes include the API server, etcd, kubelet, kube-proxy, nodes, and Pods. 

In this tutorial, you will learn about Pods, how Kubernetes uses them, their lifecycle, and more.

What is a Pod?

In Kubernetes, a Pod is the smallest deployable unit that can be managed. It is a logical host for one or more containers. A Pod can contain one or more containers that share the same network namespace and can access the same shared volumes. Pods are used to deploy and scale applications in Kubernetes. 

How Pods Work

When you deploy an application in Kubernetes, you create one or more Pods, and Kubernetes manages the Pods for you, ensuring that the desired number of replicas are running at all times. Each Pod in Kubernetes has a unique IP address and can have one or more containers running inside it.

💡
To scale up your application, you create Pods, and to scale down, you delete the Pods. You do not add additional containers to an existing Pod to scale your application.

Multi-Container Pods

A single Pod can have multiple containers but usually not multiple containers of the same kind.

Sometimes, you might have a scenario where helper containers are doing some kind of supporting task for a web application, such as processing user-entered data, processing a file uploaded by the user, etc., and you want these helper containers to live alongside your application container. In that case, you can have both containers as part of the same POD.

Creating a Pod

There are two main ways to create a pod in Kubernetes: Imperatively or Declaratively.

Imperatively

To deploy a pod in Kubernetes, you can use the kubectl run command followed by the name of the pod and the image to use. For example:

kubectl run my-pod --image=my-image

This will create a pod named my-pod using the my-image image.

Declaratively

You can create a pod declaratively by using a YAML file. This method involves creating a YAML file that describes the desired state of the pod. You can then use the kubectl create command to create the pod using the YAML file. Here's an example:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: my-image

You would then create the pod using the following command:

kubectl create -f my-pod.yaml

Both methods have advantages and disadvantages, so you should choose the one that best suits your use case.

Embark on a guided journey to Kubernetes mastery with KodeKloud's Kubernetes Learning Path.

Commonly Used Pods' Commands

To get a list of all the pods in your cluster, use the command.

kubectl get pods

 This will show you the name, status, and other details of all the pods in your cluster.

If you want to see the logs of a specific pod, you can use the kubectl logs command followed by the pod's name. For example:

kubectl logs my-pod

This will show you the logs of the my-pod pod.

To delete a pod, you can use the kubectl delete command followed by the name of the pod. For example:

kubectl delete pod my-pod

This will delete the my-pod pod from your cluster.

Networking in Kubernetes

Pods in Kubernetes are designed to work together and communicate with each other. By default, all pods are assigned a unique IP address within the Kubernetes cluster, which allows them to communicate with each other using that IP address. 

Pods can communicate with other pods in the same namespace and with Pods in other namespaces. Additionally, Pods can communicate with services created within the same Kubernetes cluster.

Kubernetes uses a flat network model, which means that every Pod can communicate with every other Pod using its IP address. This model simplifies network configuration and makes it easy to scale the cluster as needed.

To ensure that Pods can communicate with each other, Kubernetes assigns a unique IP address to each Pod. This IP address is accessible only within the Kubernetes cluster and is not visible outside the cluster.

Overall, Kubernetes provides a robust networking model that allows pods to communicate with each other and with other resources within the cluster.

Pods are Ephemeral

Kubernetes is designed to manage Pods dynamically, which means that Pods can come and go as needed to handle changes in workload or to maintain the desired state of the system. This approach allows Kubernetes to be highly scalable and resilient, as it can quickly adapt to changes in demand without requiring manual intervention.

However, it also means that any data or state stored within a Pod is also ephemeral and may be lost when the Pod is terminated. To address this issue, Kubernetes provides various mechanisms for managing persistent data, such as using stateful sets or persistent volumes.

Kubernetes Reference Docs


More Kubernetes: