1. GitOps คืออะไร?
GitOps คือวิธีการจัดการ infrastructure และ applications โดยใช้ Git เป็น "Single Source of Truth" หรือแหล่งข้อมูลหลัก ทุกการเปลี่ยนแปลงจะถูกบันทึกใน Git repository และระบบจะ sync สถานะจาก Git ไปยัง Kubernetes cluster โดยอัตโนมัติ
แนวคิดหลักของ GitOps
- Declarative: ระบุสถานะที่ต้องการ (Desired State) ในไฟล์ YAML
- Versioned: ทุกการเปลี่ยนแปลงถูก version ใน Git
- Automated: ระบบ sync อัตโนมัติเมื่อมีการเปลี่ยนแปลง
- Reconciled: ระบบตรวจสอบและแก้ไข drift อัตโนมัติ
GitOps Workflow
2. ทำไมต้องใช้ GitOps?
Version Control
ทุกการเปลี่ยนแปลงถูกบันทึกใน Git สามารถย้อนกลับ (rollback) ได้ทุกเมื่อ รู้ว่าใครเปลี่ยนอะไร เมื่อไหร่
Automatic Sync
ระบบ sync อัตโนมัติจาก Git ไป cluster ไม่ต้องรันคำสั่ง kubectl apply ด้วยมือ
Git as Single Source
สิ่งที่อยู่ใน Git คือสิ่งที่จะได้ใน cluster เสมอ ไม่มี configuration drift
Audit Trail
มีประวัติการเปลี่ยนแปลงครบถ้วน ผ่าน compliance audit ได้ง่าย
PR-Based Workflow
เปลี่ยนแปลงผ่าน Pull Request ทีมสามารถ review ก่อน deploy
Easy Rollback
มีปัญหาก็ git revert แล้วระบบจะ rollback อัตโนมัติ
3. ArgoCD vs Flux: เลือกอะไรดี?
ในปี 2025-2026 มี GitOps tools หลักอยู่ 2 ตัวคือ ArgoCD และ Flux ทั้งคู่เป็น CNCF graduated projects และใช้กันอย่างแพร่หลายในอุตสาหกรรม
| Feature | ArgoCD | Flux |
|---|---|---|
| UI/Dashboard | ✓ Web UI สวยงาม ใช้งานง่าย | △ มี UI แต่ต้องติดตั้งเพิ่ม |
| Learning Curve | ง่าย เหมาะกับผู้เริ่มต้น | ปานกลาง ต้องเรียนรู้เพิ่ม |
| Multi-cluster | ✓ รองรับดีเยี่ยม | ✓ รองรับ |
| Helm Support | ✓ Native support | ✓ Native support |
| Kustomize | ✓ Native support | ✓ Native support |
| Progressive Delivery | ✓ Argo Rollouts | ✓ Flagger |
| Resource Usage | ปานกลาง (~300MB) | เบา (~100MB) |
| Market Share 2025 | ~65% | ~35% |
คำแนะนำ: เลือก ArgoCD ถ้า...
- • ต้องการ Web UI ที่ใช้งานง่าย
- • ทีมยังใหม่กับ GitOps
- • ต้องการ visualize การ deploy
- • ต้องการใช้ Argo Rollouts สำหรับ Canary/Blue-Green deployments
4. ArgoCD Architecture
argocd-server
API Server และ Web UI รับ request จากผู้ใช้ และ serve dashboard
argocd-repo-server
จัดการ Git repository cloning และ cache manifests
argocd-application-controller
ตรวจสอบและ sync Applications เป็นหัวใจของ GitOps loop
Redis
Cache ข้อมูลเพื่อเพิ่มประสิทธิภาพการทำงาน
5. สิ่งที่ต้องเตรียม
Kubernetes Cluster
- Kubernetes v1.23+ (แนะนำ v1.28+)
- kubectl ติดตั้งและ config แล้ว
- kubeconfig มี admin access
Git Repository
- GitHub, GitLab, หรือ Bitbucket
- Personal Access Token หรือ SSH Key
- Repository มี Kubernetes manifests
CLI Tools
- kubectl (ตรวจสอบด้วย kubectl version)
- argocd CLI (ติดตั้งภายหลัง)
- helm (ถ้าใช้ Helm charts)
Resources
- CPU: 1 core สำหรับ ArgoCD
- Memory: 512MB - 1GB
- Storage: 5GB สำหรับ repo cache
6. ติดตั้ง ArgoCD
สร้าง Namespace และติดตั้ง ArgoCD
ติดตั้ง ArgoCD ใน namespace ชื่อ argocd
# สร้าง namespace
kubectl create namespace argocd
# ติดตั้ง ArgoCD
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
ตรวจสอบการติดตั้ง
รอให้ Pods ทั้งหมด Running
# ตรวจสอบ pods
kubectl get pods -n argocd
# ผลลัพธ์ควรจะประมาณนี้
NAME READY STATUS RESTARTS AGE
argocd-application-controller-0 1/1 Running 0 2m
argocd-applicationset-controller-5f69df9bb8-5xzm9 1/1 Running 0 2m
argocd-dex-server-6f9dd4dc8b-b5x9w 1/1 Running 0 2m
argocd-notifications-controller-7f9b9f5c5b-x8wz4 1/1 Running 0 2m
argocd-redis-5886b5b9d5-2j3k4 1/1 Running 0 2m
argocd-repo-server-6c5f8b9d5b-n7p2q 1/1 Running 0 2m
argocd-server-5d9f8b9d5b-k4m8n 1/1 Running 0 2m
เข้าถึง ArgoCD UI
เลือกวิธีเข้าถึง (ทางเลือกใดทางเลือกหนึ่ง):
ทางเลือกที่ 1: Port Forward (สำหรับทดสอบ)
# Port forward ไปยัง argocd-server
kubectl port-forward svc/argocd-server -n argocd 8080:443
# เปิด browser: https://localhost:8080
ทางเลือกที่ 2: LoadBalancer (สำหรับ Production)
# เปลี่ยน service type เป็น LoadBalancer
kubectl patch svc argocd-server -n argocd -p '{"spec": {"type": "LoadBalancer"}}'
# ตรวจสอบ External IP
kubectl get svc argocd-server -n argocd
ทางเลือกที่ 3: Ingress (แนะนำสำหรับ Production)
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: argocd-server-ingress
namespace: argocd
annotations:
nginx.ingress.kubernetes.io/ssl-passthrough: "true"
nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
spec:
ingressClassName: nginx
rules:
- host: argocd.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: argocd-server
port:
number: 443
ดึงรหัสผ่านเริ่มต้น
ArgoCD สร้างรหัสผ่าน admin อัตโนมัติ
# ดึงรหัสผ่านเริ่มต้น (ArgoCD v2.4+)
kubectl -n argocd get secret argocd-initial-admin-secret \
-o jsonpath="{.data.password}" | base64 -d
# Username: admin
# Password: (ผลลัพธ์จากคำสั่งด้านบน)
คำแนะนำ: เปลี่ยนรหัสผ่านทันทีหลัง login ครั้งแรก!
ติดตั้ง ArgoCD CLI
Linux / macOS
# macOS (Homebrew)
brew install argocd
# Linux
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/argocd
# ตรวจสอบ version
argocd version --client
Login ผ่าน CLI
# Login (เปลี่ยน URL ให้ตรงกับของคุณ)
argocd login localhost:8080 --insecure
# Username: admin
# Password: (รหัสผ่านที่ได้จากขั้นตอนก่อนหน้า)
# เปลี่ยนรหัสผ่าน
argocd account update-password
7. Deploy Application แรก
วิธีที่ 1: สร้างผ่าน CLI (แนะนำสำหรับเริ่มต้น)
# สร้าง application จาก GitLab/GitHub repository
argocd app create guestbook \
--repo https://github.com/argoproj/argocd-example-apps.git \
--path guestbook \
--dest-server https://kubernetes.default.svc \
--dest-namespace default
# Sync application
argocd app sync guestbook
# ตรวจสอบสถานะ
argocd app get guestbook
วิธีที่ 2: สร้างผ่าน YAML (Declarative)
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: guestbook
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/argoproj/argocd-example-apps.git
targetRevision: HEAD
path: guestbook
destination:
server: https://kubernetes.default.svc
namespace: default
syncPolicy:
automated:
prune: true # ลบ resources ที่ไม่มีใน Git อัตโนมัติ
selfHeal: true # แก้ไข drift อัตโนมัติ
syncOptions:
- CreateNamespace=true
# Apply application
kubectl apply -f guestbook-app.yaml
# ตรวจสอบ
kubectl get applications -n argocd
argocd app list
Application Resource Diagram
8. Sync Policies และ Automation
Sync Policy กำหนดว่า ArgoCD จะจัดการการ sync อย่างไร มี 3 โหมดหลัก:
Manual
Sync เมื่อกดปุ่มเท่านั้น เหมาะกับ production ที่ต้องการควบคุม
Automated
Sync อัตโนมัติเมื่อมีการเปลี่ยนแปลงใน Git
Auto + Self-Heal
Sync อัตโนมัติ + แก้ไข manual changes ใน cluster
# โหมด 1: Manual Sync (default)
spec:
syncPolicy: {}
# โหมด 2: Automated Sync
spec:
syncPolicy:
automated:
prune: true # ลบ resources ที่ไม่มีใน Git
# โหมด 3: Automated + Self-Heal (แนะนำสำหรับ staging)
spec:
syncPolicy:
automated:
prune: true
selfHeal: true # แก้ไข drift อัตโนมัติ
syncOptions:
- CreateNamespace=true
- PrunePropagationPolicy=foreground
- PruneLast=true
Sync Options ที่ใช้บ่อย
| Option | คำอธิบาย |
|---|---|
CreateNamespace=true |
สร้าง namespace อัตโนมัติถ้ายังไม่มี |
PrunePropagationPolicy |
กำหนดวิธีลบ resources (foreground/background/orphan) |
PruneLast=true |
ลบ resources เป็นขั้นตอนสุดท้าย |
ApplyOutOfSyncOnly=true |
Apply เฉพาะ resources ที่เปลี่ยนแปลง |
ServerSideApply=true |
ใช้ Server-Side Apply (แนะนำ) |
9. Best Practices 2025-2026
1. ใช้ App of Apps Pattern
สร้าง Application หลักที่ควบคุม Applications อื่นๆ ทั้งหมด ทำให้จัดการหลาย apps ได้จากที่เดียว
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: apps
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/org/k8s-configs.git
path: apps
destination:
server: https://kubernetes.default.svc
namespace: argocd
2. แยก Environments ด้วย Kustomize Overlays
ใช้ Kustomize overlays สำหรับ dev, staging, production environments
apps/
├── base/
│ ├── deployment.yaml
│ ├── service.yaml
│ └── kustomization.yaml
└── overlays/
├── development/
│ └── kustomization.yaml
├── staging/
│ └── kustomization.yaml
└── production/
└── kustomization.yaml
3. ใช้ ApplicationSets สำหรับ Multi-Cluster
ApplicationSets ช่วย deploy application เดียวกันไปหลาย clusters อัตโนมัติ
4. เปิด Notifications สำหรับ Sync Events
ตั้งค่า ArgoCD Notifications ส่งข้อความไป Slack, Email, หรือ Teams เมื่อมี sync failure
5. ใช้ RBAC และ SSO
เชื่อมต่อ ArgoCD กับ SSO (OIDC, SAML, LDAP) และกำหนด RBAC policies สำหรับทีมต่างๆ
10. แก้ไขปัญหาที่พบบ่อย
Application แสดง Status: OutOfSync
Cluster state ไม่ตรงกับ Git repository
# แก้ไข:
argocd app sync <app-name> --force
Sync Failed: Error from server (Forbidden)
ArgoCD ไม่มีสิทธิ์สร้าง/แก้ไข resources
# ตรวจสอบ:
kubectl get appproject default -n argocd -o yaml
Repository connection failed
ArgoCD ไม่สามารถ clone Git repository ได้
# เพิ่ม repository credentials:
argocd repo add https://github.com/org/repo.git --username <user> --password <token>
Health Status: Degraded
Pods ไม่สามารถ start หรือ crash ได้
# ตรวจสอบ pods:
kubectl get pods -n <namespace> -l app=<app-name>
kubectl describe pod <pod-name> -n <namespace>
kubectl logs <pod-name> -n <namespace>
สรุป
GitOps กับ ArgoCD คือ approach ที่ทันสมัยสำหรับจัดการ Kubernetes deployments โดยใช้ Git เป็น single source of truth ทำให้ทีม DevOps สามารถ:
- Track การเปลี่ยนแปลงทั้งหมดด้วย Git history
- Automate deployments ด้วย sync policies
- Rollback ได้ง่ายเมื่อเกิดปัญหา
- เพิ่ม security ด้วย PR review workflow