ArgoCD GitOps 持续部署教程
ArgoCD 是一款面向 Kubernetes 的声明式 GitOps 持续部署工具。它以 Git 仓库作为应用的唯一真实来源,自动将 Git 中定义的应用状态同步到 Kubernetes 集群,实现代码即部署。本文将在搬瓦工 VPS 上的 Kubernetes 环境中部署 ArgoCD 并配置 GitOps 工作流。
一、前置条件
- Kubernetes 集群:需要先搭建 Kubernetes 或 K3S 集群。
- kubectl:已配置并可以连接到集群。
- 内存:ArgoCD 需要约 512MB 额外内存。
- Git 仓库:用于存放 Kubernetes 部署清单。
搬瓦工 VPS 购买地址:bwh81.net,使用优惠码 NODESEEK2026 享受 6.77% 折扣。
二、安装 ArgoCD
2.1 创建命名空间
kubectl create namespace argocd
2.2 部署 ArgoCD
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
2.3 等待所有 Pod 就绪
kubectl wait --for=condition=Ready pods --all -n argocd --timeout=300s
kubectl get pods -n argocd
2.4 暴露 ArgoCD 服务
使用 NodePort 暴露 ArgoCD Server:
kubectl patch svc argocd-server -n argocd -p '{"spec": {"type": "NodePort"}}'
或者使用端口转发:
kubectl port-forward svc/argocd-server -n argocd 8080:443 --address=0.0.0.0 &
三、登录 ArgoCD
3.1 获取初始管理员密码
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d; echo
3.2 安装 ArgoCD CLI
curl -sSL -o argocd-linux-amd64 https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64
install -m 555 argocd-linux-amd64 /usr/local/bin/argocd
rm argocd-linux-amd64
3.3 CLI 登录
argocd login localhost:8080 --username admin --password YOUR_PASSWORD --insecure
建议登录后立即修改密码:
argocd account update-password
四、准备 GitOps 仓库
在 Git 仓库中创建 Kubernetes 部署清单。目录结构示例:
my-app/
├── base/
│ ├── deployment.yaml
│ ├── service.yaml
│ └── kustomization.yaml
└── overlays/
├── staging/
│ └── kustomization.yaml
└── production/
└── kustomization.yaml
示例 base/deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 2
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: nginx:1.25
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "100m"
limits:
memory: "128Mi"
cpu: "250m"
五、创建 ArgoCD Application
5.1 通过 CLI 创建
argocd app create my-app \
--repo https://github.com/yourname/my-gitops-repo.git \
--path my-app/base \
--dest-server https://kubernetes.default.svc \
--dest-namespace default \
--sync-policy automated \
--auto-prune \
--self-heal
5.2 通过 YAML 清单创建
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: my-app
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/yourname/my-gitops-repo.git
targetRevision: HEAD
path: my-app/base
destination:
server: https://kubernetes.default.svc
namespace: default
syncPolicy:
automated:
prune: true
selfHeal: true
syncOptions:
- CreateNamespace=true
kubectl apply -f application.yaml
六、同步策略
- 自动同步(Automated):Git 仓库变更后自动同步到集群。
- 手动同步(Manual):需要手动点击同步按钮或使用 CLI 触发。
- Self-Heal:当集群中的资源被手动修改时,自动恢复为 Git 中的状态。
- Prune:当资源从 Git 中删除时,自动从集群中删除。
# 手动触发同步
argocd app sync my-app
# 查看应用状态
argocd app get my-app
# 查看同步历史
argocd app history my-app
七、回滚操作
# 查看应用历史版本
argocd app history my-app
# 回滚到指定版本
argocd app rollback my-app 2
# 通过 Git revert 回滚(推荐的 GitOps 方式)
cd my-gitops-repo
git revert HEAD
git push origin main
八、添加私有仓库
# 使用 HTTPS + Token
argocd repo add https://github.com/yourname/private-repo.git \
--username git \
--password YOUR_TOKEN
# 使用 SSH
argocd repo add git@github.com:yourname/private-repo.git \
--ssh-private-key-path ~/.ssh/id_rsa
九、常见问题
应用状态显示 OutOfSync
查看差异详情:
argocd app diff my-app
常见原因是集群中存在被手动修改的资源,启用 Self-Heal 可自动修复。
同步失败
argocd app get my-app
kubectl get events -n default --sort-by='.lastTimestamp'
资源不足
ArgoCD 本身需要一定的资源,如果 VPS 内存不足,可以减少 ArgoCD 组件的资源请求。
总结
ArgoCD 为 Kubernetes 应用提供了优雅的 GitOps 持续部署方案,以 Git 作为唯一真实来源,确保集群状态与代码定义一致。搭配 Kubernetes 或 K3S 使用效果最佳。选购搬瓦工 VPS 请参考 全部方案,使用优惠码 NODESEEK2026 享受 6.77% 折扣。