Write a YAML & Interact With A New Pod

YAML is today the default language becauase its a human readable way to write structured data. I spend some time understanding how to write yaml playbooks so hope this helps someone.

The Container Structure — Memorise This

These are all siblings and they all belong to the container so they sit at the same level.

containers:
- name:           # container name
  image:          # image
  ports:          # list of ports
  env:            # list of env vars
  resources:      # cpu/memory limits
  volumeMounts:   # where to mount volumes

When to Use Quotes

You only need quotes when the value contains special characters:

SituationQuote?Example
Colon in valueYes"nginx:latest"
IP addressYes"10.0.0.1"
URLYes"http://10.0.0.1:8080"
Number as stringYes"5432"
Memory / CPUYes"128Mi", "250m"
Port numbersNocontainerPort: 8080
ReplicasNoreplicas: 2
Plain wordsNodefault, postgres

Indentation

Indentation is one area most begineers get caught , this is always about relationships – 2 spaces, always and never tabs

Instead of writing YAML from scratch, generate the skeleton with kubectl

❯ k run --help
# Check what a pod manifest looks like
kubectl run nginx --image=nginx --dry-run=client -o yaml 

This is how most people write YAML in real life – generate, edit, apply.

# Generate a deployment manifest
kubectl apply deployment myapp --image=nginx --dry-run=client -o yaml > deployment.yaml

Make some changes to the file and create a pod by running

# Apply for real
kubectl apply -f yourfile.yaml

Run k get pods and k describe to check the state of our new pod

# k get pods 
# k describe pod <podname>

To interact with the pod check the Ip addresss by running

# Show IP 
kubectl get pods -o wide
# Interact with a pod
kubectl exec -ti <podname> --/bin/bash

Add an entry into the ~/.bashrc so we can just run kexec pod so If bash isn’t available in the container (some minimal images use sh):

kexec() {
kubectl exec -it $1 -- /bin/bash 2>/dev/null || kubectl exec -it $1 -- /bin/sh
} 

awk k3s-cp-01 ~ ❯ k get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginxash 1/1 Running 0 10h 10.42.0.81 k3s-cp-01
nginxash4 1/1 Running 0 11h 10.42.1.163 k3s-worker-02
webserver 1/1 Running 0 10h 10.42.1.164 k3s-worker-02

awk k3s-cp-01 ~ ❯

awk k3s-cp-01 ~ ❯ kexec nginxash
root@nginxash:/#

Indentation Tools

Few tools i found in VScode that can help but always remember for the cka exam we must only use VI so that’s the real skill you need to master with writing yaml properly than relying on AI tools.

  • | VS Code + Red Hat YAML | Flags syntax errors as you type |
  • | Prettier | Auto-formats indentation |
  • Codeium | AI autocomplete for YAML structure |
  • | kubectl dry-run | Validates against Kubernetes schema |

(Visited 1 times, 1 visits today)

By Ash Thomas

Ash Thomas is a seasoned IT professional with extensive experience as a technical expert, complemented by a keen interest in blockchain technology.