The Rule of White Space
The most important thing to understand about YAML is that Indentation is everything.
In languages like Java or C++, you use curly braces {} to define blocks of code. In YAML, the number of spaces at the beginning of a line determines which "parent" a "child" belongs to.
No Tabs Allowed!
You must never use tabs in a YAML file. Most modern IDEs (like VS Code) will automatically convert your Tab key into two spaces, but if you accidentally insert a hard Tab character, the YAML parser will crash.
The "Colon Space" Rule
A common mistake for beginners is forgetting the space after the colon.
- ❌ WRONG:
name:GuideDevOps(The parser thinks this is one long string). - ✅ RIGHT:
name: GuideDevOps(The parser correctly identifies a Key and a Value).
Document Structure
1. The Three Dashes (---)
A YAML file can actually contain multiple separate documents in a single file. This is extremely common in Kubernetes (e.g., putting a Service and a Deployment in one file).
Each document starts with three dashes.
---
# Document 1
name: frontend
---
# Document 2
name: backend2. The Three Dots (...)
The three dots signify the end of a document. While optional and rarely used in DevOps, you might see them in complex data streams.
Handling Comments
Unlike JSON, YAML supports comments natively using the # symbol.
# This whole line is a comment
replicaCount: 3 # This comment explains why we need 3 podsCase Sensitivity
YAML is case-sensitive.
Active: True is not the same as active: true. Always stick to a consistent casing strategy (usually camelCase or snake_case) for your keys to avoid confusion.
Summary Checklist
- Use Spaces, not Tabs.
- Always put a Space after the Colon.
- Maintain consistent Indentation (usually 2 spaces).
- Use
---to separate multiple manifests in one file.