Chapters: JavaScript HTML & CSS JSON XML YAML ← Full TOC

← YAML Index

⚙️ YAML in DevOps — Kubernetes, Docker Compose, Ansible, GitHub Actions

YAML is the configuration language of modern DevOps. Kubernetes manifests, Docker Compose files, Ansible playbooks, and GitHub Actions workflows all use YAML. Understanding YAML deeply is essential for cloud-native development and infrastructure automation.

📋 Reference

ItemSyntax / ValueDescription
k8s DeploymentapiVersion: apps/v1 / kind: DeploymentKubernetes Deployment manifest
k8s metadatametadata: name, namespace, labels, annotationsKubernetes object metadata
k8s specspec: replicas, selector, template, containersDeployment specification
k8s resourcesresources: requests/limits: cpu/memoryContainer resource constraints
k8s env varsenv: - name: VAR / value: val / valueFrom:Container environment variables
k8s ConfigMapkind: ConfigMap / data: key: valueNon-secret configuration data
k8s Secretkind: Secret / data: key: base64valSecret data (base64 encoded)
k8s Servicekind: Service / spec: ports, selector, typeNetwork service definition
k8s Ingresskind: Ingress / spec: rules, tlsHTTP routing rules
k8s namespacekubectl apply -f manifest.yaml -n namespaceApply to specific namespace
Docker Composeversion / services / networks / volumesdocker-compose.yml structure
Compose serviceimage, build, ports, environment, volumesContainer service definition
Compose depends_ondepends_on: [db, redis]Service startup dependency order
Compose healthcheckhealthcheck: test, interval, timeout, retriesContainer health check
Ansible playbook- name / hosts / become / tasksPlaybook structure
Ansible task- name: / module: / with_items: / when:Task definition
Ansible varsvars: / vars_files: / group_vars/Variable definitions
Ansible handlershandlers: - name: / listen:Triggered on change notification
Ansible rolesroles: / defaults/ tasks/ handlers/ templates/Role directory structure
GHA workflowon: / jobs: / steps: / uses: / run:GitHub Actions workflow structure
GHA triggerson: push/pull_request/schedule/workflow_dispatchEvent triggers
GHA matrixstrategy: matrix: os/python-versionParallel build matrix
GHA secretssecrets.GITHUB_TOKEN / env: MY_VARAccess secrets and env vars
GHA actionsuses: actions/checkout@v4Use pre-built action

💡 Example

# ── Kubernetes Deployment ────────────────────────────────────
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mywebuniversity-api
  namespace: production
  labels:
    app: mywebuniversity
    tier: backend
    version: "2.0"
spec:
  replicas: 3
  selector:
    matchLabels:
      app: mywebuniversity
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        app: mywebuniversity
        tier: backend
    spec:
      containers:
        - name: api
          image: mywebuniversity/api:2.0.1
          ports:
            - containerPort: 8080
          env:
            - name: NODE_ENV
              value: production
            - name: DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: db-secret
                  key: password
            - name: REDIS_URL
              valueFrom:
                configMapKeyRef:
                  name: app-config
                  key: redis_url
          resources:
            requests:
              cpu:    "100m"
              memory: "128Mi"
            limits:
              cpu:    "500m"
              memory: "512Mi"
          livenessProbe:
            httpGet:
              path: /health
              port: 8080
            initialDelaySeconds: 10
            periodSeconds: 30
          readinessProbe:
            httpGet:
              path: /ready
              port: 8080
            initialDelaySeconds: 5
            periodSeconds: 10
---
# ── Docker Compose ───────────────────────────────────────────
services:
  api:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        NODE_VERSION: "20"
    image: mywebuniversity/api:latest
    ports:
      - "3000:3000"
    environment:
      NODE_ENV:    development
      DB_HOST:     db
      REDIS_HOST:  cache
    volumes:
      - ./src:/app/src
      - /app/node_modules
    depends_on:
      db:
        condition: service_healthy
      cache:
        condition: service_started
    restart: unless-stopped

  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_DB:       mywebuniversity
      POSTGRES_USER:     admin
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U admin -d mywebuniversity"]
      interval: 10s
      timeout:  5s
      retries:  5

  cache:
    image: redis:7-alpine
    command: redis-server --maxmemory 256mb --maxmemory-policy allkeys-lru

volumes:
  postgres_data:
---
# ── GitHub Actions CI/CD ─────────────────────────────────────
name: CI/CD Pipeline

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]
  schedule:
    - cron: '0 6 * * 1'   # Every Monday at 6am UTC

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ['3.10', '3.11', '3.12']
        os: [ubuntu-latest, macos-latest]
    steps:
      - uses: actions/checkout@v4
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install dependencies
        run: |
          pip install -r requirements.txt
          pip install pytest pytest-cov
      - name: Run tests
        run: pytest --cov=. --cov-report=xml
      - name: Upload coverage
        uses: codecov/codecov-action@v4

  deploy:
    needs: test
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    steps:
      - uses: actions/checkout@v4
      - name: Deploy to production
        env:
          SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
        run: |
          echo "$SSH_KEY" > /tmp/deploy_key
          chmod 600 /tmp/deploy_key
          ssh -i /tmp/deploy_key deploy@mywebuniversity.com \
            'cd /app && git pull && systemctl restart api'
---
# ── Ansible Playbook ─────────────────────────────────────────
---
- name: Configure MyWebUniversity web server
  hosts: webservers
  become: true
  vars:
    app_user:  www-data
    app_dir:   /var/www/React/prod
    node_ver:  "20"
  vars_files:
    - vars/secrets.yml

  pre_tasks:
    - name: Update apt cache
      apt:
        update_cache: true
        cache_valid_time: 3600

  tasks:
    - name: Install system packages
      apt:
        name:
          - apache2
          - python3
          - python3-pip
          - git
        state: present

    - name: Enable Apache CGI module
      apache2_module:
        name: cgid
        state: present
      notify: Restart Apache

    - name: Deploy application files
      copy:
        src:   "{{ item }}"
        dest:  "{{ app_dir }}/"
        owner: "{{ app_user }}"
        mode:  "0644"
      with_fileglob:
        - files/*.html
      notify: Reload Apache

    - name: Deploy CGI scripts
      copy:
        src:   cgi-bin/
        dest:  /usr/lib/cgi-bin/
        owner: "{{ app_user }}"
        mode:  "0755"

  handlers:
    - name: Restart Apache
      service:
        name:  apache2
        state: restarted

    - name: Reload Apache
      service:
        name:  apache2
        state: reloaded

← YAML in Python  |  🏠 Index  |  YAML vs JSON vs TOML →