Skip to content
Production YAML
step 1/5

Reading — step 1 of 5

Read

~1 min readProduction

Production YAML

Real-world YAML usage:

K8s manifests:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  labels:
    app: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: app
          image: myapp:1.0
          ports:
            - containerPort: 8080

Validate with kubectl apply --dry-run=client -f file.yaml.

Helm templates: YAML with Go template directives ({{ .Values.foo }}).

Ansible playbooks:

- name: Install web server
  hosts: webservers
  tasks:
    - name: Install nginx
      apt:
        name: nginx
        state: present

GitHub Actions:

name: CI
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm test

Best practices:

  1. Use YAML 1.2: avoid Norway problem + octal.
  2. Always quote strings that look like other types: zip codes, versions, country codes.
  3. Use 2 spaces for indent: K8s + most communities.
  4. Lint in CI: yamllint or kubeval.
  5. Schema-validate: many tools have JSON Schema for their YAML.
  6. Avoid anchors in shared CI: not all CIs support.
  7. Comment liberally: YAML supports them; explain non-obvious parts.
  8. Don't pretty-print machine-generated: makes diffs noisier.
  9. Document file structure: README explaining schema.
  10. Test rendering: especially for templated YAML (Helm), render and inspect.

Generators:

  • yq: jq-style query/edit.
  • yamllint: style + correctness.
  • envsubst (with caveats): substitute env vars.
  • Helm: K8s templating.
  • Kustomize: K8s overlay system.
  • Ansible Vault: encrypt parts of YAML.

Editors:

  • VS Code with Kubernetes extension: completion + validation.
  • IntelliJ + plugin: same.
  • vim/nvim with YAML plugins.

Don't roll your own YAML parser unless toy. Use:

  • Python: ruamel.yaml (round-trip + comments).
  • Go: go-yaml/yaml.v3.
  • JS: js-yaml.
  • Java: snakeyaml.

The spec is full of corners; mature parsers handle them.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…