希望把 Kubernetes 的部署状态收敛到 Git 仓库,由集群持续拉取并对账,而不是在 CI 里直接执行 kubectl apply。适合多环境、多集群和需要审计变更记录的团队。
kubectl 管理权限。kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
kubectl -n argocd rollout status deploy/argocd-server
kubectl -n argocd port-forward svc/argocd-server 8080:443
kubectl -n argocd get secret argocd-initial-admin-secret \
-o jsonpath='{.data.password}' | base64 -d
生产环境建议通过 Ingress/Gateway 暴露,并接入 OIDC 登录,不要长期使用 port-forward 和 admin 本地账号。
argocd login localhost:8080 --username admin --insecure
argocd repo add https://github.com/example/gitops.git \
--username git --password "$GIT_TOKEN"
argocd app create demo \
--repo https://github.com/example/gitops.git \
--path apps/demo/overlays/prod \
--dest-server https://kubernetes.default.svc \
--dest-namespace demo \
--sync-policy automated \
--auto-prune \
--self-heal
argocd app diff demo
argocd app sync demo
argocd app get demo
确认稳定后再开启自动同步。删除资源前先确认 auto-prune 的影响范围,生产环境可先用 --sync-option Prune=false 做灰度。
需要按团队隔离时,创建 AppProject 限制可部署的仓库、集群和命名空间:
argocd proj create team-a \
--src https://github.com/example/gitops.git \
--dest https://kubernetes.default.svc,demo
argocd app get demo 的 Sync 状态为 Synced,Health 为 Healthy。self-heal 能把资源恢复。argocd app history demo 能看到每次同步对应的 Git 提交。argocd app rollback demo <历史ID>,但下一次自动同步仍会回到 Git 状态。argocd app set demo --sync-policy none,再手工处理。auto-prune 会删除 Git 中移除的资源,首次开启前先看 diff。lookup 函数在 Argo CD 渲染时可能拿不到集群状态。ComparisonError,先看 repo-server 日志。destination.namespace 不会自动创建命名空间,必要时加 CreateNamespace=true。argocd — CLI 登录、创建应用、同步与回滚kubectl — 查看 Argo CD 组件和受管资源helm — 管理 chart 版本kustomize — 渲染多环境清单