Creating and Managing Kubernetes Pods

Anatomy of a Pod

A Pod is the smallest deployable unit in Kubernetes and can contain one or more containers. Here’s a simple Pod YAML definition:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: nginx-container
    image: nginx:latest

Creating Pods

To create a Pod, you can use the kubectl apply command:

kubectl apply -f my-pod.yaml

Managing Pod Lifecycle

  • Starting: Once a Pod is created, it goes through various states to become Running.
  • Stopping: Delete a Pod with kubectl delete pod <POD_NAME>.

To save time during the exam you can use kubectl delete pod <POD_NAME> --grace-period=0 --force, and use kubectl replace -f pod.yaml --force to replace instead of delete and apply.

Scaling: Pods themselves are not scalable. Use Deployments or ReplicaSets for that.

Sometimes, you’ll need to delete a pod and recreate it again when you need to modify something that is immutable.

Pod Status and Health

You can check the Pod status using:

kubectl get pods

For health checks, you can use liveness and readiness probes.

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: nginx-container
    image: nginx:latest
    livenessProbe:
      httpGet:
        path: /
        port: 80
    readinessProbe:
      httpGet:
        path: /
        port: 80

Exercise: Creating and Managing a Pod

Setup

Create a file named exercise-pod.yaml with the following content:

apiVersion: v1
kind: Pod
metadata:
  name: exercise-pod
spec:
  containers:
  - name: busybox
    image: busybox
    command:
      - sleep
      - "3600"
Steps
  1. Create the Pod: kubectl apply -f exercise-pod.yaml
  2. Check the Pod status: kubectl get pods
  3. Delete the Pod: kubectl delete pod exercise-pod
Troubleshooting
  • If the Pod is not starting, describe it for clues: kubectl describe pod exercise-pod
  • Check logs: kubectl logs exercise-pod -c busybox
Solution

You should see the Pod go through states from Pending to Running. Deleting the Pod should remove it from the cluster.