DevOps 是一种融合开发(Development)与运维(Operations)的文化、实践和工具的协作范式,旨在通过自动化

📅 2026/6/16 22:13:13
DevOps 是一种融合开发(Development)与运维(Operations)的文化、实践和工具的协作范式,旨在通过自动化
基于 GitOpsArgo 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 # 副本数3HPA镜像 tag: v1.2.0语义化PSA: restrictedTLS 强制 ├── bases/ │ └── nginx-app/ # 公共基础 manifestDeployment, 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 启用 kustomizeplugin:name:kustomizedestination:server:https://kubernetes.default.svc# 或外部 API Server 地址namespace:nginx-prodsyncPolicy:automated:prune:true# 删除 Git 中已移除的资源selfHeal:true# 自动修复手动篡改如 kubectl editsyncOptions:-CreateNamespacetrue-ApplyOutOfSyncOnlytrueignoreDifferences:-group:appskind:DeploymentjsonPointers:-/spec/revisionHistoryLimit# 忽略历史版本数差异便于人工调试环境差异化通过 Kustomize Overlay 实现environments/prod/kustomization.yamlapiVersion:kustomize.config.k8s.io/v1beta1kind:Kustomizationresources:-../../bases/nginx-apppatchesStrategicMerge:-deployment-patch.yaml# 修改 replicas、image tag、resourcesconfigMapGenerator:-name:app-configliterals:-ENVproduction-LOG_LEVELwarnimages:-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 镜像签名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 控制流量切分与指标驱动的渐进发布⚠️ 注意事项避免在 Git 中硬编码敏感信息 → 使用 External Secrets Operator Vault/ AWS Secrets Manager 同步 secret 到 K8stargetRevision推荐使用 Git tag如v1.2.0而非main分支保障 prod 可重现、可审计对 prod 环境启用auto-sync: false 手动批准argocd app sync nginx-app-prod --yes符合变更控制规范ITIL/ISO27001DevOps 是一种融合开发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:64Micpu:250mlimits:memory:128Micpu:500mDevOps KubernetesThe 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.