๐Ÿ“ŠData Engineering

YAML

YAML Ain't Markup Language

A human-readable data serialization format. Widely used for configuration files and K8s manifests.

Detailed Explanation

YAML (YAML Ain't Markup Language) is a human-readable and easy-to-write data serialization format. Because it expresses structure through indentation, it is more readable than JSON, and because it supports comments, it is well suited to configuration files. It has become the standard configuration format for DevOps tools such as Kubernetes, Docker Compose, Ansible, and GitHub Actions.

YAML supports three basic types: scalars (strings, numbers, booleans), sequences (arrays), and mappings (objects). Anchors (&) and aliases (*) let you reference duplicate data to apply the DRY (Don't Repeat Yourself) principle, and multiple documents can be placed in a single file by separating them with ---.

YAML 1.2 is a superset of JSON, so every valid JSON document is valid YAML. However, there are points to watch out for, such as indentation errors, the ban on tabs, and implicit type conversion (yes/no being interpreted as booleans). Tools such as yamllint and yaml-language-server help prevent syntax errors.

For managing complex configurations, Helm (Kubernetes) and Kustomize provide YAML templating and overlay features. Most languages offer parser libraries, such as PyYAML and ruamel.yaml for Python and js-yaml for JavaScript.

Code Example

# YAML ์•ต์ปค/๋ณ„์นญ ๋ฐ Kubernetes ์„ค์ • ์˜ˆ์ œ

# ========== ์•ต์ปค์™€ ๋ณ„์นญ์œผ๋กœ ์ค‘๋ณต ์ œ๊ฑฐ ==========
defaults: &defaults
  adapter: postgres
  host: localhost
  pool: 5

development:
  <<: *defaults      # defaults ๋ณ‘ํ•ฉ
  database: myapp_dev

production:
  <<: *defaults      # defaults ๋ณ‘ํ•ฉ
  host: db.production.com
  database: myapp_prod
  pool: 25

# ========== Kubernetes Deployment ๋งค๋‹ˆํŽ˜์ŠคํŠธ ==========
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-server
  namespace: production
  labels: &app-labels    # ์•ต์ปค ์ •์˜
    app: api-server
    environment: production
    version: v2.1.0
spec:
  replicas: 3
  selector:
    matchLabels:
      <<: *app-labels    # ๋ณ„์นญ์œผ๋กœ ์žฌ์‚ฌ์šฉ
  template:
    metadata:
      labels:
        <<: *app-labels
    spec:
      containers:
        - name: api
          image: myregistry/api-server:v2.1.0
          ports:
            - containerPort: 8080
              protocol: TCP
          env:
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: db-credentials
                  key: url
          resources:
            requests:
              memory: "256Mi"
              cpu: "250m"
            limits:
              memory: "512Mi"
              cpu: "500m"

How It's Said in Practice

Senior: "In a Helm chart's values.yaml, if you pull the common settings out into an anchor and override them per environment, you can reduce duplication."

Junior: "What's an anchor?"

Senior: "You define it with &defaults and reference it with *defaults. If you use <<: *defaults, you can merge as well."

Interviewer: "What are the differences between YAML and JSON, and in which situations is each used?"

Candidate: "YAML has the advantages of comment support, readability, and anchors/aliases, so it suits configuration files. JSON is fast to parse and strict, so it suits API communication."

Take a mock interview on this topic โ†’ WIA SOOM AI Interview ยท Start for free

Senior: "Here, on: true is parsed as the boolean true, not the string 'on'. To write it as a string, wrap it in quotes like 'on'."

Junior: "Ah, so that's why the CI didn't run. I'll add the quotes."

Cautions

Learn More