Kubernetes Pods: How to Create and Manage Them (original) (raw)

Last Updated : 28 Apr, 2026

A Pod represents a single instance of a running process in your cluster and can contain one or more containers. Think of a Pod as a lightweight, application-specific "logical host". All containers within a single Pod are guaranteed to be co-located on the same worker node, and they share the same environment. This shared context is the magic of the Pod abstraction and includes:

10_10_10_1

Kubernetes Pods

In the following diagram, you can see cube-like structures; they are called containers Each of the containers will have one container. The cylinder-like structure is called volume, where the data of the containers will be stored and the circles are called pods.

A Pod can contain one or more containers, and all containers inside a Pod share:

Pods allow easy movement of containers within a cluster. They are created and managed by controllers, which handle:

If a node fails in the cluster, the controller detects the unresponsive Pod and replicates it on another node to continue the same function.

The three commonly used controllers are:

The Pod Lifecycle

A Pod follows a defined lifecycle and exists in one of several high-level phases.

Working with Kubernetes Pods

Pod Operating System

The operating system (OS) running inside pods is typically determined by the container image used to create the pod's containers. Pods in Kubernetes can run containers based on various Linux distributions, such as Ubuntu, Alpine Linux, or CentOS, among others.

Pods and controllers on Kubernetes

The pod will not be rescheduled to a different node when it expires because it is ephemeral (lasting only a very brief time). , we shouldn't directly construct or use pods; instead, we should deploy pod-like deployment, replica sets, and daemon sets to maintain the pod with the aid of Kubernetes services. We have to deploy the pods with the help of objects. The main objects are

Getting Started with Kubernetes Pod

**1. Create a Pod Imperatively

For a quick test, you can create a simple Nginx Pod with a single command. This is a great way to verify that your cluster is working.

kubectl run nginx --image=nginx

This command instructs Kubernetes to create a Pod named nginx using the official nginx container image.

**2. Generate a Declarative Manifest

Writing YAML from scratch can be tedious. A powerful technique is to use an imperative command to generate the YAML for you. The --dry-run=client -o yaml flags tell kubectl to output the YAML representation of the object without actually creating it in the cluster.

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

Now, open the pod.yaml file. It will look something like this:

apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:

Let's break down the key fields:

**3. Create a Pod Declaratively

First, let's clean up the Pod we created imperatively.

kubectl delete pod nginx

Now, apply the configuration from the manifest file you just created. The kubectl apply command is the cornerstone of the declarative approach. It tells Kubernetes to make the cluster's live state match the state defined in the file.

kubectl apply -f pod.yaml

Kubernetes will read the file and create the Pod. If you were to change the file and run kubectl apply again, Kubernetes would intelligently update the existing Pod to match your new desired state.

Inspecting and Interacting with Your Pod

Once a Pod is running, you'll need a set of commands to check its status, view its logs, and debug any issues.

kubectl get pods

NAME READY STATUS RESTARTS AGE

nginx 1/1 Running 0 60s

kubectl describe pod nginx

kubectl logs nginx

kubectl exec -it nginx -- bash

Advanced Patterns: Multi-Container Pods

Containers in a Pod are so tightly coupled, you can use them to create powerful, cohesive application units. Two common patterns are Init Containers and Sidecar Containers.

Init Containers

An Init Container is a specialized container that runs and completes _before the main application containers are started. They are defined in a separate initContainers block in the Pod manifest. You can have multiple Init Containers, and they will run sequentially. Each one must exit successfully before the next one is started.

**Use cases for Init Containers:

Sidecar Containers

A Sidecar container runs alongside your main application container within the same Pod. Unlike an Init Container, it starts with and continues to run for the full life of the application container. This pattern allows you to extend or enhance the functionality of the main container without adding complexity to the application's own codebase.

**Use cases for Sidecar Containers:

Communication between Kubernetes pods

Kubernetes Pod update and replacement

In Kubernetes, updating and swapping out pods is like applying an additional application of create to your application.

Kubernetes Static Pods

**Static Pods in Kubernetes

Static pods are managed directly by the kubelet on a specific node rather than through the API server. You define them using a pod manifest file placed in a folder like /etc/kubernetes/manifests, and the kubelet automatically starts the pod. They are node-specific, cannot be managed by Deployments or ReplicaSets, and are often used for critical system components like kube-apiserver or monitoring agents.

Basic Kubectl Commands for Kubernetes Pods

**Create Pod: A pod can be created by using the create command format.

kubectl create -f [FILENAME]

In the [FILENAME], you need to insert the required filename with which you want to create your file, and then a new pod with the name GeeksforGeeks will be created.

kubectl create

**To Delete Kubernetes Pod: Delete the pod using the below command.,

kubectl delete -f FILENAME

Here the pod named GeeksforGeeks will be deleted

Kubectl delete pod

**To Get Kubernetes Pod: To see the number of pods available in the particular namespace can be seen using the below command.

Kubectl get pod --namespace

Troubleshooting with kubectl