1. บทนำ: GitOps และ ArgoCD คืออะไร?
GitOps คือวิธีการจัดการ Infrastructure และ Applications โดยใช้ Git เป็นแหล่งข้อมูลหลัก (Single Source of Truth) โดยทุกการเปลี่ยนแปลงจะถูกบันทึกใน Git Repository และระบบจะ Sync การเปลี่ยนแปลงนั้นไปยัง Kubernetes Cluster โดยอัตโนมัติ
ArgoCD เป็นเครื่องมือ GitOps Continuous Delivery (CD) สำหรับ Kubernetes ที่ช่วยให้คุณสามารถจัดการการ deploy แอปพลิเคชันได้อย่างง่ายดาย โดยทำหน้าที่เปรียบเสมือน Controller ที่คอยตรวจสอบและ Sync สถานะของแอปพลิเคชันใน Cluster ให้ตรงกับที่กำหนดไว้ใน Git
ทำไมต้องใช้ Multi-Cluster Management?
- • High Availability - กระจาย workload ไปหลาย regions/zones
- • Environment Isolation - แยก Dev, Staging, Production environments
- • Disaster Recovery - สำรองระบบเมื่อเกิดเหตุการณ์ไม่คาดคิด
- • Multi-Tenancy - รองรับลูกค้าหลายรายใน clusters แยกกัน
2. Multi-Cluster Architecture
โครงสร้าง ArgoCD Multi-Cluster Architecture
ประเภทของ Multi-Cluster Architecture
Hub-and-Spoke Pattern
ArgoCD ติดตั้งใน Hub Cluster เดียว และจัดการ Spoke Clusters ทั้งหมดจากจุดศูนย์กลาง (แนะนำสำหรับ Production)
Replica Pattern
ติดตั้ง ArgoCD ในทุก Cluster (แนะนำสำหรับแต่ละทีมมี Cluster ของตัวเอง)
3. สิ่งที่ต้องเตรียม
Hardware Requirements
- Hub Cluster: 2 vCPU, 4GB RAM (ขั้นต่ำ)
- Spoke Clusters: 1 vCPU, 2GB RAM ต่อ cluster
- Network connectivity ระหว่าง clusters
Software Requirements
- Kubernetes v1.24+ (ทุก cluster)
- kubectl v1.28+
- ArgoCD v2.9+ (รองรับ ApplicationSets)
- Git Repository (GitHub, GitLab, Bitbucket)
4. การติดตั้ง ArgoCD บน Hub Cluster
ขั้นตอนที่ 1: สร้าง Namespace และติดตั้ง ArgoCD
# สร้าง namespace สำหรับ ArgoCD
kubectl create namespace argocd
# ติดตั้ง ArgoCD ด้วย manifest จาก official
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
# ตรวจสอบสถานะ pods
kubectl get pods -n argocd
# รอจนกว่า pods ทั้งหมดจะพร้อม (ประมาณ 2-3 นาที)
kubectl wait --for=condition=available --timeout=600s deployment/argocd-server -n argocd
ขั้นตอนที่ 2: เข้าถึง ArgoCD UI
# ตั้งค่า LoadBalancer หรือใช้ port-forward (สำหรับ testing)
kubectl port-forward svc/argocd-server -n argocd 8080:443
# ดึงรหัสผ่านเริ่มต้น (username: admin)
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d
# เข้าถึง UI ได้ที่ https://localhost:8080
ขั้นตอนที่ 3: ติดตั้ง ArgoCD CLI
# สำหรับ Linux
curl -sSL -o argocd-linux-amd64 https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64
sudo install -m 555 argocd-linux-amd64 /usr/local/bin/argocd
rm argocd-linux-amd64
# สำหรับ macOS
brew install argocd
# Login เข้า ArgoCD
argocd login localhost:8080 --grpc-web
5. ลงทะเบียน Clusters
วิธีที่ 1: ใช้ ArgoCD CLI (แนะนำ)
# ตั้งค่า context ของ cluster ที่ต้องการเพิ่ม
kubectl config use-context dev-cluster
# เพิ่ม cluster เข้าสู่ ArgoCD
argocd cluster add dev-cluster --name dev-cluster
# เพิ่ม Staging cluster
kubectl config use-context staging-cluster
argocd cluster add staging-cluster --name staging-cluster
# เพิ่ม Production cluster
kubectl config use-context prod-cluster-1
argocd cluster add prod-cluster-1 --name prod-cluster-1
# ตรวจสอบ clusters ทั้งหมด
argocd cluster list
วิธีที่ 2: ใช้ Kubernetes Secret (Manual)
apiVersion: v1
kind: Secret
metadata:
name: prod-cluster-1-secret
namespace: argocd
labels:
argocd.argoproj.io/secret-type: cluster
type: Opaque
stringData:
name: prod-cluster-1
server: https://prod-cluster-1.example.com:6443
config: |
{
"bearerToken": "eyJhbGciOiJSUzI1NiIsImtpZCI6...",
"tlsClientConfig": {
"insecure": false,
"caData": "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0t..."
}
}
คำแนะนำ: ควรใช้ Service Account แทน Bearer Token เพื่อความปลอดภัยที่ดีขึ้น
6. ApplicationSets สำหรับ Multi-Cluster
ApplicationSets เป็น feature ของ ArgoCD ที่ช่วยให้คุณสามารถ deploy แอปพลิเคชันเดียวกันไปยังหลาย clusters พร้อมกันได้อย่างง่ายดาย โดยใช้ template และ generators
ตัวอย่าง ApplicationSet สำหรับ Multi-Environment
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: webapp-multi-cluster
namespace: argocd
spec:
# ใช้ List Generator กำหนด clusters ที่ต้องการ deploy
generators:
- list:
elements:
- cluster: dev-cluster
url: https://dev-cluster.example.com:6443
env: dev
replicas: "1"
- cluster: staging-cluster
url: https://staging-cluster.example.com:6443
env: staging
replicas: "2"
- cluster: prod-cluster-1
url: https://prod-cluster-1.example.com:6443
env: prod
replicas: "3"
- cluster: prod-cluster-2
url: https://prod-cluster-2.example.com:6443
env: prod
replicas: "3"
# Template สำหรับสร้าง Application
template:
metadata:
name: 'webapp-{{env}}-{{cluster}}'
labels:
app: webapp
environment: '{{env}}'
spec:
project: default
source:
repoURL: https://github.com/your-org/webapp-manifests.git
targetRevision: main
path: 'overlays/{{env}}'
helm:
parameters:
- name: replicaCount
value: '{{replicas}}'
destination:
server: '{{url}}'
namespace: webapp
syncPolicy:
automated:
prune: true
selfHeal: true
syncOptions:
- CreateNamespace=true
ประเภทของ Generators
List Generator
กำหนด clusters ด้วย list แบบ hardcode (เหมาะกับจำนวน cluster ไม่มาก)
Cluster Generator
ดึงข้อมูล clusters จาก ArgoCD โดยอัตโนมัติ (เหมาะกับหลาย clusters)
Git Generator
อ่านข้อมูลจาก Git repository (เหมาะกับ dynamic configurations)
Matrix Generator
รวม generators หลายตัวเข้าด้วยกัน (สำหรับ complex use cases)
Cluster Generator Example (แนะนำ)
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: webapp-all-clusters
namespace: argocd
spec:
generators:
- clusters:
selector:
matchLabels:
argocd.argoproj.io/secret-type: cluster
environment: production # เลือกเฉพาะ production clusters
template:
metadata:
name: 'webapp-{{name}}'
spec:
project: default
source:
repoURL: https://github.com/your-org/webapp-manifests.git
targetRevision: main
path: overlays/production
destination:
server: '{{server}}'
namespace: webapp
syncPolicy:
automated:
prune: true
selfHeal: true
7. ตัวอย่างโค้ด
Directory Structure สำหรับ Multi-Cluster
webapp-manifests/
├── base/ # Base manifests (kustomize)
│ ├── deployment.yaml
│ ├── service.yaml
│ ├── configmap.yaml
│ └── kustomization.yaml
├── overlays/
│ ├── dev/ # Dev environment overrides
│ │ ├── kustomization.yaml
│ │ └── patches/
│ │ └── replica-patch.yaml
│ ├── staging/ # Staging environment overrides
│ │ ├── kustomization.yaml
│ │ └── patches/
│ └── production/ # Production environment overrides
│ ├── kustomization.yaml
│ └── patches/
├── applicationset.yaml # ApplicationSet definition
└── README.md
Script สำหรับ Register หลาย Clusters พร้อมกัน
#!/bin/bash
# กำหนดรายการ clusters
CLUSTERS=(
"dev-cluster:dev-cluster-context"
"staging-cluster:staging-cluster-context"
"prod-cluster-1:prod-cluster-1-context"
"prod-cluster-2:prod-cluster-2-context"
)
# Loop ผ่าน clusters และลงทะเบียน
for cluster_info in "${CLUSTERS[@]}"; do
IFS=':' read -r name context <<< "$cluster_info"
echo "Registering cluster: $name"
# Switch context
kubectl config use-context "$context"
# Register cluster
argocd cluster add "$context" --name "$name" --grpc-web
# เพิ่ม labels สำหรับใช้กับ ApplicationSet
kubectl label secret -n argocd "${name}-cluster-secret" \
environment="${name%%-*}" \
argocd.argoproj.io/secret-type=cluster
echo "✓ Cluster $name registered successfully"
done
echo "All clusters registered!"
argocd cluster list
8. Best Practices สำหรับ Multi-Cluster Management
1. แยก Hub Cluster
ติดตั้ง ArgoCD ใน dedicated management cluster แยกจาก workload clusters เพื่อความปลอดภัยและความเสถียร
2. ใช้ Projects
แบ่งแอปพลิเคชันออกเป็น ArgoCD Projects ตามทีมหรือ environment เพื่อควบคุมสิทธิ์การเข้าถึง
3. จัดการ Secrets
ใช้ External Secrets Operator หรือ Sealed Secrets เพื่อจัดการ sensitive data ใน Git
4. Auto-Sync Policy
เปิดใช้งาน auto-sync สำหรับ non-production environments แต่ใช้ manual sync สำหรับ production
5. ใช้ Labels
ติด labels บน clusters เพื่อใช้กับ ApplicationSet generators (environment, region, tier)
6. Monitoring
ติดตั้ง Prometheus + Grafana เพื่อ monitor ArgoCD metrics และ cluster health
9. แก้ไขปัญหาที่พบบ่อย
ปัญหา: Cluster Connection Failed
สาเหตุ: Network connectivity หรือ authentication ผิดพลาด
# ตรวจสอบ connection
argocd cluster get
# ตรวจสอบ logs
kubectl logs -n argocd deployment/argocd-application-controller
# ลบและเพิ่ม cluster ใหม่
argocd cluster rm
argocd cluster add --name
ปัญหา: Application Sync Failed
สาเหตุ: Manifest ไม่ถูกต้อง หรือ resource conflicts
# ตรวจสอบ sync status
argocd app get
# Force sync
argocd app sync --force
# ตรวจสอบ diff
argocd app diff
ปัญหา: ApplicationSet ไม่สร้าง Applications
สาเหตุ: Generator ไม่ match กับ cluster labels
# ตรวจสอบ ApplicationSet status
kubectl get applicationset -n argocd -o yaml
# ตรวจสอบ cluster labels
kubectl get secret -n argocd -l argocd.argoproj.io/secret-type=cluster
# เพิ่ม label ให้ cluster
kubectl label secret -n argocd environment=production
สรุป
การใช้ ArgoCD สำหรับ Multi-Cluster Management ช่วยให้คุณสามารถจัดการ Kubernetes Clusters หลายตัวได้อย่างมีประสิทธิภาพ ด้วย GitOps approach ที่ทำให้ทุกการเปลี่ยนแปลงถูกต้องและสามารถตรวจสอบย้อนกลับได้
ArgoCD
GitOps Controller
ApplicationSets
Multi-Cluster Deploy
GitOps
Single Source of Truth