GitOps 與 ArgoCD 實戰

DevOps 2025-01-02T08:00:00.000Z

GitOps 與 ArgoCD 實戰

GitOps 以 Git 為唯一的真理來源,實現宣告式的持續部署。

GitOps 核心原則

  1. 宣告式:系統狀態以 YAML 描述
  2. 版本控制:所有變更透過 Git 管理
  3. 自動同步:工具自動將 Git 狀態套用到集群
  4. 可觀測:狀態差異即時可見

ArgoCD 安裝

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

Application 定義

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: myapp
  namespace: argocd
spec:
  project: default
  source:
    repoURL: https://github.com/example/myapp.git
    targetRevision: HEAD
    path: k8s
  destination:
    server: https://kubernetes.default.svc
    namespace: default
  syncPolicy:
    automated:
      prune: true
      selfHeal: true

最佳實踐

  • 使用獨立 Git repo 存放 Kubernetes manifests
  • 設定自動同步但關閉 auto-prune
  • 使用 Secrets Manager 管理敏感資料
  • 設定通知提醒同步失敗