Container Orchestration

Kubernetes & Docker Swarm: So Sánh Chi Tiết & Hướng Dẫn Production Năm 2026

Từ kiến trúc đến triển khai thực tế — bài viết đầy đủ nhất giúp bạn hiểu rõ cả hai nền tảng container orchestration hàng đầu thế giới

27/06/2026 25 phút đọc Võ Đào Huy Hoàng

1. Giới thiệu

Năm 2026, container orchestration không còn là lựa chọn — nó là tiêu chuẩn bắt buộc cho bất kỳ hệ thống production scale nào. Docker đã thay đổi cách chúng ta đóng gói ứng dụng, nhưng khi bạn có 10, 50, hay 500 container chạy trên nhiều máy chủ, bạn cần một lớp điều phối (orchestrator) để quản lý chúng.

Hai cái tên thống trị thị trường: Docker Swarm (tích hợp sẵn trong Docker Engine) và Kubernetes (K8s — hệ sinh thái mã nguồn mở lớn nhất thế giới). Mỗi nền tảng có triết lý thiết kế, điểm mạnh và yếu riêng. Bài viết này sẽ đi sâu vào cả hai, so sánh từng khía cạnh, và giúp bạn ra quyết định sáng suốt cho dự án của mình.

2. Container Orchestration là gì?

Container orchestration là quá trình tự động hoá việc triển khai, quản lý, scaling và networking của các container. Một orchestrator giải quyết những bài toán sau:

  • Service Discovery — container A làm sao tìm được container B?
  • Load Balancing — phân phối traffic đều giữa các container replica
  • Health Checks & Self-healing — container chết → tự động restart
  • Rolling Updates — deploy phiên bản mới không downtime
  • Secret & Config Management — quản lý API keys, cấu hình tập trung
  • Resource Management — CPU/RAM limits giữa các service

Tại sao không chỉ dùng Docker Compose?

Docker Compose rất tuyệt cho development và single-host deployment. Nhưng khi bạn cần chạy trên nhiều máy chủ (multi-host), Compose không hỗ trợ:

  • ❌ Không có cluster management — mỗi máy chạy riêng lẻ
  • ❌ Không có built-in load balancing giữa các host
  • ❌ Không có self-healing — container crash trên host A, không auto migrate
  • ❌ Không có rolling update xuyên host

Container orchestration (Swarm hoặc K8s) giải quyết tất cả vấn đề trên, biến một nhóm máy chủ rời rạc thành một siêu máy tính duy nhất.

3. Docker Swarm — Đơn giản & Hiệu quả

Docker Swarm là giải pháp orchestration tích hợp sẵn trong Docker Engine từ phiên bản 1.12 (2016). Không cần cài thêm bất kỳ tool nào — chỉ cần Docker là đã có Swarm mode. Triết lý của Swarm: "keep it simple". Nó không có tham vọng trở thành hệ điều hành cho cloud, mà chỉ là một orchestrator nhẹ, dễ dùng, đủ mạnh cho phần lớn use cases.

Kiến trúc Swarm

Swarm cluster gồm hai loại node:

  • Manager Nodes — quản lý cluster state, scheduling, API endpoint (Raft consensus, 3-5 node cho HA)
  • Worker Nodes — chạy container tasks, nhận lệnh từ manager
🔥 Raft Consensus: Swarm dùng thuật toán Raft để đồng bộ state giữa các manager nodes. Với 3 managers, cluster chịu được mất 1 node. Với 5 managers, chịu được mất 2. Workers có thể scale đến hàng trăm node mà không ảnh hưởng đến control plane.

Luồng hoạt động cơ bản:

  1. User gửi docker stack deploy tới Manager API
  2. Manager phân tích Compose file → tạo Service object
  3. Scheduler (spread/binpack) chọn Worker node phù hợp
  4. Worker node pull image + chạy container task
  5. Manager giám sát health, tự động reschedule nếu task die

Cài đặt & Khởi tạo Cluster

Chỉ 3 lệnh để có một Swarm cluster sẵn sàng production:

