Certified Kubernetes Administrator - Overview of Pods


A pod is a  group of one or more containers with shared resources that run on the same host. To accurately understand the concept, we need to talk about what is the container and how it works.

Containers are another form of virtualization. Virtualization solutions like  Citrix XenServer, VMware vSphere, Microsoft Hyper-V, Red Hat KVM, etc. allow sharing a piece of hardware to multiple systems by a custom defined process named hypervisor. Containers aim to virtualize the Operating System to run the application by standardizing the process, keeping all dependencies together. Basically,  container is a method to package an application that runs any platform. 

Docker still reigns with  %83  of the container according to sysdig.com. But it was %99 in 2017. It seems that  Apache Mesos, CoreOs RKT, Linux Containers LXC are steadily growing to become a key player.

A Pod is the smallest and simplest unit in the Kubernetes that you create or deploy. Pod operates one level higher than the container. For example; an application pod contains separated containers like application containers, logging containers, monitoring containers. Each container has its standards and processes but all of them are working in a box called application Pod. You can read more information from Kubernetes Documentation.

 

Pod Templates

Pod template is a definition file contains pod specifications that can be part of other objects like deployment, replicaset, daemonset, etc. Controller uses pod templates to create an application pod. Pod template has four main objects that apiVersion, kind, metadata, spec. I added a basic pod template that uses busybox image and runs the command "echo Hello Kubernetes!"  then sleeps  3600 seconds. 

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: busybox
    command: ['sh', '-c', 'echo Hello Kubernetes! && sleep 3600']

On the other hand, I strongly advise using kubectl command to generate a pod template. Kubectl has "--generator" option that you can easily create a template. Check for more detail from Kubernetes Documentation.

# kubectl  run  --generator=run-pod/v1  --image=nginx  nginx  --dry-run -o yaml        
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  containers:
  - image: nginx
    imagePullPolicy: IfNotPresent
    name: nginx
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

Time to test your skills. Use  Katacoda and find out the answers.

 

Question 1: Create a namespace called 'casesup' from YAML file and run nginx pod in casesup namespace.

#kubectl  create  namespace  casesup  --dry-run  -o yaml
apiVersion: v1
kind: Namespace
metadata:
  creationTimestamp: null
  name: casesup
spec: {}
status: {}
#kubectl  create  namespace  casesup
namespace/casesup created
#kubectl run --generator=run-pod/v1  --image=nginx  nginx  --namespace=casesup
#kubectl  get  pods -n casesup
NAME    READY   STATUS              RESTARTS   AGE
nginx   0/1     ContainerCreating   0          6s

 

Question 2: Change nginx pod image from nginx to busybox.

#kubectl  edit  pod nginx  -n casesup
--change  image name under  containers  section.
spec:
  containers:
  - image: busybox
#kubectl  get  pods -n casesup
NAME    READY   STATUS             RESTARTS   AGE
nginx   0/1     CrashLoopBackOff   6          5m47s

Question 3: Check the logs to figure out why pod state is "CrashLoopBackOff"

#kubectl  describe pod  nginx  -n casesup
Name:         nginx
Namespace:    casesup
Priority:     0
Node:         minikube/172.17.0.31
Start Time:   Tue, 07 Jan 2020 21:10:00 +0000
Labels:       run=nginx
Annotations:  
Status:       Running
IP:           172.18.0.6IPs:
  IP:  172.18.0.6
Containers:
  nginx:    Container ID:   docker://b948bc1a8c2bc7d7d682ba4e7de045a4cf690a98e846ef80300cd15510c24392
    Image:          busybox
    Image ID:       docker-pullable://busybox@sha256:6915be4043561d64e0ab0f8f098dc2ac48e077fe23f488ac24b665166898115a
    Port:           
    Host Port:          State:          Waiting
      Reason:       CrashLoopBackOff    Last State:     Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Tue, 07 Jan 2020 21:16:54 +0000
      Finished:     Tue, 07 Jan 2020 21:16:54 +0000
    Ready:          False
    Restart Count:  7
    Environment:    
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-mbjlp (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             False
  ContainersReady   False
  PodScheduled      True
Volumes:
  default-token-mbjlp:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-mbjlp
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason     Age                     From               Message
  ----     ------     ----                    ----               -------
  Normal   Scheduled  8m2s                    default-scheduler  Successfully assigned casesup/nginx to minikube
  Normal   Pulling    8m1s                    kubelet, minikube  Pulling image "nginx"
  Normal   Pulled     7m56s                   kubelet, minikube  Successfully pulled image "nginx"
  Normal   Killing    6m55s                   kubelet, minikube  Container nginx definition changed, will be restarted
  Normal   Started    6m35s (x4 over 7m55s)   kubelet, minikube  Started container nginx
  Normal   Pulling    6m8s (x4 over 6m55s)    kubelet, minikube  Pulling image "busybox"
  Normal   Created    6m6s (x5 over 7m55s)    kubelet, minikube  Created container nginx
  Normal   Pulled     6m6s (x4 over 6m53s)    kubelet, minikube  Successfully pulled image "busybox"
  Warning  BackOff    2m58s (x20 over 6m50s)  kubelet, minikube  Back-off restarting failed container	

Question 4: Remove nginx pod and create busybox pod with busybox image. Figure out busybox CrashLoopBackOff state.

**When you use busybox image;The pod is crashing because it starts up then immediately exits, it means that your  pod starts and finishes the job that you defined in pod definition file. So next time we will add a command to keep it alive. 
#kubectl run --generator=run-pod/v1  --image=busybox  busybox  --dry-run -o yaml >  /tmp/busybox.yaml
You need to add "namespace" and "command" attribute.
#cat /tmp/busybox.yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: busybox
  name: busybox
  namespace: casesup
spec:
  containers:
  - image: busybox
    name: busybox
    command: ['/bin/sh',  '-c', 'echo Wait && sleep 36000 ']
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}
#kubectl  create  -f /tmp/busybox.yaml
pod/busybox created
#kubectl  get  pods  -n casesup
NAME      READY   STATUS    RESTARTS   AGE
busybox   1/1     Running   0          7s 

Question 5: Get a session to the busybox pod.

#kubectl  exec   busybox -n casesup -it --  /bin/sh
/ # hostname
busybox
/ #

Question 6: Get Pods IP and node information.

#kubectl  get  pods  -n casesup -o wide
NAME      READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES
busybox   1/1     Running   0          10m   172.18.0.6   minikube              

Question 7: How to get Pods logs.

#kubectl  logs  busybox  -n casesup
Wait
$

Question 8: Delete busybox pod.

#kubectl  delete  pod  busybox  -n casesup
pod "busybox" deleted

I'm a IT Infrastructure and Operations Architect with extensive experience and administration skills and works for Turk Telekom. I provide hardware and software support for the IT Infrastructure and Operations tasks.

205 Total Posts
Follow Me

Related Post