Sub topic 2
YAML essentials for Kubernetes
This chapter assumes you can use a terminal and edit text files. It also assumes you know what a Kubernetes Pod is. You need no earlier YAML experience. Most manifest errors come from a few syntax rules, so learning them now saves debugging time later.
The three building blocks
YAML describes data with three shapes: mappings, sequences, and scalars.
- A mapping is a set of
key: valuepairs. Kubernetes uses mappings for objects such asmetadata. - A sequence is a list. Each item starts with
-. - A scalar is a single value: a string, number, boolean, or null.
Nesting is shown by indentation. Use spaces only. Tabs are not allowed, and two spaces per level is the Kubernetes convention.
metadata:
name: web
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27
ports:
- containerPort: 80Read this top to bottom. metadata is a mapping with two keys. containers is a sequence with one item, and that item is a mapping with name, image, and ports. The ports key holds another sequence.
Sequences of mappings
The most common shape in manifests is a list of mappings. The dash marks the start of a new item. The keys that follow line up with the first key, not with the dash.
containers:
- name: web
image: nginx:1.27
- name: sidecar
image: busybox:1.36If image is indented differently from name, the parser either fails or attaches the key to the wrong object.
Strings, numbers, and quoting
YAML guesses a scalar's type from how it looks. That guess causes a few well-known surprises.
| You write | YAML reads it as | Fix if you wanted a string |
|---|---|---|
| `port: 80` | number | none needed |
| `tag: 1.27` | number 1.27 | `tag: "1.27"` |
| `enabled: yes` | boolean in YAML 1.1 parsers | `enabled: "yes"` |
| `value: 0123` | number | `value: "0123"` |
Kubernetes requires some fields to be strings. Label values and ConfigMap data values are examples. A label such as version: 1.27 is rejected because the value is a number. Quote it: version: "1.27". When in doubt, quote.
Multi-line text
A block scalar holds several lines in one value. A | keeps line breaks exactly as written. A > folds lines into one paragraph. ConfigMaps use | to store whole files.
data:
greeting.txt: |
Hello from a ConfigMap
Second lineComments and multiple documents
A # starts a comment that runs to the end of the line. A line containing only --- separates documents, so one file can hold several resources.
# A namespace and a ConfigMap in one file
apiVersion: v1
kind: Namespace
metadata:
name: demo
---
apiVersion: v1
kind: ConfigMap
metadata:
name: web-config
namespace: demo
data:
LOG_LEVEL: infoCheck your syntax
Syntax errors can be caught before they reach the cluster. Run a client-side dry run, which parses the file and sends nothing:
kubectl apply -f manifest.yaml --dry-run=clientA parse error names a line number. Look at that line and the one above it, since the real mistake is often just before the reported position.
Common errors
- Misaligned indentation, which moves a key to the wrong parent.
- A missing space after the colon.
name:webis one string, not a pair. - An unquoted value that YAML turns into a number or boolean.
- A missing
-in front of a list item.
Exercise
A manifest sets labels: { version: 1.27 } and the API server rejects it. Why, and how do you fix it?
Show answer
YAML reads 1.27 as a number, but label values must be strings. Quote the value: version: "1.27".