GitOps คืออะไร
GitOps เป็นแนวคิดที่ใช้ Git เป็น source of truth สำหรับ infrastructure และ application deployment โดย GitOps หมายถึง pull-based model ซึ่งถือว่า Git repository เป็นศูนย์กลางของ deployment และการจัดการ infrastructure ผสมผสาน DevOps, Infrastructure as Code, และ Git workflows เข้าด้วยกัน
GitOps ทำให้ Kubernetes และ cloud infrastructure มีความเป็นระบบ โดยเฉพาะอย่างยิ่งเมื่อใช้บริการ ผู้ให้บริการโฮสติ้ง Cloud VPS ที่รองรับ container services
4 หลักการสำคัญของ GitOps
- Declarative – Infrastructure และ application configuration ถูกเขียนในรูปแบบสถานะ (บอกว่าต้องการอะไร ไม่ใช่ขั้นตอนทำอย่างไร)
- Versioned and Immutable – Configuration ทั้งหมดเก็บอยู่ใน Git repository ซึ่งเป็น version control ที่ immutable
- Pulled Automatically – Deployment หรือ update ดึงมาจาก operator ที่เป็น pull-based ไม่ใช่ push จาก CI/CD server
- Continuously Reconciled – ตรวจสอบ actual state กับ desired state ใน Git ตลอดเวลา
ArgoCD: GitOps Operator สำหรับ Kubernetes
ArgoCD เป็น tool GitOps ที่มีความนิยมสำหรับ Kubernetes มันติดตามว่า Git repository และ cluster state อยู่ในสถานะเดียวกันหรือไม่
# ติดตั้ง ArgoCD
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
# Port forward เพื่อเข้า web UI
kubectl port-forward svc/argocd-server -n argocd 8080:443
# ดึง admin password
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d
# เชื่อมต่อ Git repository เป็น Application
cat <
ข้อดีของ ArgoCD:
- Web UI ที่สะดวกและใช้งานง่าย
- รองรับ Git diff ก่อนใช้งาน changes
- สนับสนุน webhook event จาก Git เพื่อเปิดใช้ deployment ได้เร็ว
- รองรับ drift detection เพื่อจับการเปลี่ยนแปลง configuration
Flux: GitOps Operator อีกตัวเลือก
Flux เป็น GitOps operator อีกตัวเลือกสำหรับ Kubernetes มีความคล้ายคลึงกับ ArgoCD แต่เหมาะสำหรับ Helm charts และการ sync จากหลาย source
# ติดตั้ง Flux
curl -s https://fluxcd.io/install.sh | bash
flux bootstrap github \
--owner=dotenterprise \
--repo=infrastructure \
--branch=main \
--path=./clusters/production
# สร้าง GitRepository source
flux create source git app-repo \
--url=https://github.com/dotenterprise/app-manifests \
--branch=main \
--interval=30s
# สร้าง Kustomization เพื่อ sync
flux create kustomization app \
--source=GitRepository/app-repo \
--path="./overlays/production" \
--prune=true \
--interval=30s
ข้อดีของ Flux:
- เลือกใช้ได้ง่ายสำหรับ GitOps
- รองรับ Helm charts ได้เป็นอย่างดี
- สนับสนุน Helm values จาก Secret management tools
- มี webhook receiver สำหรับ custom automation
GitOps Workflow ปกติ
ต่อไปนี้คือขั้นตอนทั่วไปของการใช้ GitOps:
# 1. Clone manifests repository
git clone https://github.com/dotenterprise/app-manifests.git
cd app-manifests
# 2. แก้ไข Kubernetes manifests
vi k8s/deployment.yaml
# - เปลี่ยน image: nginx:1.20 -> image: nginx:1.21
# - เปลี่ยน replicas: 2 -> replicas: 3
# 3. Commit และ push ไปยัง Git
git add k8s/deployment.yaml
git commit -m "Upgrade Nginx to 1.21 and scale to 3 replicas"
git push origin main
# 4. ArgoCD/Flux ตรวจสอบ Git repository
# - สามารถตรวจสอบการเปลี่ยนแปลงโดยอัตโนมัติ (ผ่าน webhook)
# - หากไม่มี webhook จะ poll ตามช่วงเวลาที่กำหนด
# 5. GitOps operator ประยุกต์ใช้ desired state
# ArgoCD:
argocd app sync my-app
# Flux:
flux reconcile source git app-repo
flux reconcile kustomization app
# 6. Kubernetes cluster ทำการ deployment
kubectl get deployments -n production
kubectl get pods -n production
# 7. หากต้องการ rollback กลับไปยังสถานะเดิม
git revert HEAD~1
git push origin main
# ArgoCD/Flux จะอัปเดต cluster โดยอัตโนมัติ
GitOps Best Practices
1. Single Source of Truth บน Git
ทั้งหมด configuration ของคุณต้องมาจาก Git repository เดียว ทำให้ทีมสามารถ review ผ่าน pull request ได้
2. Pull-Based Deployments
ไม่ใช้ push command จาก CI/CD server เพราะความปลอดภัยดีกว่า แต่ให้ operators pull จาก Git ผ่าน webhook
3. Declarative Configuration
เขียน YAML manifests ให้เป็น idempotent ซึ่งหมายความว่าสามารถเขียนได้หลายครั้งโดยให้ผลลัพธ์เดียวกัน
4. Automated Reconciliation
เปิดใช้ auto-healing และ self-healing เพื่อให้ cluster อยู่ในสถานะเดียวกับ Git อย่างต่อเนื่อง
5. Environment Parity
เก็บ environment ต่างๆ (dev, staging, prod) ไว้ใน Git ด้วยการใช้ Kustomize overlays
# k8s/overlays/dev/kustomization.yaml
namespace: dev
replicas:
- name: app
count: 1
# k8s/overlays/staging/kustomization.yaml
namespace: staging
replicas:
- name: app
count: 2
# k8s/overlays/production/kustomization.yaml
namespace: production
replicas:
- name: app
count: 3
# Git branches:
# - main-dev → dev cluster
# - main-staging → staging cluster
# - main → production cluster
GitOps กับ ผู้ให้บริการโฮสติ้ง Cloud VPS
การใช้ GitOps บน ผู้ให้บริการโฮสติ้ง Cloud VPS นำเสนอข้อดีหลายประการ:
- ลดเวลา deployment และเพิ่ม reliability
- รองรับ rollback ได้เร็วเมื่อเกิดปัญหา
- เพิ่ม infrastructure visibility ให้กับทีม
- ทำให้ deployment ยิ่งง่ายขึ้น แม้ว่า infrastructure จะมีความซับซ้อน
- ใช้ Git เป็น audit trail ของการเปลี่ยนแปลงทั้งหมด
สรุป
GitOps นำแนวคิดใหม่ที่สำคัญในการจัดการ infrastructure ด้วย Git เป็น source of truth สำหรับ application และ infrastructure deployments การเลือกใช้ ArgoCD หรือ Flux ช่วยให้คุณได้ automated, reliable, และ observable deployments บน ผู้ให้บริการโฮสติ้ง Cloud VPS ที่เหมาะสำหรับ production environments
