GitOps 核心思想
Vibe Prompt
「解釋 GitOps 與傳統部署的差異,為什麼 GitOps 更適合 K8s。」
傳統 vs GitOps
| 面向 | 傳統部署 | GitOps |
|------|---------|--------|
| 觸發方式 | 手動 SSH / CI/CD push | Git 提交自動同步 |
| 狀態來源 | 伺服器當前狀態 | Git Repo 宣告式設定 |
| 回滾方式 | 手動重部署 | git revert |
| 審計軌跡 | 無 | Git History |
| 災難復原 | 重新部署 | argocd app sync |
ArgoCD 安裝
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
# 取得密碼
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d
# 連接 ArgoCD
argocd login localhost:8080
argocd app create myapp --repo https://github.com/me/myapp.git --path k8s --dest-server https://kubernetes.default.svc --dest-namespace default
argocd app sync myapp
Application YAML
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: myapp
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/me/myapp.git
path: k8s/overlays/production
targetRevision: main
destination:
server: https://kubernetes.default.svc
namespace: production
syncPolicy:
automated:
prune: true
selfHeal: true
Sync 策略
- Manual:手動觸發同步
- Automated:自動同步(prune = 刪除多餘資源,selfHeal = 自動修復漂移)