Skip to content

Container Orchestration ​

Container orchestration automates the deployment, management, scaling, and networking of containers across multiple hosts. This guide covers orchestration concepts, Docker Swarm, and an introduction to Kubernetes.

Why Orchestration? ​

Running containers on a single host works for development, but production environments require:

ChallengeOrchestration Solution
High availabilityAutomatic failover and replication
ScalingHorizontal scaling based on demand
Load balancingDistribute traffic across instances
Service discoveryAutomatic DNS and routing
Rolling updatesZero-downtime deployments
Resource managementEfficient allocation across hosts
Health monitoringAutomatic restart of failed containers

Orchestration Architecture ​

┌──────────────────────────────────────────────────────────┐
│                    Orchestrator                            │
│              (Swarm Manager / K8s Control Plane)          │
│                                                          │
│  ┌──────────┐  ┌──────────┐  ┌──────────────────┐      │
│  │Scheduler │  │ Service  │  │Health Monitor    │      │
│  │          │  │ Discovery│  │                  │      │
│  └──────────┘  └──────────┘  └──────────────────┘      │
└──────────────────────┬───────────────────────────────────┘
                       │
          ┌────────────┼────────────┐
          ▼            ▼            ▼
    ┌───────────┐┌───────────┐┌───────────┐
    │  Node 1   ││  Node 2   ││  Node 3   │
    │┌──┐┌──┐  ││┌──┐┌──┐  ││┌──┐┌──┐  │
    ││C1││C2│  │││C3││C4│  │││C5││C6│  │
    │└──┘└──┘  ││└──┘└──┘  ││└──┘└──┘  │
    └───────────┘└───────────┘└───────────┘

Docker Swarm ​

Docker Swarm is Docker's built-in orchestration solution. It is simple to set up and integrates natively with the Docker CLI.

Initialize a Swarm ​

bash
# Initialize Swarm on the manager node
docker swarm init --advertise-addr 192.168.1.10

# Output provides a join token for worker nodes
# docker swarm join --token SWMTKN-1-xxx 192.168.1.10:2377

# On worker nodes, join the swarm
docker swarm join --token SWMTKN-1-xxx 192.168.1.10:2377

# List nodes in the swarm
docker node ls

Swarm Node Roles ​

RoleDescriptionResponsibilities
ManagerControls the swarmScheduling, orchestration, serving API
WorkerExecutes tasksRunning containers
LeaderPrimary managerRaft consensus leader

Services ​

Services are the primary abstraction in Swarm for deploying applications:

bash
# Create a service
docker service create \
  --name web \
  --replicas 3 \
  --publish 80:80 \
  nginx:latest

# List services
docker service ls

# Inspect a service
docker service inspect web

# View service tasks (containers)
docker service ps web

# Scale a service
docker service scale web=5

# Update a service
docker service update --image nginx:1.25 web

# Remove a service
docker service rm web

Rolling Updates ​

bash
# Create a service with update configuration
docker service create \
  --name api \
  --replicas 6 \
  --update-parallelism 2 \
  --update-delay 10s \
  --update-failure-action rollback \
  --rollback-parallelism 1 \
  --rollback-delay 5s \
  my-api:1.0

# Perform a rolling update
docker service update --image my-api:2.0 api

# Monitor the update
docker service ps api

# Manual rollback
docker service rollback api

Update Configuration Options ​

OptionDescriptionDefault
--update-parallelismNumber of tasks to update simultaneously1
--update-delayDelay between updating batches0s
--update-failure-actionAction on update failure (pause, continue, rollback)pause
--update-max-failure-ratioFailure rate to tolerate0
--update-orderUpdate order (start-first, stop-first)stop-first

Swarm Networking ​

bash
# Create an overlay network
docker network create --driver overlay my-overlay

# Create a service on the overlay network
docker service create \
  --name api \
  --network my-overlay \
  --replicas 3 \
  my-api:latest

docker service create \
  --name db \
  --network my-overlay \
  postgres:16

# Services can reach each other by name
# api can connect to db using hostname "db"

Stack Deployments ​

Docker Stacks allow you to deploy multi-service applications using Compose files:

yaml
# docker-stack.yml
version: "3.9"

