H 142: Managing Pods (15 pts)

What You Need for this Project

Purpose

To set up a Kubernetes cluster and deploy a containerized application.

I am following this tutorial: Kubernetes Bootcamp (CKAD) .

Preparation

First do the previous project, "Hello Kubernetes".

Starting Minikube

On your Linux server, execute these commands:

minikube start
Your cluster starts, showing several messages, ending with "Done! kubectl is now configured...", as shown below.

Troubleshooting

If minkube just hangs and won't start, press Ctrl+C and execute these commands:

minikube delete
minikube start

Examining minikube Status

On your Linux server, execute these commands:

minikube status
You should see several "Running" items, as shown below.

Create a Pod with the nginx:alpine Image

Execute these commands:

kubectl get all
kubectl run mypod --image=nginx:alpine
kubectl get all
The new pod appears, as shown below:

Viewing Pod Information

Execute this command:

kubectl describe pods mypod | less
The first page of output has basic information about your pod, in a human-readable format, as shown below:

Press the SPACEBAR a few times to move to the end of the output.

You see a list of events affecting this pod, as shown below:

Press q to exit from "less."

Viewing a Pod Manifest File

A manifest file describes the desired state of a pod. Let's look at the manifest file for the pod we just created.

Execute this command:


kubectl get pod mypod -o yaml | less
You see information about your pod in YAML format, which is machine-readable.

Like Python, whitespace matters in the YAML format. Items are indented to indicate what sections they belong to.

The "metadata" section is highlighted in the image below:

After the "metadata" section comes the "status" section, visible in the image above.

Press the SPACEBAR a few times to move to the "status" section, as shown below:

There are three main sections in a Manifest file:

Deleting the Pod

Execute these commands:

kubectl get pods
kubectl delete pod mypod
kubectl get pods
The pod is gone, as shown below:

Generating a Manifest File

You can use the "kubectl run" command with the "--dryrun" switch to create a manifest file, without actually creating the pod.

We'll create a manifest file named "sleeper.yaml", based on the Busybox image, that sleeps for 10 seconds and then stops.

Execute these commands:


kubectl run sleep-pod --image=busybox --dry-run=client -o yaml --command -- sleep 10 > sleeper.yaml
ls -l
kubectl get pods
A file named sleeper.yaml appears, but no pod is created, as shown below:

Execute this command:


cat sleeper.yaml
The file is quite simple, as shown below. It specifies that the pod will use the "busybox" image, and run the "sleep" command with the argument "10".

Notice the restartPolicy of "Always."

Creating a Pod from a Manifest File

Execute these commands:

kubectl apply --filename=sleeper.yaml
kubectl get pods
The pod is created, as shown below.

Wait about ten seconds, and then repeat the "kubectl get pods" command.

Then wait 30 seconds, and repeat it again.

Then wait 60 seconds and repeat it again.

You'll see a STATUS of "CrashLoopBackOff", as shown below.

This pod starts, waits ten seconds, and then stops.

But since the manifest file says to always restart, it automatically starts again.

Kubernetes notices this behavior and labels it a "crash loop", so it delays the restarts in an increasing pattern of time intervals, to reduce the resources consumed by this pod.

Deleting the Crashing Pod

Execute these commands:

kubectl delete pod sleep-pod
kubectl get pods
The pod is gone, as shown below.

Editing the Manifest File

Execute this command:

nano sleeper.yaml
Change the restartPolicy to Never, as shown below.

Save the file with Ctrl+x, y, Enter.

Creating a Pod from the Manifest File

Execute these commands:

kubectl apply --filename=sleeper.yaml
kubectl get pods
The pod is created, as shown below.

Wait about ten seconds, and then repeat the "kubectl get pods" command.

Then wait 20 seconds, and repeat it again.

The pod has a STATUS of "Completed", and zero RESTARTS, as shown below.

This pod doesn't have the "crash loop" problem.

Flag H 142.1: READY (10 pts)

The flag is covered by a green rectangle in the image above.

Cleaning Up

Execute these commands:

kubectl delete pod sleep-pod
kubectl get pods
The pod is gone, as shown below.

Init Containers

A Pod can have multiple containers running apps within it, but it can also have one or more init containers, which are run before the app containers are started.

Init containers are exactly like regular containers, except:

Generating a Manifest

We'll create a pod based on busybox that senda a message to stdout, which will end up the the log.

Execute these commands:


kubectl run myapp --image=busybox --restart=Never \
  --dry-run=client -o yaml --command echo HELLO_FROM_ECHOPOD > echoer.yaml

kubectl apply --filename=echoer.yaml
kubectl get pods
kubectl logs myapp 
The pod runs, and stops immediately, with status "Completed". The message appears in the log, as shown below.

Adding an Init Container

Execute this command:

nano echoer.yaml
Add these lines, as shown below.

  initContainers:
  - command:
    - echo
    - HELLO_FROM_INIT
    image: busybox
    name: myapp-init
    resources: {}

Save the file with Ctrl+x, y, Enter.

To delete the old pod and recreate it with the Init container, execute these commands:


kubectl delete pod myapp
kubectl apply --filename=echoer.yaml
kubectl get pods
kubectl logs myapp --all-containers=true
Both log messages appear, showing that the Init container worked, as shown below.

Flag H 142.2: Events (5 pts)

Execute this command:

kubectl describe pods myapp
The flag is covered by a green rectangle in the image below.

Cleaning Up

Execute these commands:

kubectl delete pod myapp
kubectl get pods
The pod is gone, as shown below.

References

Kubernetes Bootcamp (CKAD)

Posted 3-10-25
Updated with exercises after the flag 3-12-25