YAML: The Heart of DevOps
Now that you've mastered the syntax, let's look at how the top DevOps tools actually use YAML in practice. This will help you recognize the patterns in the wild.
1. Kubernetes Manifests
Kubernetes is almost entirely managed via YAML. Every Resource (Pod, Deployment, Service) is a Mapping.
apiVersion: apps/v1
kind: Deployment # The "Type" of resource
metadata:
name: nginx-webapp
spec: # The "Specification"
replicas: 3
selector:
matchLabels:
app: nginx
template: # A nested template for the Pod
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.21
ports:
- containerPort: 802. Docker Compose
Docker Compose uses YAML to define multi-container local environments.
version: '3.8'
services:
database:
image: postgres:13-alpine
environment:
POSTGRES_PASSWORD: ${DB_PASS} # Uses env variable interpolation
volumes:
- db-data:/var/lib/postgresql/data
web:
build: .
ports:
- "5000:5000"
depends_on:
- database
volumes:
db-data:3. GitHub Actions (CI/CD)
CI/CD pipelines use YAML to define "Events" and "Jobs."
name: CI Pipeline
on: [push] # An Event (List style)
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v2
- name: Run Tests
run: | # Literal Block for multiple command strings
npm install
npm testBest Practices Checklist
When working with YAML in the real world:
- Use a Linter: Tools like
yamllintor VS Code's YAML extension will catch indentation errors before you deploy. - Quote your Booleans: Sometimes tools expect a string but find a boolean (e.g.,
active: "true"). - Comment your Code: Use
#to define why you are making specific config choices. - Prefer 2-Space Indentation: While 4 spaces are valid, 2 spaces are the industry standard for readability in deep hierarchies.
- Validate Schema: Tools often have specific schemas (like Kubernetes CRDs). Valid YAML syntax doesn't always mean valid tool configuration!