services:
  web:
    image: nginx:1.25
    ports:
      - "80:80"
    deploy:
      replicas: 3
      update_config:
        parallelism: 1
        delay: 10s
      restart_policy:
        condition: on-failure
    networks:
      - frontend

  api:
    image: my-api:latest
    deploy:
      replicas: 2
      resources:
        limits:
          cpus: '0.5'
          memory: 256M
    networks:
      - frontend
      - backend

  db:
    image: postgres:16
    volumes:
      - db-data:/var/lib/postgresql/data
    deploy:
      placement:
        constraints:
          - node.role == manager
    networks:
      - backend
    secrets:
      - db_password

networks:
  frontend:
    driver: overlay
  backend:
    driver: overlay
    internal: true

volumes:
  db-data:

secrets:
  db_password:
    external: true
bash
# Deploy a stack
docker stack deploy -c docker-stack.yml myapp

# List stacks
docker stack ls

# List services in a stack
docker stack services myapp

# List tasks in a stack
docker stack ps myapp

# Remove a stack
docker stack rm myapp

Introduction to Kubernetes ​

Kubernetes (K8s) is the industry-standard container orchestration platform for large-scale production deployments.

Key Kubernetes Concepts ​

ConceptDescription
PodSmallest deployable unit; one or more containers
DeploymentManages a set of identical pods with rolling updates
ServiceStable network endpoint for a set of pods
NamespaceVirtual cluster for resource isolation
ConfigMapConfiguration data as key-value pairs
SecretSensitive data (passwords, tokens)
IngressHTTP routing and load balancing
PersistentVolumeStorage that outlives pods

Basic Kubernetes Deployment ​

yaml
# deployment.yaml
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: my-app
          image: my-app:1.0
          ports:
            - containerPort: 3000
          resources:
            requests:
              memory: "128Mi"
              cpu: "250m"
            limits:
              memory: "256Mi"
              cpu: "500m"
          readinessProbe:
            httpGet:
              path: /health
              port: 3000
            initialDelaySeconds: 5
            periodSeconds: 10
---
apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000
  type: LoadBalancer
bash
# Apply the configuration
kubectl apply -f deployment.yaml

# Check deployment status
kubectl get deployments
kubectl get pods
kubectl get services

# Scale the deployment
kubectl scale deployment my-app --replicas=5

# Update the image (rolling update)
kubectl set image deployment/my-app my-app=my-app:2.0

# View rollout status
kubectl rollout status deployment/my-app

# Rollback
kubectl rollout undo deployment/my-app

Swarm vs Kubernetes Comparison ​

FeatureDocker SwarmKubernetes
Setup complexitySimple (built into Docker)Complex (separate installation)
Learning curveLowHigh
ScalingGoodExcellent
Auto-scalingManualBuilt-in HPA
NetworkingOverlay networksCNI plugins (flexible)
StorageVolume driversCSI drivers (flexible)
CommunitySmallerVery large
Production readinessSmall-medium workloadsEnterprise-grade
GUIDocker DesktopDashboard, Lens, Rancher

Choosing an Orchestration Platform ​

                    ┌──────────────────┐
                    │ How large is your │
                    │   deployment?     │
                    └────────┬─────────┘
                             │
                ┌────────────┼────────────┐
                ▼                         ▼
        ┌───────────┐            ┌───────────────┐
        │  Small /  │            │   Large /     │
        │  Medium   │            │   Enterprise  │
        └─────┬─────┘            └───────┬───────┘
              │                          │
              ▼                          ▼
     ┌─────────────────┐       ┌─────────────────┐
     │  Docker Swarm   │       │   Kubernetes    │
     │  or Compose     │       │                 │
     └─────────────────┘       └─────────────────┘

Health Checks and Monitoring ​

Swarm Health Checks ​

yaml
services:
  web:
    image: my-app:latest
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    deploy:
      replicas: 3
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3

Monitoring Stack ​

yaml
services:
  prometheus:
    image: prom/prometheus:latest
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
    ports:
      - "9090:9090"

  grafana:
    image: grafana/grafana:latest
    ports:
      - "3000:3000"
    volumes:
      - grafana-data:/var/lib/grafana

  cadvisor:
    image: gcr.io/cadvisor/cadvisor:latest
    volumes:
      - /:/rootfs:ro
      - /var/run:/var/run:ro
      - /sys:/sys:ro
      - /var/lib/docker/:/var/lib/docker:ro

volumes:
  grafana-data:

Next Steps ​

基于 MIT 许可发布