ทำ GitOps บน Kubernetes ด้วย ArgoCD แบบ Automated Deployment

GitOps บน Kubernetes ด้วย ArgoCD

GitOps เป็นแนวทางการจัดการ Kubernetes โดยใช้ Git repository เป็นแหล่งที่มาของความจริง (Source of Truth) ArgoCD คือเครื่องมือที่ช่วยให้การนำไปใช้งาน (Deployment) ของ Kubernetes เป็นไปโดยอัตโนมัติและตามตัวอักษรในสิ่งที่บันทึกไว้ใน Git repository

หลักการทำงานของ ArgoCD

Git as Source of Truth: ทุกการตั้งค่า Kubernetes ต้องเก็บไว้ใน Git repository โดยใช้ไฟล์ YAML

Continuous Synchronization: ArgoCD จะตรวจสอบสถานะของ Kubernetes cluster กับที่เก็บไว้ใน Git repository อย่างต่อเนื่อง และทำการอัปเดตให้ตรงกัน

Automated Rollback: ถ้ามีการแก้ไขตั้งค่า Kubernetes ด้วยวิธีที่ไม่ใช่ Git (Manual Change) ArgoCD จะทำการ Rollback กลับไปเหมือนเดิม

การติดตั้ง ArgoCD บน Kubernetes

ติดตั้ง ArgoCD โดยใช้ kubectl:

kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml

การเข้าถึง ArgoCD Web UI

kubectl port-forward svc/argocd-server -n argocd 8080:443
# เข้าไปที่ https://localhost:8080

ดึงรหัสผ่าน ArgoCD

kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath=\"{.data.password}\" | base64 -d
# ชื่อผู้ใช้: admin

การสร้าง ArgoCD Application

สร้างไฟล์ YAML เพื่อกำหนด Application ใน ArgoCD:

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: nginx-app
  namespace: argocd
spec:
  project: default
  source:
    repoURL: https://github.com/yourusername/your-repo
    targetRevision: main
    path: k8s/nginx
  destination:
    server: https://kubernetes.default.svc
    namespace: default
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
    syncOptions:
    - CreateNamespace=true

ส่วนสำคัญของ Application Manifest

source.repoURL: Git repository URL ที่เก็บไฟล์ Kubernetes YAML

targetRevision: Branch ของ Git repository (main, develop, etc.)

path: Path ไปยังไฟล์ Kubernetes ใน Git repository

destination: Kubernetes cluster และ namespace ที่จะ deploy application

syncPolicy: นโยบายการซิงค์ ทำให้ deployment เป็นไปโดยอัตโนมัติและ rollback เมื่อมีการแก้ไขด้วยตนเอง

การสร้าง Git Repository สำหรับ GitOps

โครงสร้างของ Repository ที่เหมาะสม:

my-gitops-repo/
├── k8s/
│   ├── nginx/
│   │   ├── deployment.yaml
│   │   ├── service.yaml
│   │   └── configmap.yaml
│   ├── database/
│   │   ├── statefulset.yaml
│   │   └── pvc.yaml
│   └── monitoring/
│       └── prometheus.yaml
├── argocd/
│   ├── applications.yaml
│   └── appproject.yaml
└── README.md

การจัดการ ArgoCD ด้วย ArgoCD CLI

ติดตั้ง ArgoCD CLI:

curl -sSL -o argocd-linux-amd64 https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64
chmod +x argocd-linux-amd64
sudo mv argocd-linux-amd64 /usr/local/bin/argocd

เข้าสู่ระบบ ArgoCD:

argocd login localhost:8080 --insecure --username admin --password <password>

สร้าง Application:

argocd app create nginx-app \
  --repo https://github.com/yourusername/your-repo \
  --path k8s/nginx \
  --dest-server https://kubernetes.default.svc \
  --dest-namespace default \
  --sync-policy automated

การตรวจสอบสถานะ Application

argocd app list
argocd app get nginx-app
argocd app logs nginx-app

การซิงค์ Application

argocd app sync nginx-app
# รอให้ Application ซิงค์เสร็จ
argocd app wait nginx-app

การ Rollback Application

argocd app history nginx-app
argocd app rollback nginx-app 0  # Rollback ไปยัง revision แรก

การสร้าง AppProject สำหรับการควบคุมการเข้าถึง

สร้าง AppProject เพื่อกำหนดการเข้าถึงและอนุญาต:

apiVersion: argoproj.io/v1alpha1
kind: AppProject
metadata:
  name: development
  namespace: argocd
spec:
  sourceRepos:
  - 'https://github.com/yourusername/*'
  destinations:
  - namespace: 'dev-*'
    server: https://kubernetes.default.svc
  clusterResourceWhitelist:
  - group: '*'
    kind: '*'
  namespaceResourceBlacklist:
  - group: ''
    kind: ResourceQuota

การรวม ArgoCD กับ GitHub Webhooks

เพื่อให้ ArgoCD ตรวจสอบสถานะโดยอัตโนมัติทันทีที่มีการ push ไป Git:

1. เข้าไปที่ GitHub Repository Settings
2. ไปที่ Webhooks
3. Add webhook ด้วย URL: https://<argocd-server>/api/webhook
4. เลือก events: Push events
5. ทำเครื่องหมาย \"Active\"
6. Click \"Add webhook\"

การติดตั้ง ArgoCD Notifications

ส่งการแจ้งเตือนเมื่อ Application sync สำเร็จหรือล้มเหลว:

kubectl apply -f https://raw.githubusercontent.com/argoproj-labs/argocd-notifications/release-1.0/manifests/install.yaml -n argocd

การกำหนดค่า Notification Service

apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-notifications-cm
  namespace: argocd
data:
  service.slack: |
    token: $slack-token
  trigger.on-sync-failed: |
    - when: sync_operation_phase_failed
      oncePer: app.status.operationState.finishedAt
      send: [on-sync-failed]
  template.on-sync-failed: |
    message: |
      Application {{.app.metadata.name}} sync is {{.app.status.operationState.phase}}.
      {{if eq .app.status.operationState.phase \"Failed\"}}:exclamation:{{end}}

Best Practices สำหรับ GitOps

  • ใส่ทุกไฟล์ Kubernetes ใน Git repository
  • ใช้ Branch strategy (main, develop, feature branches) เพื่อควบคุมการเปลี่ยนแปลง
  • ใช้ Kustomize หรือ Helm เพื่อจัดการการตั้งค่าที่มีความซับซ้อน
  • ทำการ Code Review ก่อนการ merge ไปยัง main branch
  • ใช้ ArgoCD Notifications เพื่อติดตามสถานะ deployment
  • เก็บ Secrets โดยใช้ Sealed Secrets หรือ External Secrets Operator
  • ตั้งค่า RBAC ให้เหมาะสม เพื่อจำกัดการเข้าถึง