YAML untuk DevOps: Docker Compose, Kubernetes, dan CI/CD
Di dunia DevOps, YAML praktis menjadi bahasa universal untuk mendeskripsikan infrastruktur dan automasi. Berikut tiga konteks paling umum.
1. Docker Compose
version: "3.9"
services:
web:
image: nginx:latest
ports:
- "80:80"
api:
build: ./api
environment:
- NODE_ENV=production
depends_on:
- db
db:
image: postgres:16
Setiap service didefinisikan sebagai key di bawah services, dengan konfigurasi image, port, environment variable, dan dependensi antar service.
2. Kubernetes Manifest
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
replicas: 3
selector:
matchLabels:
app: api
template:
metadata:
labels:
app: api
spec:
containers:
- name: api
image: myregistry/api:1.4.0
ports:
- containerPort: 8080
Kubernetes sangat bergantung pada YAML untuk mendefinisikan Deployment, Service, ConfigMap, Secret, dan resource lain. Kesalahan indentasi sekecil apa pun bisa membuat kubectl apply gagal total.
3. CI/CD Pipeline (GitHub Actions)
name: CI
on: [push]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm install
- run: npm test
Tips Menghindari Error YAML di Production
- Selalu gunakan linter YAML (misalnya
yamllint) sebelum commit. - Jangan campur tab dan spasi — set editor untuk otomatis konversi tab jadi 2 spasi.
- Quote string yang mengandung karakter spesial seperti
:,#, atau nilai seperti"yes"/"no"untuk hindari Norway Problem. - Validasi struktur sebelum deploy — paste manifest ke JSONYAMify untuk cek apakah YAML valid dan lihat strukturnya dalam Tree View.
YAML for DevOps: Docker Compose, Kubernetes, and CI/CD
In the DevOps world, YAML has practically become the universal language for describing infrastructure and automation. Here are the three most common contexts.
1. Docker Compose
version: "3.9"
services:
web:
image: nginx:latest
ports:
- "80:80"
api:
build: ./api
environment:
- NODE_ENV=production
depends_on:
- db
db:
image: postgres:16
Each service is defined as a key under services, with config for image, ports, environment variables, and dependencies between services.
2. Kubernetes Manifests
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
replicas: 3
selector:
matchLabels:
app: api
template:
metadata:
labels:
app: api
spec:
containers:
- name: api
image: myregistry/api:1.4.0
ports:
- containerPort: 8080
Kubernetes relies heavily on YAML to define Deployments, Services, ConfigMaps, Secrets, and more. Even a tiny indentation mistake can make kubectl apply fail entirely.
3. CI/CD Pipeline (GitHub Actions)
name: CI
on: [push]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm install
- run: npm test
Tips to Avoid YAML Errors in Production
- Always run a YAML linter (e.g.
yamllint) before committing. - Never mix tabs and spaces — configure your editor to convert tabs to 2 spaces automatically.
- Quote strings containing special characters like
:,#, or values like"yes"/"no"to avoid the Norway Problem. - Validate structure before deploying — paste your manifest into JSONYAMify to check it's valid YAML and inspect it in Tree View.