[ PROMPT_NODE_24654 ]
cicd
[ SKILL_DOCUMENTATION ]
# CI/CD 流水线与 GitOps
## CI/CD 流水线阶段
### 1. 源码 (Source) → 2. 构建 (Build) → 3. 测试 (Test) → 4. 部署 (Deploy) → 5. 监控 (Monitor)
┌──────────┐ ┌───────┐ ┌──────┐ ┌────────┐ ┌─────────┐
│ Source │──▶│ Build │──▶│ Test │──▶│ Deploy │──▶│ Monitor │
│ (Git) │ │ │ │ │ │ │ │ │
└──────────┘ └──────┘ └──────┘ └────────┘ └─────────┘
## GitOps 原则
### 核心概念
1. **Git 作为单一事实来源**:所有配置均存储在 Git 中
2. **声明式**:定义期望状态,而非命令式步骤
3. **自动化**:持续协调期望状态与实际状态
4. **可审计**:所有变更均在 Git 历史中可追踪
### GitOps 工作流
开发者 ──▶ Git Push ──▶ GitOps 控制器 ──▶ Kubernetes 集群
(ArgoCD/Flux)
│
▼
持续同步
## ArgoCD 设置
### 安装
bash
# 安装 ArgoCD
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
# 访问 ArgoCD UI
kubectl port-forward svc/argocd-server -n argocd 8080:443
# 获取初始密码
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d
### ArgoCD 应用
yaml
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: myapp-production
namespace: argocd
finalizers:
- resources-finalizer.argocd.argoproj.io
spec:
project: default
source:
repoURL: https://github.com/myorg/myapp-gitops.git
targetRevision: main
path: kubernetes/overlays/production
destination:
server: https://kubernetes.default.svc
namespace: production
syncPolicy:
automated:
prune: true # 删除不在 Git 中的资源
selfHeal: true # 如果集群状态漂移则进行同步
allowEmpty: false
syncOptions:
- CreateNamespace=true
- PrunePropagationPolicy=foreground
- PruneLast=true
retry:
limit: 5
backoff:
duration: 5s
factor: 2
maxDuration: 3m
ignoreDifferences:
- group: apps
kind: Deployment
jsonPointers:
- /spec/replicas # 忽略 HPA 管理的副本数
### ArgoCD ApplicationSet (多环境)
yaml
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: myapp
namespace: argocd
spec:
generators:
- list:
elements:
- env: dev