ArgoCD ApplicationSet Container Orchestration

โดย อ. บอมกิตติทัศน์เจริญพนาสิทธิ์ | อัปเดต 24 ก. พ. 2026 | อ่าน 16 นาที
- GitOps คืออะไร — แนวคิดพื้นฐาน
- ArgoCD คืออะไร — GitOps CD สำหรับ Kubernetes
- ติดตั้ง ArgoCD บน Kubernetes
- สร้าง Application แรก — Sync จาก Git
- Sync Policy — Auto vs Manual
- ApplicationSet — สร้างหลาย Application จาก Template
- Generator Types — List, Cluster, Git, Matrix
- Multi-cluster Deployment ด้วย ApplicationSet
- Container Orchestration Concepts
- Rolling Update, Blue-Green, Canary
- Argo Rollouts — Progressive Delivery
- ArgoCD กับ Helm และ Kustomize
- RBAC และ SSO
- Monitoring และ Notifications
- Best Practices และสรุป
GitOps คืออะไร — แนวคิดพื้นฐาน
GitOps เป็นแนวคิดที่ Weaveworks นำเสนอในปี 2017 หลักการคือใช้ Git Repository เป็น Single Source of Truth สำหรับ Infrastructure และ Application ทุกการเปลี่ยนแปลง (Deploy, Config Change, Scale) ต้องผ่าน Git Commit โดยส่ง Pull Request, Code Review, Approve แล้ว Merge จากนั้น GitOps Operator จะ Sync ให้ Cluster ตรงกับ Git อัตโนมัติ
ข้อดีของ GitOps ได้แก่ Audit Trail ทุกการเปลี่ยนแปลงมี Git History, Rollback ง่าย แค่ Revert Git Commit, Consistency ทุก Environment ตรงกับ Git, Security ไม่ต้องให้ CI/CD เข้าถึง Cluster โดยตรง (Pull-based) และ Self-healing ถ้ามีคนแก้ Cluster โดยตรง ArgoCD จะ Sync กลับให้ตรง Git
ArgoCD คืออะไร — GitOps CD สำหรับ Kubernetes
ArgoCD เป็น Declarative GitOps Continuous Delivery Tool สำหรับ Kubernetes เป็น CNCF Graduated Project ที่นิยมที่สุดในหมวด GitOps ทำงานโดยเปรียบเทียบ Desired State (ใน Git) กับ Actual State (บน Cluster) ถ้าต่างกันจะแสดงว่า OutOfSync และ Sync ให้ตรงกันอัตโนมัติ (ถ้าตั้ง Auto-sync) หรือรอ Manual Sync
ArgoCD มี Web UI ที่สวยงามมากแสดง Application Status, Resource Tree, Diff ระหว่าง Git กับ Cluster, Sync History และ Rollback ได้จาก UI เลยนอกจากนี้ยังมี CLI, gRPC API และ Webhook สำหรับ Automation
ติดตั้ง ArgoCD บน Kubernetes
# ติดตั้งด้วย kubectl
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
# หรือติดตั้งด้วย Helm (แนะนำ)
helm repo add argo https://argoproj.github.io/argo-helm
helm install argocd argo/argo-cd \
--namespace argocd \
--create-namespace \
--set server.service.type=LoadBalancer
# ดึง Admin Password
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d
# ติดตั้ง CLI
curl -sSL -o argocd https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64
chmod +x argocd && sudo mv argocd /usr/local/bin/
# Login
argocd login <ARGOCD_SERVER> --username admin --password <PASSWORD>
สร้าง Application แรก — Sync จาก Git
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: my-api
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/myorg/k8s-manifests.git
targetRevision: main
path: apps/my-api/overlays/production
destination:
server: https://kubernetes.default.svc
namespace: production
syncPolicy:
automated:
prune: true # ลบ Resource ที่ไม่มีใน Git
selfHeal: true # แก้ไข Manual Change กลับให้ตรง Git
allowEmpty: false
syncOptions:
- CreateNamespace=true
- PrunePropagationPolicy=foreground
retry:
limit: 5
backoff:
duration: 5s
factor: 2
maxDuration: 3m
Sync Policy — Auto vs Manual
| Policy | ลักษณะ | เหมาะกับ |
|---|---|---|
| Manual Sync | ต้องกด Sync เอง | Production ที่ต้อง Approval ก่อน Deploy |
| Auto Sync | Sync อัตโนมัติเมื่อ Git เปลี่ยน | Dev/Staging ที่ต้องการ Deploy ทันที |
| Auto Sync + Prune | ลบ Resource ที่ไม่มีใน Git ด้วย | ต้องการ Git เป็น Truth 100% |
| Auto Sync + Self-heal | แก้ Manual Change กลับ | ป้องกัน Drift จาก kubectl edit |
ApplicationSet — สร้างหลาย Application จาก Template
ApplicationSet เป็น CRD ที่สร้าง ArgoCD Application หลายตัวจาก Template เดียวแทนที่จะเขียน Application YAML ซ้ำๆสำหรับทุก Service หรือทุก Cluster
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: microservices
namespace: argocd
spec:
generators:
- list:
elements:
- name: user-api
namespace: user
- name: order-api
namespace: order
- name: payment-api
namespace: payment
- name: notification-api
namespace: notification
template:
metadata:
name: '{{name}}'
spec:
project: default
source:
repoURL: https://github.com/myorg/k8s-manifests.git
targetRevision: main
path: 'apps/{{name}}/overlays/production'
destination:
server: https://kubernetes.default.svc
namespace: '{{namespace}}'
syncPolicy:
automated:
prune: true
selfHeal: true
ApplicationSet นี้สร้าง 4 Application (user-api, order-api, payment-api, notification-api) จาก Template เดียวถ้าเพิ่ม Service ใหม่แค่เพิ่ม Element ใน List
Generator Types — List, Cluster, Git, Matrix
| Generator | Source | Use Case |
|---|---|---|
| List | Static List ใน YAML | จำนวน Service/Cluster น้อยและคงที่ |
| Cluster | ArgoCD Cluster Secrets | Deploy ไปทุก Cluster ที่ลงทะเบียนกับ ArgoCD |
| Git Directory | Directory ใน Git Repo | Monorepo ที่แต่ละ Directory คือ 1 Service |
| Git File | JSON/YAML File ใน Git | Config File กำหนด Parameter ต่อ Environment |
| Matrix | Combine 2 Generators | Deploy ทุก Service ไปทุก Cluster (Cartesian Product) |
| Merge | Merge หลาย Generators | Override Parameter ต่อ Cluster |
| Pull Request | GitHub/GitLab PR | สร้าง Preview Environment ต่อ PR |
Multi-cluster Deployment ด้วย ApplicationSet
# Deploy ทุก Service ไปทุก Cluster ด้วย Matrix Generator
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: multi-cluster-apps
namespace: argocd
spec:
generators:
- matrix:
generators:
# Generator 1: Clusters
- clusters:
selector:
matchLabels:
env: production
# Generator 2: Services
- git:
repoURL: https://github.com/myorg/k8s-manifests.git
revision: main
directories:
- path: apps/*
template:
metadata:
name: '{{path.basename}}-{{name}}'
spec:
project: default
source:
repoURL: https://github.com/myorg/k8s-manifests.git
targetRevision: main
path: '{{path}}/overlays/{{metadata.labels.env}}'
destination:
server: '{{server}}'
namespace: '{{path.basename}}'
Container Orchestration Concepts
Container Orchestration คือการจัดการ Container จำนวนมากให้ทำงานร่วมกัน Kubernetes เป็น Orchestrator ที่นิยมที่สุดจัดการ Scheduling (วาง Pod บน Node ที่เหมาะสม), Scaling (เพิ่ม/ลด Pod ตาม Load), Networking (Service Discovery, Load Balancing), Storage (Persistent Volume), Health Check (Restart Pod ที่ล่ม) และ Rolling Update (อัปเดตทีละ Pod ไม่ Downtime)
ArgoCD เป็นส่วนสำคัญของ Container Orchestration เพราะจัดการ Deployment Lifecycle ทั้งหมดตั้งแต่ Desired State ใน Git จนถึง Actual State บน Cluster ทำให้ Deployment เป็น Declarative, Auditable และ Reproducible
Rolling Update, Blue-Green, Canary
- Rolling Update — อัปเดตทีละ Pod แทนที่ Old Pod ด้วย New Pod ไม่มี Downtime เป็น Default ของ Kubernetes Deployment
- Blue-Green — Deploy Version ใหม่ (Green) ขนานกับ Version เก่า (Blue) แล้วสลับ Traffic ทั้งหมดไป Green ถ้ามีปัญหาสลับกลับ Blue ได้ทันที
- Canary — ส่ง Traffic ส่วันนี้อย (เช่น 5%) ไป Version ใหม่ก่อนดู Metrics ว่าปกติแล้วค่อยๆเพิ่มเป็น 25% → 50% → 100% ถ้ามีปัญหา Rollback ทันที
Argo Rollouts — Progressive Delivery
apiVersion: argoproj.io/v1alpha1
kind: Rollout
metadata:
name: my-api
spec:
replicas: 5
strategy:
canary:
steps:
- setWeight: 10
- pause: {duration: 5m}
- setWeight: 30
- pause: {duration: 5m}
- setWeight: 60
- pause: {duration: 5m}
- setWeight: 100
canaryService: my-api-canary
stableService: my-api-stable
trafficRouting:
nginx:
stableIngress: my-api-ingress
analysis:
templates:
- templateName: success-rate
startingStep: 2
args:
- name: service-name
value: my-api-canary
selector:
matchLabels:
app: my-api
template:
metadata:
labels:
app: my-api
spec:
containers:
- name: api
image: my-api:v2.0
ports:
- containerPort: 8080
ArgoCD กับ Helm และ Kustomize
ArgoCD รองรับหลาย Config Management Tool
# Helm Source
source:
repoURL: https://charts.bitnami.com/bitnami
chart: postgresql
targetRevision: 13.4.0
helm:
values: |
primary:
persistence:
size: 50Gi
auth:
postgresPassword: secret
# Kustomize Source
source:
repoURL: https://github.com/myorg/k8s-manifests.git
path: apps/my-api/overlays/production
kustomize:
images:
- my-api=registry.example.com/my-api:v2.0
RBAC และ SSO
ArgoCD มี RBAC ในตัวกำหนดว่าใครทำอะไรกับ Application/Project ไหนได้รองรับ SSO ผ่าน OIDC, SAML, LDAP, GitHub, GitLab, Okta, Keycloak
# argocd-rbac-cm ConfigMap
policy.csv: |
p, role:dev, applications, get, */*, allow
p, role:dev, applications, sync, dev/*, allow
p, role:ops, applications, *, */*, allow
g, dev-team, role:dev
g, ops-team, role:ops
Monitoring และ Notifications
- Prometheus Metrics — ArgoCD Export Metrics สำหรับ Grafana Dashboard (Sync Status, Health, Latency)
- ArgoCD Notifications — ส่งแจ้งเตือนไป Slack, Teams, Email เมื่อ Sync สำเร็จ/ล้มเหลว
- Web UI — ดู Application Status, Resource Tree, Diff, Sync History
Best Practices และสรุป
- แยก App Repo กับ Config Repo — Application Code อยู่ Repo หนึ่ง Kubernetes Manifests อยู่อีก Repo
- ใช้ ApplicationSet แทน Application ซ้ำๆ — ลด Duplication จัดการง่าย
- ตั้ง Auto-sync + Self-heal สำหรับ Non-prod — Manual Sync สำหรับ Production
- ใช้ Argo Rollouts สำหรับ Canary — ไม่ใช่ Kubernetes Deployment ปกติ
- ใช้ RBAC — จำกัดสิทธิ์ตาม Team และ Environment
- ใช้ Project — แยก Application ตาม Team/Environment ด้วย ArgoCD Project
- Notification — ตั้งแจ้งเตือน Slack เมื่อ Sync ล้มเหลว
- Backup ArgoCD — Export Application/Project YAML เก็บใน Git
ArgoCD + ApplicationSet เป็นเครื่องมือ GitOps ที่ทรงพลังสำหรับ Container Orchestration บน Kubernetes ทำให้ Deployment เป็น Declarative, Auditable และ Self-healing ลดความผิดพลาดจาก Manual Deployment ได้อย่างมากติดตามบทความใหม่ๆได้ที่ SiamCafe.net
Q: ArgoCD คืออะไร
GitOps CD Tool สำหรับ Kubernetes ใช้ Git เป็น Source of Truth เปรียบเทียบ Git กับ Cluster แล้ว Sync ให้ตรงกันอัตโนมัติ