Docker Cluster Management
This chapter covers Docker cluster management technologies, including Docker Swarm and Kubernetes basics.
Why Cluster Management?
Single-host Docker faces production challenges:
| Problem | Description |
|---|---|
| Single point of failure | One server down, all services unavailable |
| Resource bottleneck | Limited single-machine resources |
| Load balancing | No automatic traffic distribution |
| Service discovery | Difficult cross-node communication |
Docker Swarm
Docker Swarm is Docker's built-in cluster management and orchestration tool.
Architecture
┌─────────────────────────────────────────────┐
│ Docker Swarm Cluster │
│ │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ Manager │ │ Manager │ │ Manager │ │
│ │ Node 1 │ │ Node 2 │ │ Node 3 │ │
│ └────┬─────┘ └────┬─────┘ └────┬─────┘ │
│ │ │ │ │
│ ┌────▼─────┐ ┌────▼─────┐ ┌────▼─────┐ │
│ │ Worker │ │ Worker │ │ Worker │ │
│ │ Node 1 │ │ Node 2 │ │ Node 3 │ │
│ └──────────┘ └──────────┘ └──────────┘ │
└─────────────────────────────────────────────┘Initialize Swarm
bash
docker swarm init --advertise-addr <MANAGER-IP>
# Run the output join command on Worker nodes
docker node lsDeploy Services
bash
docker service create --name web --replicas 3 -p 80:80 nginx:latest
docker service ls
docker service ps web
docker service scale web=5
docker service update --image nginx:1.25 web
docker service rm webDeploy with Stack
yaml
# docker-stack.yml
version: '3.8'
services:
web:
image: nginx:latest
deploy:
replicas: 3
update_config:
parallelism: 1
delay: 10s
restart_policy:
condition: on-failure
ports:
- "80:80"bash
docker stack deploy -c docker-stack.yml myapp
docker stack ls
docker stack services myapp
docker stack rm myappKubernetes Introduction
Kubernetes (K8s) is the most popular container orchestration platform.
Core Concepts
| Concept | Description |
|---|---|
| Pod | Smallest deployment unit, contains one or more containers |
| Deployment | Manages Pod replicas and updates |
| Service | Provides stable network access to Pods |
| Namespace | Logical resource isolation |
| ConfigMap | Configuration management |
| Secret | Sensitive information management |
Try K8s with Docker Desktop
- Open Docker Desktop → Settings → Kubernetes
- Check "Enable Kubernetes"
- Click "Apply & Restart"
bash
kubectl cluster-info
kubectl get nodesBasic Operations
bash
kubectl create deployment web --image=nginx --replicas=3
kubectl expose deployment web --port=80 --type=NodePort
kubectl get pods
kubectl get services
kubectl scale deployment web --replicas=5
kubectl set image deployment/web nginx=nginx:1.25
kubectl logs <pod-name>
kubectl delete deployment webDocker Swarm vs Kubernetes
| Feature | Docker Swarm | Kubernetes |
|---|---|---|
| Learning curve | Low | High |
| Setup complexity | Simple | Complex |
| Feature richness | Basic | Very rich |
| Community ecosystem | Smaller | Very large |
| Scale | Small-medium | Large |
| Docker integration | Native | Requires extra config |
| Auto-scaling | Manual | Supported |
Choosing
- Docker Swarm: Small-medium teams, quick setup, simple cluster needs
- Kubernetes: Large teams, complex microservices, rich ecosystem support needed
Chapter Summary
Cluster management is key to running containerized applications in production. Docker Swarm offers simplicity, while Kubernetes is the industry standard for large-scale orchestration. Choose based on team size and business requirements.