12902黄大年茶思屋榜文第129期 第2题:终端场景支持轻量化的快照技术
2026/6/6 0:39:47
graph TD A[Git仓库] --> B[ArgoCD Controller] B --> C[应用状态检测] C --> D{状态一致?} D -->|是| E[保持当前状态] D -->|否| F[同步应用] F --> G[Kubernetes API Server] G --> H[集群状态更新] H --> I[回写Git状态] style A fill:#f9f,stroke:#333,stroke-width:2px style B fill:#bbf,stroke:#333,stroke-width:2px style G fill:#bfb,stroke:#333,stroke-width:2pxGitOps三大核心原则:
| 组件 | 职责 | 关键特性 |
|---|---|---|
| Application Controller | 应用状态管理 | 持续同步、健康检查 |
| Repository Server | Git仓库访问 | 缓存、加密、Webhook |
| Redis | 状态存储 | 缓存应用状态 |
| UI | 可视化管理 | 应用概览、操作界面 |
# 安装ArgoCD kubectl create namespace argocd kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml # 安装ArgoCD CLI brew install argocd # macOS # 或下载二进制 curl -sSL -o argocd-linux-amd64 https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64 sudo install -m 555 argocd-linux-amd64 /usr/local/bin/argocd# 获取初始密码 kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d # 登录ArgoCD argocd login argocd.example.com --username admin --password <password> # 修改密码 argocd account update-passwordargocd app create my-app \ --repo https://github.com/example/app-config.git \ --path k8s/production \ --dest-server https://kubernetes.default.svc \ --dest-namespace default \ --sync-policy automated \ --auto-prune \ --self-healapiVersion: argoproj.io/v1alpha1 kind: Application metadata: name: my-app namespace: argocd spec: project: default source: repoURL: https://github.com/example/app-config.git targetRevision: HEAD path: k8s/production destination: server: https://kubernetes.default.svc namespace: default syncPolicy: automated: prune: true selfHeal: true syncOptions: - CreateNamespace=true - PrunePropagationPolicy=foregroundspec: syncPolicy: automated: prune: true # 自动删除不再需要的资源 selfHeal: true # 自动修复被手动修改的资源 allowEmpty: false syncOptions: - Validate=false # 跳过验证(适用于CRD) - SkipDryRunOnMissingResource=true retry: limit: 5 backoff: duration: 5s factor: 2 maxDuration: 3mapiVersion: argoproj.io/v1alpha1 kind: AppProject metadata: name: production namespace: argocd spec: description: Production applications sourceRepos: - https://github.com/example/* destinations: - server: https://kubernetes.default.svc namespace: production clusterResourceWhitelist: - group: "*" kind: "*" orphanedResources: warn: trueapiVersion: argoproj.io/v1alpha1 kind: Application metadata: name: api-service spec: source: repoURL: https://github.com/example/app-config.git path: k8s/base targetRevision: HEAD helm: parameters: - name: image.tag value: v1.2.3 - name: replicaCount value: "3" valueFiles: - values-production.yamlapiVersion: argoproj.io/v1alpha1 kind: Application metadata: name: web-app spec: source: repoURL: https://github.com/example/app-config.git path: k8s/web destination: server: https://kubernetes.default.svc namespace: default healthChecks: - name: api-health type: HTTP url: http://localhost:8080/health timeout: 30s - name: readiness-probe type: PodExec podName: web-app-* command: ["cat", "/tmp/ready"]name: Deploy to Kubernetes on: push: branches: - main jobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Install ArgoCD CLI run: | curl -sSL -o argocd-linux-amd64 https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64 sudo install -m 555 argocd-linux-amd64 /usr/local/bin/argocd - name: Login to ArgoCD run: | argocd login ${{ secrets.ARGOCD_SERVER }} \ --username ${{ secrets.ARGOCD_USERNAME }} \ --password ${{ secrets.ARGOCD_PASSWORD }} \ --insecure - name: Sync application run: | argocd app sync my-app --prune --forcedeploy: stage: deploy image: alpine:latest before_script: - apk add --no-cache curl - curl -sSL -o argocd https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64 - chmod +x argocd script: - ./argocd login $ARGOCD_SERVER --username $ARGOCD_USER --password $ARGOCD_PWD --insecure - ./argocd app sync my-app --prune --force only: - main# 查看应用历史 argocd app history my-app # 回滚到指定版本 argocd app rollback my-app --revision <commit-hash> # 回滚到上一个版本 argocd app rollback my-appflowchart TD A[灾难发生] --> B[确认影响范围] B --> C[隔离故障组件] C --> D[检查Git状态] D --> E{Git状态正常?} E -->|是| F[ArgoCD自动恢复] E -->|否| G[从备份恢复Git] G --> F F --> H[验证恢复状态] H --> I[恢复流量]apiVersion: apps/v1 kind: Deployment metadata: name: argocd-application-controller namespace: argocd spec: replicas: 2 template: spec: containers: - name: argocd-application-controller resources: requests: cpu: 200m memory: 512Mi limits: cpu: 1 memory: 1Gi args: - --status-processors=20 - --operation-processors=10 - --repo-server-timeout-seconds=60apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: name: argocd-metrics namespace: argocd spec: selector: matchLabels: app.kubernetes.io/name: argocd-metrics endpoints: - port: metrics interval: 30s关键监控指标:
| 指标 | 用途 | 告警阈值 |
|---|---|---|
argocd_app_sync_total | 同步成功率 | < 99% |
argocd_app_sync_duration_seconds | 同步耗时 | > 5min |
argocd_app_health_status | 应用健康状态 | != Healthy |
apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: app-developer namespace: argocd rules: - apiGroups: ["argoproj.io"] resources: ["applications"] verbs: ["get", "list", "watch", "update"]# 使用Sealed Secrets kubectl create secret generic db-password --from-literal=password=secret123 kubeseal --format=yaml --cert=public-key.pem < secret.yaml > sealed-secret.yaml # 使用External Secrets Operator apiVersion: external-secrets.io/v1beta1 kind: ExternalSecret metadata: name: db-credentials spec: secretStoreRef: name: vault-backend kind: SecretStore target: name: db-credentials data: - secretKey: password remoteRef: key: database/production/passwordArgoCD是实现GitOps的最佳工具之一,核心价值在于:
通过GitOps工作流,我们可以实现真正的"一键部署"和"一键回滚",大幅提升运维效率和系统稳定性。
作者简介:侯万里(万里侯),资深运维工程师、云原生专家,专注于AI智能运维领域。让机器自动发现和解决问题,是我的不懈追求。