Chapter 8 of 47 · Pods in depth, part 4

Writing your first Pod

Time to write YAML. One small file, every line explained, then the five kubectl commands you will use on Pods every day.

The Pod file, line by line

Create pod.yaml with this content. Click any highlighted part to see what it means:

apiVersion: v1
kind: Pod

metadata:
  name: posts-pod

spec:
  containers:
    - name: posts
      image: micro-posts:1.0
      ports:
        - containerPort: 4000

apiVersion: v1

Which version of the Kubernetes API this object uses. Pods, Services and ConfigMaps are in the core group, v1. You will see apps/v1 for Deployments. Get it wrong and kubectl refuses the file.

Pod name versus container namePod · metadata.name = posts-podContainer · name = postsimage = micro-posts:1.0

The shape to remember

Do not memorise every option. Remember the skeleton and what each part answers:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: my-container
      image: my-image
apiVersion → which Kubernetes API?
kind       → what am I creating?
metadata   → what is it called?
spec       → what should it look like?
containers → what runs inside it?
image      → which image?

Environment variables, resource limits, probes, volumes and more containers all get added inside this same shape later.

Create and inspect it

Click through the commands in order. Output is from a typical cluster; names and times will differ.

$ kubectl apply -f pod.yaml
pod/posts-pod created

Send the file to Kubernetes: "this is the Pod I want". Run it again unchanged and you get pod/posts-pod unchanged.

Two to remember: kubectl get pods for a quick overview, kubectl describe pod posts-pod for the detail.

What READY 1/1 means

READY is ready containers / total containers in the Pod.

NAME        READY   STATUS
posts-pod   1/1     Running

Pod └── posts ✓ — 1 of 1 containers ready.

The local image trap

You built micro-posts on your laptop. The cluster's nodes may not be able to see your local Docker images, and the Pod gets stuck in ErrImagePull. Two fixes, depending on your setup:

Local clusterGet your image into it
kindkind load docker-image micro-posts:1.0
minikubeminikube image load micro-posts:1.0
Docker DesktopUsually sees images you built locally already
A real clusterPush to a registry (Docker Hub, GHCR, ECR…) and use the full name
Use a tag. With image: micro-posts (no tag, meaning :latest), Kubernetes defaults to imagePullPolicy: Always and tries to download it from Docker Hub, even though it is already loaded. micro-posts:1.0 defaults to IfNotPresent and uses the local copy.

Docker → Kubernetes

You learned Docker first. Pick the Kubernetes equivalent:

DockerKubernetes
ImageImage (the same one)
ContainerContainer inside a Pod
docker runPods created from YAML, usually by a Deployment
docker logskubectl logs
docker execkubectl exec
Docker networkCluster network, every Pod has an IP
Port mapping -pService
Restart by handControllers maintain the desired state
Docker:      Image → Container
Kubernetes:  Image → Container → Pod → Deployment → Service

Recap

Next: the objects that manage Pods for you. ReplicaSet first, then Deployment, then Service.

Pod YAML: common questions

What does READY 1/1 mean in kubectl get pods?

Ready containers out of total containers in the Pod. 1/1 means its one container is ready; 1/2 means one of two containers is not ready yet.

Does containerPort expose my app?

No. containerPort documents the port the app listens on. To reach the app from outside the Pod you create a Service.