DevOps 是一种融合开发(Development)与运维(Operations)的文化、实践和工具的协作范式,旨在通过自动化
基于 GitOps(Argo CD)实现多环境(dev/staging/prod)差异化部署,核心思想是:“环境即代码” + “配置分离” + “声明式同步”。Argo CD 通过监听 Git 仓库中不同分支、目录或 Helm values 文件的变化,自动将对应环境的 Kubernetes 清单(YAML)同步到目标集群,确保环境状态与 Git 一致(Git as Single Source of Truth)。
✅ 推荐实践架构(推荐使用Application-per-Environment + Kustomize 分层模式):
repo/ ├── apps/ # 所有应用定义(Argo CD Application CR) │ ├── nginx-app-dev.yaml # 指向 dev 环境配置 │ ├── nginx-app-staging.yaml │ └── nginx-app-prod.yaml ├── environments/ │ ├── dev/ │ │ ├── kustomization.yaml # base + dev overlay(副本数=1,无资源限制,启用debug) │ │ └── configmap.yaml │ ├── staging/ │ │ ├── kustomization.yaml # 副本数=2,启用监控探针,镜像 tag: latest-staging │ └── prod/ │ ├── kustomization.yaml # 副本数=3+HPA,镜像 tag: v1.2.0(语义化),PSA: restricted,TLS 强制 ├── bases/ │ └── nginx-app/ # 公共基础 manifest(Deployment, Service, IngressClass) └── clusters/ ├── dev-cluster.yaml # Argo CD Cluster Secret(可选,若跨集群) └── prod-cluster.yaml🔧 关键步骤详解:
定义 Argo CD Application CR(每个环境一个)
apps/nginx-app-prod.yaml示例:apiVersion:argoproj.io/v1alpha1kind:Applicationmetadata:name:nginx-app-prodnamespace:argocdspec:project:defaultsource:repoURL:https://github.com/your-org/infra.gittargetRevision:mainpath:environments/prod# 使用 Kustomize 构建引擎(需 Argo CD 启用 kustomize)plugin:name:kustomizedestination:server:https://kubernetes.default.svc# 或外部 API Server 地址namespace:nginx-prodsyncPolicy:automated:prune:true# 删除 Git 中已移除的资源selfHeal:true# 自动修复手动篡改(如 kubectl edit)syncOptions:-CreateNamespace=true-ApplyOutOfSyncOnly=trueignoreDifferences:-group:appskind:DeploymentjsonPointers:-/spec/revisionHistoryLimit# 忽略历史版本数差异(便于人工调试)环境差异化通过 Kustomize Overlay 实现
environments/prod/kustomization.yaml:apiVersion:kustomize.config.k8s.io/v1beta1kind:Kustomizationresources:-../../bases/nginx-apppatchesStrategicMerge:-deployment-patch.yaml# 修改 replicas、image tag、resourcesconfigMapGenerator:-name:app-configliterals:-ENV=production-LOG_LEVEL=warnimages:-name:nginxnewTag:v1.2.0安全与治理增强
- ✅ 使用 Argo CD Projects 隔离权限(如
prod-project仅允许 CI/CD 服务账户 + SRE 组同步) - ✅ 启用
Sync Windows(如仅允许工作日 9:00–18:00 同步 prod) - ✅ 结合
App-of-Apps模式管理整个环境生命周期(如prod-envApplication 负责部署所有 prod 应用) - ✅ 镜像签名验证:集成 Cosign + Notary v2,通过 Argo CD 的
verifySignature配置校验 OCI 镜像签名
- ✅ 使用 Argo CD Projects 隔离权限(如
CI/CD 协同(发布流程)
- 开发提交 → CI(如 GitHub Actions)构建镜像 → 推送至 registry → 更新
environments/staging/kustomization.yaml中images:字段 → Git push → Argo CD 自动同步 staging - 人工审批后,将 staging 的
kustomization.yaml变更合并至prod/目录(或打 Git tag)→ 触发 prod 同步 - (进阶)结合 Argo Rollouts 实现金丝雀发布:
Application控制蓝绿基线,RolloutCR 控制流量切分与指标驱动的渐进发布
- 开发提交 → CI(如 GitHub Actions)构建镜像 → 推送至 registry → 更新
⚠️ 注意事项:
- 避免在 Git 中硬编码敏感信息 → 使用 External Secrets Operator + Vault/ AWS Secrets Manager 同步 secret 到 K8s
targetRevision推荐使用 Git tag(如v1.2.0)而非main分支,保障 prod 可重现、可审计- 对 prod 环境启用
auto-sync: false+ 手动批准(argocd app sync nginx-app-prod --yes),符合变更控制规范(ITIL/ISO27001)
DevOps 是一种融合开发(Development)与运维(Operations)的文化、实践和工具的协作范式,旨在通过自动化、持续集成/持续交付(CI/CD)、监控与反馈闭环,实现软件更快速、可靠、安全的构建、测试、部署和运维。Kubernetes(简称 K8s)是 CNCF 毕业的开源容器编排平台,是 DevOps 实践中支撑云原生应用弹性伸缩、高可用、声明式部署与服务治理的核心基础设施。
二者关系紧密:
✅ DevOps 提供方法论与流程(如 GitOps、SRE 实践、可观测性文化);
✅ Kubernetes 提供落地载体——统一调度容器化工作负载,支撑 CI/CD 流水线中的构建环境、测试集群、预发/生产环境的一致性;
✅ 典型整合场景包括:
- 使用 Argo CD / Flux 实现 GitOps 驱动的 Kubernetes 应用交付;
- Jenkins/GitLab CI 调用 kubectl 或 Helm 部署到 K8s 集群;
- Prometheus + Grafana + ELK 实现 K8s 全栈可观测性,赋能 DevOps 快速定位故障;
- 使用 Tekton 或 GitHub Actions 构建云原生 CI/CD 流水线,原生适配 K8s 原语(Pod 作为构建代理)。
关键能力协同:
🔹 自动化部署 →kubectl apply -f/ Helm / Kustomize
🔹 环境一致性 → 容器镜像 + 声明式 YAML = “一次构建,随处部署”
🔹 弹性与韧性 → K8s 自愈(重启 Pod)、滚动更新、健康检查 → 降低发布风险
🔹 安全左移 → Trivy 扫描镜像、OPA/Gatekeeper 策略即代码、K8s RBAC 与 PodSecurityPolicy(或 PSA)
# 示例:一个符合 DevOps 最佳实践的 K8s Deployment(含就绪/存活探针、资源限制、标签)apiVersion:apps/v1kind:Deploymentmetadata:name:nginx-applabels:app:nginxspec:replicas:3selector:matchLabels:app:nginxtemplate:metadata:labels:app:nginxspec:containers:-name:nginximage:nginx:1.25ports:-containerPort:80livenessProbe:httpGet:path:/healthzport:80initialDelaySeconds:30readinessProbe:httpGet:path:/readyzport:80initialDelaySeconds:5resources:requests:memory:"64Mi"cpu:"250m"limits:memory:"128Mi"cpu:"500m"DevOps & Kubernetes
The DevOps approach goes hand-in-hand with Linux® containers, which give your team the underlying technology needed for a cloud-native development style. Containers support a unified environment for development, delivery, integration, and automation.
And Kubernetes is the modern way to automate Linux container operations. Kubernetes helps you easily and efficiently manage clusters running Linux containers across public, private, or hybrid clouds.
Choosing reliable platforms, both inside and outside the container—like Red Hat® Enterprise Linux and Red Hat OpenShift® Container Platform—ensures that scaling and automation won’t fail when you need it most. With the right platforms, you can best take advantage of the culture and process changes you’ve implemented.
