Certified Kubernetes Administrator (CKA) : Part-3: Containers

Background

This blog is a part of a mini-series for preparing for the CKA exam. This blog would focus on the concepts related to Containers.

How to read through the various blogs in this mini-series

Please feel free to go through all blogs (or) a particular blog, for a concept that You want to have a quick review. However, I would recommend going through the exam tips, as it may help You better organize Your time.

  1. Part-1 : Tips
  2. Part-2: Storage
  3. Part-3: Containers - You are reading this
  4. Part-4 to Part-8: would update the links here as I publish them

Environment

Examples are executed using Docker Desktop on Mac, with Kubernetes.

 % kubectl get nodes
NAME             STATUS   ROLES           AGE   VERSION
docker-desktop   Ready    control-plane   10d   v1.24.0

Containers and Workloads Usecases

A Pod is the smallest deployable unit of computing that could be created in Kubernetes - it contains one or more containers along with a shared network and storage.

  • Assigning CPU and memory resources to containers
  • Pod with an init container
  • Pod with multiple containers

Assigning CPU and memory resources to containers

  • Following is an example for requesting resources and specifying the CPU and memory limits. CPU is throttled as per limits and if the container tries to exceed memory limits, it becomes a candidate for termination
apiVersion: v1
kind: Pod
metadata:
  name: pod-resources-demo
spec:
  containers:
  - name: cpu-mem-demo
    image: nginx
    resources:
      limits:
        cpu: "1"
        memory: "200Mi"
      requests:
        cpu: "0.5"
        memory: "100Mi"
  • get the pod details and check
    % kubectl get pod pod-resources-demo -o yaml
    apiVersion: v1
    kind: Pod
    metadata:
    annotations:
      kubectl.kubernetes.io/last-applied-configuration: |
        {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"pod-resources-demo","namespace":"default"},"spec":{"containers":[{"image":"nginx","name":"cpu-mem-demo","resources":{"limits":{"cpu":"1","memory":"200Mi"},"requests":{"cpu":"0.5","memory":"100Mi"}}}]}}
    creationTimestamp: "2022-05-28T06:16:40Z"
    name: pod-resources-demo
    ...
    

Pod with an init container

  • A pod can have multiple containers running apps
  • It could also have one or more init containers that are executed before apps are started
  • In the example below the init container just sleeps for 60 seconds and the app container is only started after init container is finished
apiVersion: v1
kind: Pod
metadata:
  name: init-container-pod
spec:
  containers:
  - name: app
    image: busybox
    command: ['sh', '-c', 'echo The app is running! && sleep 3600']
  initContainers:
  - name: init
    image: busybox
    command: ['sh', '-c', 'echo Initializing! && sleep 60']
  • check the output that app container is not started before 60 seconds
 % kubectl get pods
NAME                 READY   STATUS     RESTARTS   AGE
init-container-pod   0/1     Init:0/1   0          46s
% kubectl logs init-container-pod
Defaulted container "app" out of: app, init (init)
Error from server (BadRequest): container "app" in pod "init-container-pod" is waiting to start: PodInitializing
  • check that app container is started after 60 seconds
% kubectl get pods
NAME                 READY   STATUS    RESTARTS   AGE
init-container-pod   1/1     Running   0          71s
 % kubectl logs init-container-pod -c init
Initializing!
% kubectl logs init-container-pod -c app 
The app is running!

Pod with an multiple containers

  • A pod can have multiple containers running apps
  • It could also have one or more init containers that are executed before apps are started
  • In the example below, one container is writing files locally and the other container exposes them to stdout
apiVersion: v1
kind: Pod
metadata:
  name: multi-container-pod
spec:
  containers:
  - name: app
    image: busybox
    command: ['sh', '-c', 'while true; do echo App is Logging >> /outputlogs/log.log; sleep 10; done']
    volumeMounts:
    - name: logs
      mountPath: /outputlogs
  - name: logoutput
    image: busybox
    command: ['sh', '-c', 'tail -f /inputlogs/log.log']
    volumeMounts:
    - name: logs
      mountPath: /inputlogs
  volumes:
  - name: logs
    emptyDir: {}
  • Check that pod contains multiple containers and that logs from app container are sent to stdout using logs container
 % kubectl get pod multi-container-pod                   
NAME                  READY   STATUS    RESTARTS   AGE
multi-container-pod   2/2     Running   0          15s
% kubectl logs -f multi-container-pod -c logoutput      
App is Logging
App is Logging
App is Logging
....

References