# Create, View and Destroy a Pod

In 
Published 2022-12-03

This tutorial explains to you how to create, view and destroy a pod in a Kubernetes cluster.

# Pods - explanation

Things to retain:

  • Pods are a layer of abstraction over containers.
  • In K8s you cannot deploy containers directly. You deploy pods which contains one or more containers.
  • Pods don't run anything it is just a sandbox for containers
  • Pods are collections of containers that share the same resources (memory, volumes, IPs etc) and local network. This enables easy communication between containers in a pod.
  • The containers in a pod can use localhost interface for container-to-container communication.
  • The simple model is to deploy 1 container per Pod. The multi-container Pods are used when the containers . work very tightly (in service mesh, for instance).
  • If you want to scale up, you need to deploy another pod containing that container.
  • All containers in a pod are deployed on the same machine (node).
  • When a pod fails, a new one is created (with a different IP and new ID).

When we create a pod, we generally add some labels to that pod. The labels add more information regarding what is running in the pods, on which environment, etc.

Example labels:

  • "release" : "stable" or "release" : "canary"
  • "environment" : "dev" or "environment" : "qa" or "environment" : "production"
  • "tier" : "frontend" or "tier" : "backend" or "tier" : "cache"

# Create a Pod in Kubernetes

In order to create a pod, we need to create a file as my-first-pod.yml which describes the pod we want to create:

my-first-pod.yml
apiVersion: apps/v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    zone: prod
    version: v1
spec:
  containers:
  - name: nginx-c
    image: nginx:1.14.2
    ports:
    - containerPort: 8080

This file defines a pod which contains one nginx container.

The following command will create a new pod:

kubectl apply -f my-first-pod.yml

The Imperative way of creating a pod is by running the kubectl run command:

kubectl run nginx-pod --image=nginx:1.14.2 --port=8080 --labels="zone=prod,version=v1"

# View Pod information

We can run the following command to see the result:

kubectl get pods --show-labels

NAME        READY   STATUS    RESTARTS   AGE     LABELS
nginx-pod   1/1     Running   0          2m37s   version=v1,zone=prod

Get more information on a specific pod:

kubectl describe pod nginx-pod

In my case the result is:

Name:             nginx-pod
Namespace:        default
Priority:         0
Service Account:  default
Node:             minikube/192.168.49.2
Start Time:       Tue, 31 Jan 2023 20:58:46 +0200
Labels:           version=v1
                  zone=prod
Annotations:      <none>
Status:           Running
IP:               10.244.0.4
IPs:
  IP:  10.244.0.4
Containers:
  nginx-c:
    Container ID:   docker://9d8b8e5616ab36e895886732af87f7a8b221a884d3724d58ded0605dd9615a23
    Image:          nginx:1.14.2
    Image ID:       docker-pullable://nginx@sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
    Port:           8080/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Tue, 31 Jan 2023 20:58:58 +0200
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-6dph8 (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  kube-api-access-6dph8:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age    From               Message
  ----    ------     ----   ----               -------
  Normal  Scheduled  6m46s  default-scheduler  Successfully assigned default/nginx-pod to minikube
  Normal  Pulling    6m45s  kubelet            Pulling image "nginx:1.14.2"
  Normal  Pulled     6m36s  kubelet            Successfully pulled image "nginx:1.14.2" in 9.685379s (9.6854052s including waiting)
  Normal  Created    6m34s  kubelet            Created container nginx-c
  Normal  Started    6m34s  kubelet            Started container nginx-c

# Delete Pods in Kubernetes

Delete the Pods created above:

kubectl delete pod nginx-pod