Kubernetes Pods: An Introduction

Ajeet Rai
5 min readOct 8, 2020

What is a K8s pod?

The pod is the smallest building block. Within a cluster, a pod represents a process that’s running.

When you created a Deployment,Kubernetes creates a Pod to host your application instance. A Pod is a Kubernetes abstraction that represents a group of one or more application containers (such as Docker), and some shared resources for those containers. Those resources include:

  • Shared storage, as Volumes
  • Networking, as a unique cluster IP address
  • Information about how to run each container, such as the container image version or specific ports to use.
Pod Overview

The inside of a pod can have one or more containers. Those within a single pod share:

  • A unique network IP
  • Network
  • Storage
  • Any additional specifications you’ve applied to the pod

Nodes

A Pod always runs on a Node. A Node is a worker machine in Kubernetes and may be either a virtual or a physical machine, depending on the cluster. Each Node is managed by the Master. A Node can have multiple pods, and the Kubernetes master automatically handles scheduling the pods across the Nodes in the cluster.

Pods and controllers

You can use workload resources to create and manage multiple Pods for you. A controller for the resource handles replication and rollout and automatic healing in case of Pod failure. For example, if a Node fails, a controller notices that Pods on that Node have stopped working and creates a replacement Pod. The scheduler places the replacement Pod onto a healthy Node.

Here are some examples of workload resources that manage one or more Pods:

Pod model types

There are two model types of pod you can create:

  • One-container-per-pod
  • Multi-container-pod

Each pod runs a single instance of your application. If you need to scale the app horizontally (such as running several replicas), you can use a pod per instance. This is different from running multiple containers of the same app within a single pod.

If a node fails or if you’re maintaining nodes, the pods won’t survive. To solve this issue, K8S has controllers — typically, a pod can be created with a type of controller.

we will discuss controllers later.

Pod lifecycle

A pod status tells us where the pod is in its lifecycle. It is meant to give you an idea not for certain, therefore It is good practice to debug if pod does not come up cleanly. The five phases of a pod lifecycle are:

  1. Pending. The pod is accepted, but at least one container image has not been created.
  2. Running. The pod is bound to a node, and all containers are created. One container is running or in the process of starting or restarting.
  3. Succeeded.
  4. Failed
  5. Unknown. The state of the pod couldn’t be obtained.

The manifest (YAML)

We will break the manifest down into four parts:

  • ApiVersion — Version of the Kubernetes API you’re using
  • Kind — Kind of object you want to create
  • Metadata — Information that uniquely identifies the object, such as name or namespace.
  • Spec — Specified configuration of our pod, for example image name, container name, volumes, etc.

ApiVersion, kind, and metadata are required fields that are applicable to all Kubernetes objects, not just pods. The layout of spec, which is also required, varies across objects. The example manifest shown above shows what a single container pod spec looks like.

apiVersion: "api version"              (1)
kind: "object to create" (2)
Metadata: (3)
Name: "Pod name"
labels:
App: "label value"
Spec: (4)
containers:
- name: "container name"
image: "image to use for container"

Single container pod

Our pod-1.yaml is the manifest for our single container pod. It runs an nginx pod .

apiVersion: v1
kind: Pod
metadata:
name: firstpod
labels:
app: myapp
spec:
containers:
- name: my-first-pod
image: nginx

we will deploy this manifest into our local Kubernetes cluster by running Kubectl create -f pod-1.yaml. Then we run “kubectl get pods” to confirm that our pod is running as expected.

kubectl get pod
NAME READY STATUS RESTARTS AGE
firstpod 1/1 Running 0 45s

To confirm it’s actually running, run kubectl exec firstpod — kubeconfig=kubeconfig — service nginx status. This runs a command inside our pod by passing in — service nginx status.

kubectl exec firstpod — service nginx status
nginx is running.

Now clean up by running kubectl delete pod firstpod

kubectl delete pod firstpod
pod "firstpod" deleted

Multi-container manifest

we will deploy: a pod with multiple containers that work as a single entity. One container writes the current date to a file every 10 seconds while the other container serves the logs for us.

Go ahead and deploy the pod-2.yaml manifest with kubectl create -f pod-2.yaml.

apiVersion: v1
kind: Pod
metadata:
name: multi-container-pod # Name of our pod
spec:
volumes:
- name: shared-date-logs # Creating a shared volume for my containers
emptyDir: {}
containers:
- name: container-writing-dates # Name of first container
image: alpine # Image to use for first container
command: ["/bin/sh"]
args: ["-c", "while true; do date >> /var/log/output.txt; sleep 10;done"] # writing date every 10secs
volumeMounts:
- name: shared-date-logs
mountPath: /var/log # Mounting log dir so app can write to it.
- name: container-serving-dates # Name of second container
image: nginx:1.7.9 # Image for second container
ports:
- containerPort: 80 # Defining what port to use.
volumeMounts:
- name: shared-date-logs
mountPath: /usr/share/nginx/html # Where nginx will serve the written file
kubectl create -f pod-2.yaml
pod "multi-container-pod" created

Then we confirm that it’s really deployed.

kubectl get pod --kubeconfig=kubeconfig 
NAME READY STATUS RESTARTS AGE
multi-container-pod 2/2 Running 0 1m

Check to make sure two containers are in our pod by running kubectl describe pod “pod name”.

Connect to the container by running kubectl exec -ti multi-container-pod -c container-serving-dates — kubeconfig=kubeconfig bash. Now we are inside the container.

Now curl ‘http://localhost:80/app.txt'.

we will get the desired output.

This is the example of how multi-contaner manifest works.

Thank you….

--

--