# Trên máy chủ đầu tiên (Manager) docker swarm init --advertise-addr=192.168.1.10 # Output sẽ hiện token join worker docker swarm join --token SWMTKN-1-xxxx 192.168.1.10:2377 # Trên máy worker docker swarm join --token SWMTKN-1-xxxx 192.168.1.10:2377 # Kiểm tra cluster docker node ls ID HOSTNAME STATUS AVAILABILITY dz6p4v8k9x2a * node1 Ready Active y7q5r2s3t1b0 node2 Ready Active

So với Kubernetes, Swarm setup đơn giản hơn rất nhiều: không cần kubeadm, không cần CNI plugin, không cần etcd riêng. Docker Engine đã bao gồm tất cả.

Triển khai Service với Stack

Swarm dùng Docker Compose file (v3+) để định nghĩa services. File này giống hệt Compose file cho development — chỉ cần thêm vài trường orchestration-specific:

# docker-compose.yml — Swarm stack version: '3.8' services: api: image: myapp/api:1.2.0 deploy: replicas: 5 update_config: parallelism: 2 delay: 10s order: start-first restart_policy: condition: any delay: 5s resources: limits: cpus: '0.5' memory: 256M ports: - target: 3000 published: 80 mode: ingress networks: - app-net healthcheck: test: ["CMD", "curl", "-f", "http://localhost/health"] interval: 30s timeout: 10s retries: 3 redis: image: redis:7-alpine deploy: replicas: 1 networks: - app-net networks: app-net: driver: overlay
# Deploy stack docker stack deploy -c docker-compose.yml myapp # Kiểm tra services docker stack services myapp # ID NAME REPLICAS IMAGE # xyz123 myapp_api 5/5 myapp/api:1.2.0 # abc456 myapp_redis 1/1 redis:7-alpine

Một điểm mạnh của Swarm: Compose file = Dev = Production. Bạn dùng cùng file cho docker compose up (dev) và docker stack deploy (production). Không cần chuyển đổi format.

Scaling & Rolling Update

Swarm hỗ trợ scaling cực kỳ đơn giản:

# Scale service lên 10 replicas docker service scale myapp_api=10 # Rolling update với zero downtime docker service update --image myapp/api:1.3.0 myapp_api # Xem trạng thái update docker service ps myapp_api

Swarm thực hiện rolling update theo thứ tự định nghĩa trong update_config: mỗi lần update 2 container, chờ 10s, kiểm tra health, rồi tiếp tục. Nếu health check fail, Swarm tự động rollback.

⚠️ Giới hạn của Swarm: Không hỗ trợ Horizontal Pod Autoscaler (HPA) dựa trên CPU/memory. Bạn phải scale thủ công hoặc dùng third-party tool. Đây là một trong những lý do lớn nhất để chọn K8s cho workload có traffic biến động mạnh.

4. Kubernetes — Hệ sinh thái toàn diện

Kubernetes (K8s) là hệ thống orchestration mã nguồn mở do Google phát triển (dựa trên Borg/Omega), nay thuộc Cloud Native Computing Foundation (CNCF). Với hơn 100,000+ stars trên GitHub và hàng triệu cluster production trên toàn thế giới, K8s là tiêu chuẩn de facto cho container orchestration.

Kiến trúc Kubernetes

Kubernetes phức tạp hơn Swarm nhưng linh hoạt hơn nhiều. Cluster gồm:

🧠
Control Plane
🖥️
Worker Node
📦
Pods

Control Plane components:

  • kube-apiserver — cổng giao tiếp duy nhất với cluster (REST API)
  • etcd — key-value store, lưu toàn bộ cluster state (Raft consensus)
  • kube-scheduler — quyết định Pod chạy trên node nào
  • kube-controller-manager — chạy các controllers (Deployment, Node, Endpoint...)

Worker Node components:

  • kubelet — agent chính trên mỗi node, giao tiếp với API server
  • kube-proxy — network rules, load balancing cho Services
  • Container Runtime — Docker / containerd / CRI-O
🔑 Key concept: Desired State — Kubernetes hoạt động theo mô hình declarative. Bạn khai báo trạng thái mong muốn (ví dụ: "chạy 3 replicas của image X"), và K8s liên tục điều chỉnh để đạt trạng thái đó. Đây là khác biệt căn bản với Swarm (imperative + declarative hybrid).

Cài đặt Cluster

