CodeBlanks Logo Icon CodeBlanks

Pod YAML

Learn the YAML structure for a Pod definition.

Pod YAML

Every Kubernetes object has four top-level fields: apiVersion, kind, metadata (name, labels) and spec (desired state). A pod's spec is a list of containers:

apiVersion: v1
kind: Pod
metadata:
  name: web
  labels:
    app: web
spec:
  containers:
    - name: nginx
      image: nginx:1.27
      ports:
        - containerPort: 80

containerPort is informational (like EXPOSE in a Dockerfile); it doesn't open traffic.

Command and arguments

A container's command can be changed in YAML: command replaces the image's ENTRYPOINT, and args its CMD:

spec:
  containers:
    - name: is
      image: busybox:1.36
      command: ["sh", "-c"]
      args: ["echo merhaba; sleep 3600"]

In YAML, lists are written either with square brackets (["a", "b"]) or with lines each starting with -.

Practice Write a pod definition

Write a pod YAML: name web, label app: web; a single container named nginx, image nginx:1.27, containerPort: 80.

Starter code

terminal
# buraya yaz

Sample solution

terminal
apiVersion: v1
kind: Pod
metadata:
  name: web
  labels:
    app: web
spec:
  containers:
    - name: nginx
      image: nginx:1.27
      ports:
        - containerPort: 80

Expected output

pod/web created

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the right apiVersion for a pod?

  • batch/v1
  • v1 Correct answer
  • apps/v1
  • pod/v1

Explanation: Pod, Service and ConfigMap are in the core group (v1); Deployment is in apps/v1.

2 Write the object's type.

Question
apiVersion: v1
____: Pod
Answer
apiVersion: v1
kind: Pod

Explanation: kind is the object's type.

3 Match the fields.

Term Meaning
apiVersion API group and version
kind Object type
metadata Name and labels
spec Desired state

All lessons in Intermediate