Kubernetes Pods: The Basic Deployable Unit

Reading Time: 4 minutes

Hello readers, I’ll be covering about the basic unit of execution in Kubernetes, which is Pod.

What are pods?

Pods are the atomic unit of scheduling in kubernetes. Its the smallest and the simplest unit in Kubernetes object model that you create and deploy.

A pod consist of 1 or more containers which shares common storage and network namespaces. It is recommended to run a single container inside a pod unless the containers are dependent on each other. In case, we want the containers to be tightly coupled, then we can have a multi-container pod.

Also, the pods are ephemeral in nature. It means, whenever a pod dies, it cannot be restarted. It spawns again on a node with some different IP address.

Kubernetes is also known for auto-scaling of the application which is actually done by increasing the replicas of these pods. Rather than increasing containers in a specific pod, we increase the pod replicas.

Pod Communication

Every pod is assigned with its own IP address which is provided by one of the node component known as Kube-Proxy. The pod’s containers can communicate inside the whole cluster via IP addresses.

There are two types of communication that takes places.
1. Inter-pod communication
2. Intra-pod communication

1. Inter-pod communication: The communication between two containers residing inside different pods is termed as inter-pod communication. Here communication takes via the pod’s IP address.

This image has an empty alt attribute; its file name is image-7.png

2. Intra-pod communication: The communication between two containers residing inside the same pod is termed as intra-pod communication. Here communication takes place via the localhost, using the container’s port. Also, a multi-containered pod exposes its containers to other pod via the port number.

This image has an empty alt attribute; its file name is image-8.png

Pod Life-cycle

The pod has certain status in its various phases of life cycle. The life cycle phases of a pod are:

  • Pending: When the pod is accepted by the Kubernetes system and start downloading required images. The pod remains in this stage before being scheduled on a node.
  • Running: When the pod has been bound to a node, and all the containers have been created. Also, at least one container is running, or in the process of running.
  • Succeeded: When all the containers have successfully terminated.
  • Failed: When any of the container is terminated in failure, i.e., the container is exited with a non-zero status.
  • Unknown: If for some reason, the pod didn’t obtain any state, then the state is Unknown.

To check a particular pod status, the command is as follows: kubectl describe pods <pod-name> | grep Status:

Pod manifest

Pod manifest are the spec files, which are written in yaml or json format, which are used to tell the desired state of the cluster. Here’s a sample manifest file written in yaml format:

---
apiVersion: v1
kind: Pod
metadata: 
  name: nginx-pod
  labels: 
    app: nginx
    tier: dev 
spec:               
  containers:
  - name: nginx-container
    image: nginx
    ports:
    - containerPort: 80

The manifest file, also known as the podSpec file is provided to the cluster via kubectl. The podSpec file consists of mainly 4 fields:

  • apiVersion: This field specifies the version of kubernetes Api to which the object belongs. Pod belongs to v1 apiVersion.
  • kind: This field specify the type of object for which the manifest belongs to. Here, it is Pod.
  • metadata: This field includes the metadata for the object. It mainly includes two fields: name and labels.
  • spec: This field specifies the container, or list of containers which the pod will run. This field actually describes the desired state of our pod. In the above example, we are running a pod with one nginx container.

kubectl commands for pods

Discover the pod api object:

kubectl explain pods

Send the podSpec file for execution:

kubectl create -f nginx-pod.yml

Verify pod creation:

kubectl get pods/nginx-pod

Get running pods with labels:

kubectl get pods --show-labels

Retrieve pods with extra information like pod IP and node on which the pod is running:

kubectl get pods/nginx-pod -o wide

Retrieve complete definition of a running pod in yaml or json format:

kubectl get pods/nginx-pod -o yaml/json

Describe a running pod:

kubectl describe pods nginx-pod

Execute any command from the container:

kubectl exec nginx-pod hostname

Open the container’s shell in interactive mode:

kubectl exec -it nginx-pod sh

Get logs of a particular pod:

kubectl logs nginx-pod

Get log of a specific container in a multi-container pod:

kubectl logs nginx-pod -c container-name

Expose the pod port (80) to your localhost (8080) :

kubectl port-forward nginx-pod 8080:80

Delete a running pod:

Way 1: kubectl delete -f nginx-pod.yml
Way 2: kubectl delete pods nginx-pod

Delete all the pods running in the default namespace:

kubectl delete pods --all

After going through the contents, now you’ll be familiar with the concept of pods and various commands associated with them in kubernetes. For any queries, feel free to contact me at yatharth.sharma@knoldus.in.

Thank you for sticking to the end. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions on how I can improve my future posts to suit your needs. Follow me to get updates on different technologies.

Written by 

Yatharth Sharma is a Software Consultant at Knoldus Software LLP. He has done MCA from Bharati Vidyapeeth Institute of Computer Application and Management, Paschim Vihar. He has a decent knowledge of Java Language and currently working on DevOps technologies/tools like Ansible, Jenkins, Docker, Kubernetes. Apart from programming, he loves listening to rap music.