Harness

举报
yd_292659020 发表于 2026/11/10 01:09:01 2026/11/10
【摘要】 深入理解 Harness:下一代持续交付平台本文将系统介绍 Harness 平台的核心架构、关键能力、Pipeline 设计模式以及与云原生生态的集成实践。 目录1. 什么是 Harness2. 核心架构3. 关键概念4. Pipeline 设计5. 部署策略6. 与云原生生态集成7. GitOps 与 Infrastructure as Code8. Feature Flags(功能开关...

深入理解 Harness:下一代持续交付平台

本文将系统介绍 Harness 平台的核心架构、关键能力、Pipeline 设计模式以及与云原生生态的集成实践。

目录


1. 什么是 Harness

Harness 是由 Jyoti Bansal(AppDynamics 创始人)于 2016 年创立的软件交付平台,其核心理念是:

“让每一次部署都像点击一个按钮一样简单、安全、可回滚。”

Harness 不是传统意义上的 CI 工具,而是一个端到端的持续交付(Continuous Delivery, CD)平台,覆盖从代码提交到生产部署的完整生命周期:

代码提交 → 构建 → 测试 → 部署 → 验证 → 监控 → 回滚
  CI          CD        Verification    Governance

1.1 平台演进

阶段 时间 关键能力
初创 2016-2018 持续部署引擎、部署验证
扩展 2019-2020 Feature Flags、Cloud Cost Management
平台化 2021-2023 Pipeline as Code、GitOps、Template Library
AI 驱动 2024-至今 AIDA(AI Development Assistant)、智能异常诊断

2. 核心架构

Harness 平台采用模块化微服务架构,各模块可独立启用:

┌─────────────────────────────────────────────────┐
│                  Harness Platform                │
├──────────┬──────────┬──────────┬────────────────┤
│   CI     │   CD     │  Feature │  Cloud Cost    │
│ (Build)  │ (Deploy) │  Flags   │  Management    │
├──────────┴──────────┴──────────┴────────────────┤
│              Pipeline Engine (Core)              │
├──────────────────────────────────────────────────┤
│     Git Connector / Cloud Connector / Secret     │
├──────────────────────────────────────────────────┤
│     Delegate (在你的基础设施中运行的轻量 Agent)    │
└──────────────────────────────────────────────────┘

2.1 Delegate 机制

Harness 不直接访问你的基础设施,而是通过 Delegate(委托代理)模式工作:

  • Harness Delegate 是一个在你自己的网络中运行的轻量级服务(Kubernetes Pod 或 Docker 容器)
  • 所有对云资源的操作都通过 Delegate 执行,Harness SaaS 只下发指令
  • 这意味着:你的凭证永远不离开你的网络
# Delegate 在 Kubernetes 中的部署示意
apiVersion: apps/v1
kind: Deployment
metadata:
  name: harness-delegate
spec:
  replicas: 1
  selector:
    matchLabels:
      app: harness-delegate
  template:
    spec:
      containers:
      - name: delegate
        image: harness/delegate:latest
        env:
        - name: ACCOUNT_ID
          valueFrom:
            secretKeyRef:
              name: harness-secrets
              key: account-id
        - name: DELEGATE_TOKEN
          valueFrom:
            secretKeyRef:
              name: harness-secrets
              key: delegate-token

安全要点:Delegate 通过出站 HTTPS 连接与 Harness SaaS 通信,不需要在防火墙上开放入站端口。这是 Harness 在金融、医疗等强合规行业被广泛采用的原因之一。

2.2 两种部署模式

模式 说明 适用场景
Harness SaaS 平台托管在 Harness 云端,Delegate 运行在你的基础设施中 大多数团队,免运维
Harness Self-Managed 整个平台部署在你的 K8s 集群中 强合规要求、数据驻留要求

3. 关键概念

3.1 Organization → Project → Pipeline

Harness 采用三级组织结构:

Organization (组织)
  └── Project (项目)
        └── Pipeline (流水线)
              ├── Stage (阶段)
              │     └── Step (步骤)
              └── Stage
                    └── Step
  • Organization:对应一个公司或大部门,管理所有连接器和密钥
  • Project:对应一个团队或产品线,隔离 Pipeline 和环境
  • Pipeline:一条完整的交付流水线

3.2 Environment 与 Service

这两个概念是 Harness CD 的基石:

  • Service:你要部署的东西(一个 Docker 镜像、一个 Helm Chart、一个 Terraform 模块)
  • Environment:你要部署到哪里(Dev、Staging、Production)
# Service 定义示例
service:
  name: "my-microservice"
  identifier: "my_microservice"
  type: "Kubernetes"
  spec:
    manifests:
      - type: "K8sManifest"
        identifier: "main_manifest"
        spec:
          store:
            type: "Github"
            spec:
              connectorRef: "github_connector"
              paths:
                - "deploy/k8s/deployment.yaml"

3.3 Connector(连接器)

Connector 是 Harness 与外部系统交互的桥梁,支持 50+ 种集成:

类别 示例
代码仓库 GitHub、GitLab、Bitbucket、Azure Repos
容器仓库 Docker Hub、ECR、GCR、Harbor
云平台 AWS、Azure、GCP、Huawei Cloud
配置管理 HashiCorp Vault、AWS Secrets Manager
监控 Datadog、Prometheus、AppDynamics

4. Pipeline 设计

4.1 YAML-first 的 Pipeline

Harness 的 Pipeline 以 YAML 定义,可以存储在 Git 仓库中(Git-backed Pipeline),实现 Pipeline as Code:

pipeline:
  name: "Microservice CD Pipeline"
  identifier: "microservice_cd"
  projectIdentifier: "payment_team"
  orgIdentifier: "default"
  stages:
    # ---- Stage 1: 构建 ----
    - stage:
        name: "Build"
        identifier: "build"
        type: "CI"
        spec:
          execution:
            steps:
              - step:
                  type: "BuildAndPushDockerRegistry"
                  name: "Build and Push"
                  identifier: "build_push"
                  spec:
                    connectorRef: "docker_hub_connector"
                    repo: "myorg/payment-service"
                    tags:
                      - "latest"

    # ---- Stage 2: 部署到 Staging ----
    - stage:
        name: "Deploy to Staging"
        identifier: "deploy_staging"
        type: "Deployment"
        spec:
          serviceRef: "payment_service"
          environmentRef: "staging_env"
          execution:
            steps:
              - step:
                  type: "Canary"
                  name: "Canary Deploy"
                  identifier: "canary"
                  spec:
                    instances:
                      type: "Percentage"
                      spec:
                        value: 25
              - step:
                  type: "Verify"
                  name: "Health Check"
                  identifier: "verify"
                  spec:
                    continuousVerificationSpec:
                      type: "Prometheus"
                      spec:
                        query: "rate(http_requests_total{status=~\"5..\"}[5m])"

    # ---- Stage 3: 审批后部署到 Production ----
    - stage:
        name: "Deploy to Production"
        identifier: "deploy_prod"
        type: "Deployment"
        spec:
          serviceRef: "payment_service"
          environmentRef: "production_env"
          execution:
            steps:
              - step:
                  type: "Approval"
                  name: "Manual Approval"
                  identifier: "approval"
                  spec:
                    approvers:
                      userGroups:
                        - "production_approvers"
              - step:
                  type: "BlueGreen"
                  name: "Blue Green Deploy"
                  identifier: "bluegreen"

4.2 表达式引擎(Expression Engine)

Harness 内置了一个强大的表达式引擎,使用 <+...> 语法在 Pipeline 中引用动态值:

# 引用 Pipeline 变量
image: "<+pipeline.variables.image_tag>"

# 引用上一步的输出
artifact: "<+pipeline.executionStages.build.spec.execution.steps.build_push.artifacts.image.tag>"

# 引用 Service / Environment 的配置
namespace: "<+env.variables.namespace>"

# 条件判断
when: "<+pipeline.variables.env> == \"production\""

这让你可以在一条 Pipeline 中处理多环境部署,而不需要为每个环境写一条单独的 Pipeline。

4.3 Template Library(模板库)

对于有大量微服务的团队,Harness 提供 Template 机制来复用 Pipeline 逻辑:

# 定义一个通用的部署模板
template:
  name: "Standard K8s Deploy"
  identifier: "standard_k8s_deploy"
  type: "Stage"
  spec:
    type: "Deployment"
    spec:
      execution:
        steps:
          - step:
              type: "Canary"
              # ... 标准化的金丝雀部署步骤

然后在具体 Pipeline 中引用:

stages:
  - stage:
      template:
        templateRef: "standard_k8s_deploy"
        templateInputs:
          serviceRef: "<+pipeline.variables.service>"
          environmentRef: "<+pipeline.variables.env>"

实践建议:当微服务数量超过 10 个时,强烈建议使用 Template 统一部署逻辑。修改一处,所有引用该 Template 的 Pipeline 自动生效。


5. 部署策略

Harness 原生支持多种部署策略,每种都有对应的回滚机制:

5.1 Canary(金丝雀部署)

逐步将流量切到新版本,先小比例验证再全量发布:

v1 (100%) ──→ v1 (75%) + v2 (25%) ──→ v1 (50%) + v2 (50%) ──→ v2 (100%)
                                    ↑
                              自动健康检查
                              失败则回滚到 v1
- step:
    type: "Canary"
    spec:
      instances:
        type: "Percentage"
        spec:
          value: 25    # 先部署 25%
      # Harness 会自动等待并验证,然后继续
- step:
    type: "Canary"
    spec:
      instances:
        type: "Percentage"
        spec:
          value: 100   # 验证通过后,全量部署

5.2 Blue-Green(蓝绿部署)

维护两套完全相同的环境,切换流量实现零停机部署:

Blue (当前生产, 接收流量)     Green (新版本, 待命)
         │                          │
         └──── 流量切换 ────────────┘
         
切换后: Green → 生产, Blue → 待命(可快速回滚)

5.3 Rolling(滚动部署)

逐个替换实例,适合无状态服务:

- step:
    type: "Rolling"
    spec:
      instances:
        type: "Count"
        spec:
          value: 2    # 每次更新 2 个实例

5.4 部署验证(Continuous Verification)

这是 Harness 最具差异化的能力 —— 部署后自动验证,而非仅仅"部署成功":

- step:
    type: "Verify"
    name: "Auto Verification"
    spec:
      continuousVerificationSpec:
        type: "Prometheus"
        spec:
          # 监控指标:5xx 错误率
          query: |
            sum(rate(http_requests_total{status=~"5.."}[5m])) 
            / sum(rate(http_requests_total[5m]))
          # 阈值:错误率超过 5% 自动回滚
          analysis:
            type: "THRESHOLD"
            spec:
              max: "0.05"

Harness 支持多种验证源:

验证源 用途
Prometheus 指标异常检测
Datadog APM + 日志联合分析
AppDynamics 业务事务级别验证
Custom HTTP 调用自定义健康检查接口
Log Analytics 日志错误模式匹配

核心理念:不是"部署完了就成功了",而是"部署完了 + 验证通过 = 成功"。如果验证失败,Harness 自动触发回滚,无需人工介入。


6. 与云原生生态集成

6.1 Kubernetes 部署

Harness 对 Kubernetes 有一等公民级别的支持:

service:
  type: "Kubernetes"
  spec:
    manifests:
      # 直接引用 Git 中的 K8s manifest
      - type: "K8sManifest"
        spec:
          store:
            type: "Github"
            spec:
              paths: ["deploy/deployment.yaml"]
      # 或使用 Helm Chart
      - type: "Helm"
        spec:
          chart:
            type: "Github"
            spec:
              paths: ["charts/payment-service"]
          values: |
            replicaCount: 3
            image:
              tag: <+pipeline.variables.image_tag>
            resources:
              limits:
                cpu: 500m
                memory: 512Mi

6.2 Helm 集成

Harness 可以作为 Helm 的编排层,在部署时动态注入值:

- step:
    type: "HelmDeploy"
    spec:
      chartRef: "payment-service-chart"
      values: |
        # 从 Harness Secret 中注入敏感值
        database:
          password: <+secrets.getValue("db_password")>
        # 根据环境动态配置
        replicaCount: <+env.variables.replicas>
        ingress:
          host: <+env.variables.ingress_host>

6.3 与 Terraform 集成

Harness 可以编排 Terraform,实现基础设施与应用的统一交付:

- stage:
    name: "Provision Infrastructure"
    type: "Custom"
    spec:
      execution:
        steps:
          - step:
              type: "TerraformApply"
              spec:
                configurationPath: "infra/"
                workspace: "<+env.variables.tf_workspace>"
                variables:
                  - name: "region"
                    value: "<+env.variables.region>"
                  - name: "instance_count"
                    value: "3"

这样你可以实现:

Pipeline 触发
  → Stage 1: Terraform apply(创建/更新基础设施)
  → Stage 2: Build & Push(构建应用镜像)
  → Stage 3: Deploy(部署应用到新基础设施)
  → Stage 4: Verify(验证部署健康)

7. GitOps 与 Infrastructure as Code

7.1 Git-based Pipeline(GitOps 模式)

Harness 支持将 Pipeline YAML 完全存储在 Git 仓库中:

你的 Git 仓库
  ├── .harness/
  │     ├── pipelines/
  │     │     ├── deploy-staging.yaml
  │     │     └── deploy-production.yaml
  │     ├── templates/
  │     │     └── standard-deploy.yaml
  │     └── services/
  │           └── payment-service.yaml
  └── src/
        └── ...

工作流变为:

  1. 开发者修改 Pipeline YAML 并提交 PR
  2. 团队 Review Pipeline 变更
  3. 合并后 Harness 自动同步最新 Pipeline 定义
  4. 下次部署即使用新 Pipeline

好处:Pipeline 变更和代码变更一样有版本历史、有 Review 流程、有审计日志。

7.2 GitOps 部署模式

Harness 也支持 ArgoCD 风格的 GitOps 部署:

- step:
    type: "GitOps"
    spec:
      gitOpsConnector: "github_connector"
      repo: "myorg/gitops-manifests"
      branch: "production"
      path: "apps/payment-service"
      # Harness 更新 Git 仓库中的 manifest
      # ArgoCD 或 Flux 检测到变更并执行部署

8. Feature Flags(功能开关)

Harness Feature Flags 让你将代码部署与功能发布解耦:

# Python 示例:在代码中引用 Feature Flag
from harness_ff import FFClient

client = FFClient(api_key="your-api-key")

if client.is_enabled("new_checkout_flow", target=user_id):
    # 新版结账流程
    return new_checkout_handler(request)
else:
    # 旧版结账流程
    return legacy_checkout_handler(request)

8.1 渐进式发布

# 通过 Pipeline 控制 Feature Flag 的渐进式开启
- step:
    type: "FeatureFlag"
    spec:
      flag: "new_checkout_flow"
      # 先对 5% 的用户开启
      rollout:
        type: "Percentage"
        percentage: 5

工作流:

部署代码(功能默认关闭)
  → 开启 5% 用户 → 验证指标
  → 开启 25% 用户 → 验证指标
  → 开启 100% 用户 → 全量发布
  → 如有问题:一键关闭,无需重新部署

关键价值:代码部署是高风险操作(需要构建、测试、替换实例),而 Feature Flag 开关是低风险操作(一次 API 调用,毫秒级生效)。将两者解耦是现代发布工程的核心实践。


9. Cloud Cost Management

Harness 还提供云成本管理模块,与 CD 流程形成闭环:

部署新版本 → Cloud Cost Management 自动追踪新版本资源消耗
          → 与旧版本对比成本
          → 发现异常增长 → 告警

核心能力:

  • 成本可视化:按 Service、Environment、Team 维度拆分云成本
  • 闲置资源检测:自动识别未使用的 ECS/EC2、未挂载的云盘
  • 成本异常告警:成本突增时自动通知
  • 优化建议:基于使用模式推荐规格降配、预留实例购买

10. 最佳实践

10.1 Pipeline 设计原则

原则 说明
单一职责 一条 Pipeline 只负责一个 Service 的交付
环境渐进 Dev → Staging → Production,前一个 Stage 失败则不进入下一个
自动验证 每次部署后必须有 Verify Step
快速失败 在 Pipeline 早期就做 lint、unit test,避免到后面才发现问题
幂等性 Pipeline 可以安全重跑,不会产生副作用

10.2 安全实践

# 1. 敏感值使用 Harness Secret,不硬编码
database_password: <+secrets.getValue("db_password")>

# 2. Production 部署必须有人工审批
- step:
    type: "Approval"
    spec:
      approvers:
        userGroups: ["production_approvers"]
      minApprovals: 2    # 至少 2 人审批

# 3. 使用 RBAC 限制 Pipeline 执行权限
# 在 Project 级别配置角色:
# - Developer: 只能触发非 Production Pipeline
# - Release Manager: 可以触发所有 Pipeline

10.3 监控与可观测性

# 在 Pipeline 中嵌入通知
- step:
    type: "Notification"
    spec:
      type: "Slack"
      spec:
        channel: "#releases"
        message: |
          部署状态: <+pipeline.status>
          服务: <+pipeline.variables.service>
          环境: <+pipeline.variables.env>
          提交: <+pipeline.gitCommit>
          执行人: <+pipeline.triggeredBy>

10.4 常见反模式

反模式 问题 正确做法
一个巨型 Pipeline 管所有服务 难以维护,一个失败全阻塞 每个服务独立 Pipeline
跳过 Staging 直接部署 Production 缺少验证,风险高 强制 Dev → Staging → Prod
部署后不做验证 “部署成功” ≠ “服务正常” 加 Verify Step
手动修改 Production 配置 配置漂移,不可追溯 所有变更走 Pipeline

11. Harness vs Jenkins vs GitLab CI

维度 Harness Jenkins GitLab CI
定位 端到端 CD 平台 通用 CI/CD 引擎 Git 内置 CI/CD
Pipeline as Code YAML(Git-backed) Jenkinsfile (Groovy) .gitlab-ci.yml
部署验证 内置 CV,自动回滚 需插件 需自行实现
Feature Flags 内置 需第三方 需第三方
多云支持 原生支持 AWS/Azure/GCP/K8s 依赖插件 依赖插件
UI/UX 现代 Web UI 插件拼凑,体验不一 较好
学习曲线 中等(概念较多) 低(入门简单) 低(与 Git 集成)
扩展性 Template + Step Library 插件生态丰富 有限
成本 商业产品,按用户/资源收费 开源免费 免费(内置)
适用规模 中大型团队、多服务 任何规模 中小型团队

选型建议:

  • 小团队 / 单一云:GitLab CI 足够,简单直接
  • 中大型团队 / 多服务 / 多云:Harness 的统一编排 + 自动验证价值显著
  • 已有 Jenkins 深度定制:评估迁移成本,可先用 Harness 做 CD 层,保留 Jenkins 做 CI

12. 总结

Harness 的核心价值不在于"又一个 CI/CD 工具",而在于它将以下能力整合在一个平台中:

  1. Pipeline as Code — 流水线即代码,版本化、可 Review
  2. Continuous Verification — 部署后自动验证,失败自动回滚
  3. 多策略部署 — Canary / Blue-Green / Rolling 开箱即用
  4. Feature Flags — 解耦部署与发布,降低风险
  5. GitOps — Git 作为唯一事实来源,声明式交付
  6. 多云编排 — 统一管理 AWS / Azure / GCP / K8s 的部署
  7. Template 复用 — 标准化部署逻辑,减少重复

在云原生和微服务架构下,服务数量动辄数十上百,传统的脚本式部署已经难以为继。Harness 这类平台化 CD 工具的价值在于:用声明式的方式管理复杂度,用自动验证的方式保障质量,用模板复用的方式提升效率。

如果你的团队正在经历"部署靠脚本、回滚靠人肉、出事靠运气"的阶段,是时候考虑引入一个系统化的 CD 平台了。Harness 值得认真评估。


参考资料:


本文由技术博客生成,转载请注明出处。

【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0)

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。