Argo CD
Argo CD คือเครื่องมือ GitOps continuous delivery สำหรับ Kubernetes ที่คอยเปรียบเทียบ desired state ใน Git กับ resources ที่อยู่ใน cluster แล้ว sync ให้ตรงกัน รองรับ Helm, Kustomize, plain YAML และ Jsonnet
จุดเด่นของ Argo CD
Section titled “จุดเด่นของ Argo CD”- GitOps — Git เป็น source of truth สำหรับ Kubernetes manifests และ deployment history
- Continuous reconciliation — ตรวจจับ configuration drift และ sync กลับสู่ desired state ได้อัตโนมัติ
- Multi-cluster — จัดการ application บนหลาย Kubernetes clusters จาก Argo CD instance เดียว
- Access control — กำหนดสิทธิ์ด้วย Project, RBAC และ SSO/OIDC
Concepts
Section titled “Concepts”Application
Section titled “Application”Application คือ resource ที่บอก Argo CD ว่า manifests อยู่ Git repository ใด, deploy ไป cluster/namespace ใด และจะ sync ด้วยวิธีใด
Project
Section titled “Project”Project ใช้กำหนดขอบเขตของ Application เช่น repository ที่อนุญาต, destination cluster/namespace และ resource kinds ที่ deploy ได้
Sync status
Section titled “Sync status”- Synced — resources ใน cluster ตรงกับ manifests ใน Git
- OutOfSync — resources ต่างจาก desired state หรือยังไม่เคย deploy
- Unknown — Argo CD อ่านสถานะ resource ไม่ได้
Health status
Section titled “Health status”- Healthy — application พร้อมใช้งาน
- Progressing — กำลัง rollout หรือรอ resource พร้อม
- Degraded — resource ทำงานผิดปกติ เช่น Deployment มี replica ไม่พร้อม
ติดตั้ง Argo CD
Section titled “ติดตั้ง Argo CD”Install ด้วย manifests
Section titled “Install ด้วย manifests”# สร้าง namespace สำหรับ Argo CDkubectl create namespace argocd
# ติดตั้ง Argo CD แบบ stable ด้วย server-side applykubectl apply -n argocd --server-side --force-conflicts -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
# รอให้ pods พร้อมใช้งานkubectl wait --for=condition=Ready pods --all -n argocd --timeout=300sInstall Argo CD CLI
Section titled “Install Argo CD CLI”# ติดตั้ง Argo CD CLI บน macOS ด้วย Homebrewbrew install argocd
# ตรวจสอบ version ของ CLIargocd version --clientเข้า Argo CD server
Section titled “เข้า Argo CD server”# เปิด Argo CD API server ผ่าน local port 8080kubectl port-forward svc/argocd-server -n argocd 8080:443
# อ่าน password เริ่มต้นของ adminargocd admin initial-password -n argocd
# login ผ่าน CLI โดยไม่ตรวจสอบ TLS สำหรับ port-forwardargocd login localhost:8080 --username admin --insecureเปิด UI ที่ https://localhost:8080 แล้ว login ด้วย user admin และ password ที่อ่านได้จากคำสั่งข้างต้น
Application Commands
Section titled “Application Commands”ดูและสร้าง Application
Section titled “ดูและสร้าง Application”# แสดง applications ทั้งหมดargocd app list
# ดูรายละเอียดและสถานะของ applicationargocd app get my-app
# สร้าง application จาก manifests ใน Gitargocd app create my-app \ --repo https://github.com/example/platform-config.git \ --path apps/my-app \ --dest-server https://kubernetes.default.svc \ --dest-namespace productionSync และ rollback
Section titled “Sync และ rollback”# sync application ให้ state ใน cluster ตรงกับ Gitargocd app sync my-app
# ดู resource tree และ health แบบต่อเนื่องargocd app wait my-app --health --sync --timeout 300
# ดู deployment historyargocd app history my-app
# rollback ไปยัง history ID ที่ต้องการargocd app rollback my-app <history-id>
# ลบ application และ resources ที่ Argo CD จัดการargocd app delete my-app --cascadeRefresh และ diff
Section titled “Refresh และ diff”# refresh ข้อมูล application จาก Git และ clusterargocd app get my-app --refresh
# เปรียบเทียบ desired state ใน Git กับ resources ใน clusterargocd app diff my-app
# ดู logs ของ resource ใน applicationargocd app logs my-appApplication Manifest
Section titled “Application Manifest”เก็บ Application manifest ไว้ใน Git เพื่อให้การตั้งค่า Argo CD เองเป็น GitOps ด้วย:
apiVersion: argoproj.io/v1alpha1kind: Applicationmetadata: name: my-app namespace: argocdspec: project: default source: repoURL: https://github.com/example/platform-config.git targetRevision: main path: apps/my-app destination: server: https://kubernetes.default.svc namespace: production syncPolicy: automated: prune: true selfHeal: true syncOptions: - CreateNamespace=true| Field | Description |
|---|---|
source.repoURL | Git repository ที่เก็บ manifests หรือ Helm chart |
source.targetRevision | branch, tag หรือ commit SHA ที่ต้องการ deploy |
source.path | directory ของ manifests ภายใน repository |
destination.server | Kubernetes API endpoint, ใน cluster ใช้ https://kubernetes.default.svc |
destination.namespace | namespace ปลายทางของ resources |
automated.prune | ลบ resources ที่เอาออกจาก Git |
automated.selfHeal | sync กลับเมื่อมีการแก้ resources โดยตรงใน cluster |
prune เมื่อมั่นใจว่า Git เก็บ resources ครบ เพราะ resource ที่ลบออกจาก Git อาจถูก Argo CD ลบออกจาก cluster ด้วย Helm และ Kustomize
Section titled “Helm และ Kustomize”Helm chart
Section titled “Helm chart”apiVersion: argoproj.io/v1alpha1kind: Applicationmetadata: name: nginx namespace: argocdspec: project: default source: repoURL: https://charts.bitnami.com/bitnami chart: nginx targetRevision: 22.0.0 helm: parameters: - name: service.type value: ClusterIP destination: server: https://kubernetes.default.svc namespace: nginxKustomize overlay
Section titled “Kustomize overlay”source: repoURL: https://github.com/example/platform-config.git targetRevision: main path: apps/my-app/overlays/production kustomize: images: - ghcr.io/example/my-app:1.2.3Repository และ Cluster
Section titled “Repository และ Cluster”# เพิ่ม private Git repository ด้วย HTTPS credentialsargocd repo add https://github.com/example/private-config.git \ --username <git-username> \ --password <access-token>
# แสดง repositories ที่ Argo CD เชื่อมต่ออยู่argocd repo list
# เพิ่ม Kubernetes context ปัจจุบันให้ Argo CD จัดการargocd cluster add <kube-context>
# แสดง clusters ที่เชื่อมต่ออยู่argocd cluster listสำหรับ Git provider ควรใช้ deploy key หรือ access token แบบ read-only และเก็บ credentials ใน secret manager เมื่อใช้งานจริง
เชื่อม Git repository ด้วย SSH
Section titled “เชื่อม Git repository ด้วย SSH”# สร้าง SSH key เฉพาะสำหรับ Argo CD (ไม่มี passphrase)ssh-keygen -t ed25519 -b 4096 -C "argocd-repo" -f argocd-key -N ""
# เพิ่ม public key เป็น deploy key (read-only) ใน GitHub:# Settings > Deploy keys > Add deploy key แล้ววาง argocd-key.pub
# เพิ่ม private Git repository ด้วย SSH private keyargocd repo add git@github.com:example/private-config.git \ --ssh-private-key-path ./argocd-key
# แสดง repositories ที่เชื่อมต่ออยู่ พร้อมตรวจสอบ connection statusargocd repo listถ้าต้องใช้ SSH ผ่าน port 443 (กรณี network บล็อก port 22) ให้เขียน known hosts ทับแล้วระบุ URL แบบ ssh://:
# override known_hosts ด้วย host key ของ ssh.github.com port 443argocd repo add ssh://git@ssh.github.com:443/example/private-config.git \ --ssh-private-key-path ./argocd-key \ --insecure-ignore-host-keyAppProject และ RBAC
Section titled “AppProject และ RBAC”ตัวอย่าง Project สำหรับจำกัดทีม payments ให้ deploy ได้เฉพาะ repository และ namespace ที่ระบุ:
apiVersion: argoproj.io/v1alpha1kind: AppProjectmetadata: name: payments namespace: argocdspec: description: Applications owned by the payments team sourceRepos: - https://github.com/example/payments-config.git destinations: - namespace: payments-* server: https://kubernetes.default.svc clusterResourceWhitelist: - group: '' kind: Namespace# แสดง projects ทั้งหมดargocd proj list
# ดู policy และขอบเขตของ projectargocd proj get payments
# สร้าง role ใน project สำหรับผู้ที่ sync application ได้argocd proj role create payments deployer
# เพิ่ม policy ให้ role sync applications ใน projectargocd proj role add-policy payments deployer \ --action sync \ --object 'payments/*' \ --permission allowStop Service บน Production
Section titled “Stop Service บน Production”การ stop service ทำได้ด้วยการ scale Deployment เป็น 0 replicas แต่เมื่อใช้ร่วมกับ Argo CD ต้องระวังระบบ Auto Sync กับ selfHeal ที่อาจ scale กลับตามค่าใน Git รวมถึง HPA ที่คุม replicas อยู่
Scale เป็น 0 แบบชั่วคราว
Section titled “Scale เป็น 0 แบบชั่วคราว”สมมติมี Deployment ชื่อ one-id-service ใน namespace production:
# scale deployment เป็น 0 replicas เพื่อ stop servicekubectl scale deployment one-id-service --replicas=0 -n production
# ตรวจสอบว่า pods ถูก terminate หมดแล้วkubectl get pods -n production
# ตรวจสอบ replicas ของ deployment (ควรเห็น 0/0)kubectl get deployment one-id-service -n productionselfHeal: true ไว้ จะตรวจพบว่า replicas ไม่ตรงกับ Git แล้วปรับกลับเป็นค่าเดิม และถ้ามี HPA คุม Deployment อยู่ก็อาจ scale กลับเช่นกัน ตรวจสอบด้วย kubectl get hpa -n production Stop แบบ GitOps (แนะนำสำหรับ Maintenance)
Section titled “Stop แบบ GitOps (แนะนำสำหรับ Maintenance)”แก้ replicas ใน Git แล้วให้ Argo CD sync เอง เพื่อให้ Git ยังเป็น single source of truth:
apiVersion: apps/v1kind: Deploymentmetadata: name: one-id-servicespec: # เดิมเป็น 3 เปลี่ยนเป็น 0 เพื่อ stop service replicas: 0# commit และ push แล้ว Argo CD จะตรวจพบการเปลี่ยนแปลงและ terminate pods ทั้งหมดgit commit -m "stop one-id-service for maintenance" && git pushเมื่อ maintenance เสร็จ เปลี่ยน replicas กลับเป็นค่าเดิมแล้ว push ได้เลย
Stop แบบฉุกเฉิน
Section titled “Stop แบบฉุกเฉิน”กรณี production มีปัญหาและต้องหยุด service ทันทีโดยไม่รอ Git:
# step 1: ปิด auto-sync ชั่วคราวกัน Argo CD แก้ค่ากลับargocd app set one-id-service --sync-policy none
# step 2: scale เป็น 0kubectl scale deployment one-id-service --replicas=0 -n production
# step 3: ตรวจสอบผลลัพธ์kubectl get deployment one-id-service -n productionเมื่อแก้ปัญหาเสร็จ ให้ reconcile ค่าเข้า Git ก่อน แล้วค่อยเปิด auto-sync กลับ:
# แก้ replicas ใน Git ให้ตรงกับ state ที่ต้องการ แล้ว syncargocd app sync one-id-service
# เปิด auto-sync กลับพร้อม self-healargocd app set one-id-service --sync-policy automated --self-healStop ผ่าน k9s
Section titled “Stop ผ่าน k9s”# เปิด k9s ที่ namespace productionk9s -n productionจากนั้นพิมพ์ :deploy เลือก Deployment ที่ต้องการ กด s เพื่อ scale ใส่ 0 แล้ว confirm — ต้องระวัง selfHeal เหมือนกัน
สิ่งที่ไม่ควรทำ
Section titled “สิ่งที่ไม่ควรทำ”# อย่าใช้ delete pod เพื่อ stop service เพราะ Deployment controller จะสร้าง pod ใหม่ทันทีkubectl delete pod one-id-service-xxxxxการ delete pod เหมาะกับการ restart หรือ replace pod มากกว่า สำหรับ restart ให้ใช้:
# rolling restart ทั้ง deploymentkubectl rollout restart deployment/one-id-service -n productionสรุปวิธีเลือกใช้
Section titled “สรุปวิธีเลือกใช้”| สถานการณ์ | วิธีแนะนำ |
|---|---|
| Restart pod | kubectl rollout restart |
| Stop ชั่วคราว | Scale replicas = 0 |
| Stop แบบ GitOps | แก้ replicas: 0 ใน Git |
| Stop ฉุกเฉิน | ปิด auto-sync แล้ว scale 0 |
| Stop ผ่าน k9s | Scale deployment เป็น 0 |
| Stop ถาวร | ลบ Deployment จาก Git แล้ว sync |