Setup K8s cluster phức tạp hơn Swarm nhiều. Có nhiều cách:

# Cách 1: kubeadm (production-ready) kubeadm init --pod-network-cidr=10.244.0.0/16 # + cài CNI (Flannel/Calico/Cilium) kubectl apply -f https://raw.githubusercontent.com/.../kube-flannel.yml # Cách 2: Minikube (local dev) minikube start --cpus=4 --memory=8g # Cách 3: Managed K8s (cloud — khuyên dùng cho production) # EKS (AWS): eksctl create cluster --name mycluster --region ap-southeast-1 # AKS (Azure): az aks create --name mycluster --resource-group myrg # GKE (GCP): gcloud container clusters create mycluster --region=asia-southeast1

Khuyến nghị 2026: Dùng managed K8s (EKS/AKS/GKE) cho production. Self-managed K8s chỉ phù hợp khi bạn có team infrastructure riêng hoặc chạy on-premise.

Triển khai Deployment

Kubernetes dùng YAML declarative để định nghĩa resources. Một deployment cơ bản:

# deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: api labels: app: myapp tier: backend spec: replicas: 5 selector: matchLabels: app: myapp template: metadata: labels: app: myapp spec: containers: - name: api image: myapp/api:1.2.0 ports: - containerPort: 3000 resources: requests: cpu: 250m memory: 128Mi limits: cpu: 500m memory: 256Mi livenessProbe: httpGet: path: /health port: 3000 initialDelaySeconds: 10 readinessProbe: httpGet: path: /ready port: 3000 affinity: podAntiAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 100 podAffinityTerm: labelSelector: matchLabels: app: myapp topologyKey: kubernetes.io/hostname
# Triển khai kubectl apply -f deployment.yaml # Kiểm tra kubectl get pods -l app=myapp # NAME READY STATUS RESTARTS AGE # api-7d4f8c9b6a-abc12 1/1 Running 0 45s # api-7d4f8c9b6a-def34 1/1 Running 0 45s # ... # Rolling update kubectl set image deployment/api api=myapp/api:1.3.0 # Rollback nếu lỗi kubectl rollout undo deployment/api

Kubernetes hỗ trợ readiness probesliveness probes — hai loại health check riêng biệt. Readiness quyết định Pod có nhận traffic không, liveness quyết định Pod có cần restart không. Đây là tính năng Swarm không có.

Networking & Service Discovery

Kubernetes có mô hình networking phức tạp nhưng mạnh mẽ:

# service.yaml — expose deployment apiVersion: v1 kind: Service metadata: name: api-service spec: type: ClusterIP selector: app: myapp ports: - port: 80 targetPort: 3000 --- apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: api-ingress spec: ingressClassName: nginx rules: - host: api.myapp.com http: paths: - path: / pathType: Prefix backend: service: name: api-service port: number: 80

K8s có 4 loại Service: ClusterIP (internal), NodePort (static port trên mỗi node), LoadBalancer (cloud LB), và ExternalName. Kết hợp với Ingress Controller (nginx, traefik, haproxy), bạn có routing layer cực kỳ linh hoạt.

💡 So sánh: Swarm's Routing Mesh (Ingress mode) cho phép mọi node đều listen trên port 80/443 và tự động load balance đến container ở bất kỳ node nào. K8s đạt được điều tương tự qua Service + Ingress, nhưng cấu hình phức tạp hơn.

Advanced: Autoscaling, Helm & GitOps

Đây là những tính năng K8s vượt trội hoàn toàn so với Swarm:

Horizontal Pod Autoscaler (HPA)

apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: api-hpa spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: api minReplicas: 3 maxReplicas: 20 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 70 - type: Resource resource: name: memory target: type: Utilization averageUtilization: 80

HPA tự động tăng/giảm số replicas dựa trên CPU/memory hoặc custom metrics (requests/giây, queue length...). Swarm không có tính năng tương đương.

Helm — Kubernetes Package Manager

Helm là "apt-get cho Kubernetes". Bạn có thể cài cả một stack phức tạp (Prometheus + Grafana + Alertmanager) chỉ với 2 lệnh:

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts helm install monitoring prometheus-community/kube-prometheus-stack

