GitOps 工作流 Argo CD 自动化部署

FreeGuideOnline 最新 2026-07-08

bash kubectl create namespace argocd


### 部署 Argo CD

使用官方提供的安装清单:

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

等待所有 Pod 就绪:

kubectl wait --for=condition=Ready pods --all -n argocd --timeout=300s

暴露 Argo CD 服务

默认情况下 Argo CD 服务为 ClusterIP,可通过端口转发或修改 Service 类型来访问。

方式一:端口转发(开发/测试环境推荐)

kubectl port-forward svc/argocd-server -n argocd 8080:443

然后通过 https://localhost:8080 访问 UI。

方式二:修改为 NodePort 或 LoadBalancer

kubectl patch svc argocd-server -n argocd -p '{"spec": {"type": "NodePort"}}'

查看分配端口:

kubectl get svc argocd-server -n argocd

获取初始管理员密码

用户名为 admin,密码存储在 Secret 中:

kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d

登录后建议尽快修改密码。

安装 Argo CD CLI(可选)

# macOS
brew install argocd

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

使用 CLI 登录:

argocd login localhost:8080 --username admin --password <上述获取的密码> --insecure

准备 Git 仓库

创建配置仓库

在 GitHub/GitLab 上创建一个新仓库(例如 argocd-demo-config),并克隆到本地:

git clone https://github.com/your-username/argocd-demo-config.git
cd argocd-demo-config

添加 Kubernetes 部署清单

在仓库根目录创建一个 nginx 文件夹,并放入以下两个文件。

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.25.3
          ports:
            - containerPort: 80

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - port: 80
      targetPort: 80
  type: ClusterIP

推送到远程仓库

git add .
git commit -m "Initial Nginx deployment"
git push origin main

注意:本教程假设默认分支为 main,请根据实际情况调整。

在 Argo CD 中创建 Application

Application 是 Argo CD 表示一个部署单元的核心 CRD,它将 Git 仓库、目标集群、配置路径和同步策略关联起来。

方式一:通过 Web UI 创建

  1. 打开 Argo CD UI 并登录。
  2. 点击 “+ New App”
  3. 填写应用基本信息:
    • Application Namenginx-app
    • Projectdefault
    • Sync Policy:可先选择 Manual(后面可改为自动)。
  4. 配置源(Source):
    • Repository URLhttps://github.com/your-username/argocd-demo-config
    • Revisionmain
    • Pathnginx
  5. 目标集群(Destination):
    • Cluster URLhttps://kubernetes.default.svc(表示同一集群)
    • Namespacedefault
  6. 点击 “Create”

方式二:通过 CLI 创建

argocd app create nginx-app \
  --repo https://github.com/your-username/argocd-demo-config \
  --path nginx \
  --dest-server https://kubernetes.default.svc \
  --dest-namespace default

创建后查看状态:

argocd app get nginx-app

此时应用状态为 OutOfSync,因为集群中尚未部署这些资源。

同步(部署)应用

手动同步

在 Web UI 中进入 nginx-app,点击 “Sync”,选择默认选项并点击 “Synchronize”。确认后,Argo CD 将创建 Deployment 和 Service。

通过 CLI 同步:

argocd app sync nginx-app

同步完成后,应用状态变为 Synced 且健康状态为 Healthy

验证部署结果:

kubectl get pods,svc -l app=nginx

启用自动同步

编辑 Application 配置,将同步策略改为自动,并启用自我修复。

Web UI:点击 “App Details” -> “Edit” -> 勾选 “AUTO-SYNC” 和 “SELF HEAL”。

CLI

argocd app set nginx-app --sync-policy automated --auto-prune --self-heal

参数说明:

  • automated:自动应用 Git 中的变更。
  • auto-prune:自动删除 Git 中移除的资源。
  • self-heal:自动修复偏离 Git 期望状态的集群资源。

此时,任何推送到 main 分支的配置变更将在 3 分钟(默认轮询周期) 内被自动同步到集群。

体验 GitOps 自动化部署

修改 nginx/deployment.yaml,将镜像版本改为 nginx:1.25.4 或增加副本数:

image: nginx:1.25.4

提交并推送:

git add .
git commit -m "Update Nginx to 1.25.4"
git push origin main