เพิ่ม External Cluster เข้า Argo CD สำหรับ Multi-Cluster Deployment
ในยุคของ Cloud Native และ Microservices Architecture การจัดการแอปพลิเคชันข้ามหลายคลัสเตอร์ (Multi-Cluster) เป็นความจำเป็นที่สำคัญมากขึ้น Argo CD เป็นเครื่องมือ GitOps ที่ช่วยให้เราสามารถจัดการการ Deploy แอปพลิเคชันไปยังหลายคลัสเตอร์ Kubernetes ได้อย่างมีประสิทธิภาพและง่ายดาย
ทำไมต้องใช้ Multi-Cluster Deployment ด้วย Argo CD
- High Availability: ถ้าหนึ่งคลัสเตอร์มีปัญหา ยังมีคลัสเตอร์อื่นที่สามารถให้บริการได้ต่อเนื่อง
- Disaster Recovery: ข้อมูลและแอปพลิเคชันกระจายอยู่ในหลายตำแหน่ง ลดความเสี่ยงจากการสูญหายข้อมูล
- Geographic Distribution: วางคลัสเตอร์ใกล้กับผู้ใช้งาน เพื่อลด Latency
- Resource Optimization: กระจายโหลดไปยังคลัสเตอร์ต่างๆ
- Centralized Management: Argo CD ช่วยให้สามารถจัดการทุกคลัสเตอร์จากจุดเดียว
ผู้ให้บริการโฮสติ้ง Cloud VPS (https://de.co.th/cloud-vps) มีโครงสร้างพื้นฐานที่เหมาะสำหรับการรัน Kubernetes Clusters หลายตัว
ข้อกำหนดเบื้องต้น (Prerequisites)
- Argo CD Instance: ติดตั้งและทำงานอยู่ในคลัสเตอร์หลัก
- External Kubernetes Cluster: คลัสเตอร์ภายนอกที่ทำงานได้ปกติ
- kubectl Configuration: มี kubeconfig file สำหรับเชื่อมต่อ
- Network Connectivity: Management Cluster ต้องสามารถสื่อสารกับ External Cluster ได้
- Proper Permissions: Service Account ต้องมี Permissions ที่เพียงพอ
ขั้นตอนการเพิ่ม External Cluster เข้า Argo CD
1. การตรวจสอบความพร้อม
kubectl config current-context
kubectl config get-contexts
kubectl config use-context <management-cluster-context>
kubectl get pods -n argocd
2. การใช้ argocd cluster add command
argocd login <argocd-server> --username admin --password <password>
argocd cluster add <external-cluster-context> --name <cluster-name>
3. ตรวจสอบคลัสเตอร์ที่เพิ่มแล้ว
argocd cluster list
argocd cluster get https://xxx.xxx.xxx.xxx:6443
kubectl get secret -n argocd -l argocd.argoproj.io/secret-type=cluster
วิธีการยืนยันตัวตน (Authentication Methods)
1. Kubeconfig Authentication
ใช้ argocd cluster add command ซึ่งจะใช้ kubeconfig file โดยอัตโนมัติ
2. Service Account Authentication
kubectl create serviceaccount argocd-manager -n argocd
kubectl create clusterrole argocd-manager-role \
--verb=get,list,watch,create,update,patch,delete \
--resource=*
kubectl create clusterrolebinding argocd-manager-role-binding \
--clusterrole=argocd-manager-role \
--serviceaccount=argocd:argocd-manager
3. Bearer Token Authentication
argocd cluster add <external-cluster-context> \
--name <cluster-name> \
--bearer-token <token>
การ Deploy แอปพลิเคชันไปยัง External Cluster
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: production-app
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/your-org/your-repo.git
targetRevision: HEAD
path: apps/my-app
destination:
name: "Production Cluster"
namespace: production
syncPolicy:
automated:
prune: true
selfHeal: true
Cluster Labels และ ApplicationSet
argocd cluster add <external-cluster-context> \
--name <cluster-name> \
--label env=production,region=asia
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: multi-cluster-apps
namespace: argocd
spec:
generators:
- clusters:
selector:
matchLabels:
env: production
template:
metadata:
name: '{{name}}-app'
spec:
project: default
source:
repoURL: https://github.com/your-org/your-repo.git
targetRevision: HEAD
path: apps/my-app
destination:
server: '{{server}}'
namespace: default
RBAC สำหรับ Multi-Cluster
apiVersion: argoproj.io/v1alpha1
kind: AppProject
metadata:
name: production
namespace: argocd
spec:
sourceRepos:
- 'https://github.com/your-org/*'
destinations:
- namespace: '*'
server: 'https://production-cluster-api:6443'
clusterResourceWhitelist:
- group: '*'
kind: '*'
Best Practices สำหรับ Multi-Cluster Deployment
- ตั้งค่า Management Cluster ให้เป็นเพียง Control Plane
- แยก External Clusters ตามบทบาท เช่น Development, Staging, Production
- ใช้ RBAC เพื่อจำกัดสิทธิ์การเข้าถึง Cluster Secrets
- หมุนเปลี่ยน Credentials อย่างสม่ำเสมอ
- สำรองข้อมูล Argo CD Configuration เป็นระยะ
การแก้ไขปัญหา (Troubleshooting)
ปัญหา: Cluster สถานะเป็น Unknown
kubectl logs -n argocd deployment/argocd-application-controller | grep cluster-name
ปัญหา: Permission Denied เมื่อ Deploy
kubectl auth can-i get deployments --as=system:serviceaccount:argocd:argocd-manager -n argocd
สรุป
การเพิ่ม External Cluster เข้า Argo CD เป็นกระบวนการที่ค่อนข้างตรงไปตรงมา เมื่อใช้ ผู้ให้บริการโฮสติ้ง Cloud VPS เป็นพื้นฐานสำหรับการรัน Kubernetes Clusters จะได้รับความเชื่อถือได้และประสิทธิภาพที่เหมาะสำหรับการ Deploy ระดับ Production