GitOps với ArgoCD

GitOps là mô hình quản lý infrastructure dùng Git làm source of truth. ArgoCD tự động đồng bộ cluster state với Git repository:

# Application.yaml — ArgoCD app apiVersion: argoproj.io/v1alpha1 kind: Application metadata: name: myapp spec: source: repoURL: https://github.com/company/k8s-manifests path: ./production destination: namespace: production server: https://kubernetes.default.svc syncPolicy: automated: prune: true selfHeal: true

Với GitOps, mọi thay đổi đều đi qua Pull Request — có review, có audit trail, có rollback. Đây là best practice cho enterprise K8s năm 2026.

5. So Sánh Chi Tiết

Bảng so sánh tính năng

Tính năng Docker Swarm Kubernetes
Cài đặt cluster ⚡ 1 lệnh (swarm init) 5-10 bước (kubeadm + CNI)
Learning curve 🟢 Thấp (dùng Docker Compose) 🔴 Cao (nhiều concept mới)
Autoscaling ❌ Không built-in ✅ HPA + VPA + custom metrics
Rolling update ✅ Có (Compose 3.x) ✅ Có (Deployment + Strategy)
Service discovery ✅ DNS-based (built-in) ✅ DNS + Env + API
Load balancing ✅ Routing Mesh (ingress) ✅ Service + Ingress + Service Mesh
Health check ✅ restart_policy + healthcheck ✅ Liveness + Readiness + Startup probe
Secret management ✅ docker secret ✅ Secret + External Secret Operator
Storage orchestration ❌ Limited (volume driver) ✅ CSI + PVC/PV + StatefulSet
Multi-cluster ❌ Không hỗ trợ ✅ Cluster API + Federation
Ecosystem (tools) Hạn chế 👑 Rất lớn (Helm, Argo, Istio, Prometheus...)
Resource efficiency 🟢 Nhẹ (~50MB overhead/node) Nặng (~500MB-1GB overhead/node)
Production adoption Thấp (~5-10%) 🔥 Rất cao (~85-90%)

Khi nào chọn Swarm? Khi nào chọn K8s?

✅ Chọn Docker Swarm khi:
  • Team nhỏ (<5 người), không có chuyên gia infrastructure
  • Cluster nhỏ (<10 node) — edge computing, IoT, on-premise
  • Ứng dụng stateless, không cần storage phức tạp
  • Cần time-to-market nhanh — MVP, prototype, startup giai đoạn đầu
  • Docker-native stack — đơn giản hoá operations
  • Budget hạn chế — Swarm chạy được trên VPS $5/tháng
✅ Chọn Kubernetes khi:
  • Cần autoscaling dựa trên CPU/memory hoặc custom metrics
  • Chạy stateful workloads — database, Kafka, Cassandra
  • Yêu cầu network policy phức tạp, service mesh (Istio/Linkerd)
  • Multi-cloud hoặc multi-region deployment
  • Đã có team infrastructure riêng
  • Cần GitOps workflow (ArgoCD/Flux) cho compliance
  • Scale >20 nodes — K8s quản lý hiệu quả hơn

Quy tắc ngón tay cái: Nếu bạn không chắc chọn gì, hãy bắt đầu với Docker Swarm cho MVP, sau đó migrate lên Kubernetes khi cần scale hoặc thêm tính năng phức tạp. Cả hai đều dùng container — code của bạn không thay đổi, chỉ thay đổi lớp orchestration.

6. Case Study Thực Tế

Case 1: Startup Fintech — Swarm cho MVP

Công ty: Startup Fintech Việt Nam, team 4 backend engineers
Yêu cầu: MVP processing 500 transactions/giây, launch trong 2 tháng
Giải pháp: Docker Swarm trên 3 VPS ($20/tháng x 3)

Kết quả: Cluster hoạt động ổn định 18 tháng, zero downtime, phục vụ 10,000+ users. Chi phí infrastructure: $60/tháng. Khi scale lên 50,000 users, họ migrate lên K8s (EKS) trong 2 tuần mà không cần sửa code ứng dụng.

📊 Bài học: Swarm giúp startup validate product nhanh, tiết kiệm 80% chi phí infrastructure ở giai đoạn đầu. Việc migrate lên K8s khi cần scale là con đường phổ biến và an toàn.

Case 2: Enterprise E-commerce — K8s Multi-cluster

Công ty: Sàn thương mại điện tử Đông Nam Á, 200+ engineers
Yêu cầu: 100,000 requests/s, 99.99% uptime, multi-region (SG, ID, TH)
Giải pháp: 3 EKS clusters (1 region/cluster) + ArgoCD GitOps + Istio service mesh

# Cluster setup per region eksctl create cluster --name=prod-sg --region=ap-southeast-1 \ --nodegroup-name=workers --node-type=m5.xlarge --nodes=20 # Service mesh — Istio istioctl install --set profile=demo -y # GitOps — ArgoCD app-of-apps argocd app create prod --repo https://github.com/company/k8s-manifests \ --path overlays/production --dest-server https://

Kết quả: Cluster xử lý peak 150,000 requests/s (Black Friday), P99 latency <200ms. Zero downtime trong 6 tháng. ArgoCD tự động deploy 50+ microservices từ Git, mỗi ngày 20+ deployments.

📊 Chi phí: ~$15,000/tháng cho 3 EKS clusters + Istio + monitoring stack. Nhưng với revenue $10M/tháng, chi phí infrastructure chỉ ~0.15% — hoàn toàn xứng đáng cho độ tin cậy và tốc độ phát triển.

8. Lộ trình học Container Orchestration

Nếu bạn muốn học container orchestration từ đầu, đây là lộ trình tối ưu:

  1. Bước 1: Docker căn bản (2 tuần)
    Học Dockerfile, docker-compose, images, volumes, networks.
    Mục tiêu: deploy được 1 app 2-tier (frontend + API) bằng Compose.
  2. Bước 2: Docker Swarm (1 tuần)
    Học docker swarm init, stack deploy, scaling, rolling update.
    Mục tiêu: deploy app lên Swarm cluster 3 nodes.
  3. Bước 3: Kubernetes căn bản (3 tuần)
    Học Pod, Deployment, Service, ConfigMap, Secret, Ingress.
    Mục tiêu: deploy app lên Minikube, hiểu kubectl cơ bản.
  4. Bước 4: Kubernetes trung cấp (2 tuần)
    Học StatefulSet, PersistentVolume, HPA, NetworkPolicy, Helm.
    Mục tiêu: deploy stateful app (PostgreSQL) + scaling tự động.
  5. Bước 5: Production (2 tuần)
    Học monitoring (Prometheus/Grafana), GitOps (ArgoCD), security (RBAC, OPA).
    Mục tiêu: quản lý production cluster an toàn.
🎯 Tổng thời gian: ~10 tuần để từ zero đến production-ready K8s. Nếu chỉ cần Swarm: 3 tuần. Học theo thứ tự này giúp bạn hiểu sâu, không bị overwhelmed bởi K8s complexity ngay từ đầu.

💻 Code Example: Kubernetes Deployment

# Kubernetes Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    spec:
      containers:
      - name: web
        image: nginx:latest
        ports:
        - containerPort: 80

9. Kết Luận

Docker SwarmKubernetes không phải là đối thủ — chúng là công cụ cho những bài toán khác nhau:

  • Swarm: đơn giản, nhanh, đủ dùng — cho MVP, small team, edge computing
  • K8s: mạnh mẽ, linh hoạt, scale vô hạn — cho enterprise, multi-cloud, complex workloads

Năm 2026, hệ sinh thái container orchestration đã trưởng thành. Cả hai nền tảng đều ổn định, được kiểm chứng bởi hàng ngàn production deployments. Lựa chọn đúng đắn là lựa chọn phù hợp với team, budget, và requirements cụ thể của bạn, không phải chạy theo trend.

Nếu bạn đang ở giai đoạn đầu của dự án, hãy bắt đầu với Docker Swarm — bạn sẽ có sản phẩm chạy production trong 1 ngày thay vì 1 tuần. Khi cần scale, K8s luôn sẵn sàng chào đón bạn. Và nếu bạn đã dùng K8s rồi, hãy tận dụng sức mạnh của ecosystem: GitOps, Service Mesh, AIOps — đó mới là giá trị thực sự.