Gremlin 混沌工程平台

Gremlin 是全球领先的 SaaS 混沌工程平台,提供企业级的故障注入能力、安全控制和工作流自动化。

Gremlin 与其他开源工具最大的区别在于企业级特性:完善的安全机制、多人协作、实验报告、合规支持。这些特性让 Gremlin 成为大型企业落地混沌工程的首选。

核心特性

特性说明
攻击类型20+ 种故障类型,覆盖基础设施到应用层
安全机制自动停止、实验限制、紧急停止按钮
团队协作多用户、权限管理、审批流程
报告分析实验报告、趋势分析、合规导出
平台支持AWS/GCP/Azure/K8s/物理机

安装

Kubernetes

gremlin-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: gremlin
data:
  GREMLIN_TEAM_ID: "your-team-id"
  GREMLIN_TEAM_SECRET: "your-team-secret"
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: gremlin
spec:
  selector:
    matchLabels:
      app: gremlin
  template:
    metadata:
      labels:
        app: gremlin
    spec:
      hostNetwork: true
      containers:
      - name: gremlin
        image: docker.io/gremlin/gremlin:latest
        envFrom:
        - configMapRef:
            name: gremlin
        securityContext:
          privileged: true
        volumeMounts:
        - name: docker-socket
          mountPath: /var/run/docker.sock
      volumes:
      - name: docker-socket
        hostPath:
          path: /var/run/docker.sock

Docker

# 使用 Docker 运行 Gremlin Agent
docker run -d \
  --privileged \
  --network host \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v /sys/fs/cgroup:/sys/fs/cgroup \
  -e GREMLIN_TEAM_ID="your-team-id" \
  -e GREMLIN_TEAM_SECRET="your-team-secret" \
  gremlin/gremlin

故障类型

基础设施攻击

攻击类型说明参数
CPU占用 CPU 资源length, workers, cpu
Memory占用内存资源length, workers, amount
Disk磁盘读写压力length, workers, block-size
IOIO 压力length, workers
cpu-attack.yaml]
apiVersion: v1
kind: Config
metadata:
  name: cpu-attack
data:
  attacks:
    - name: "cpu-attack"
      type: "cpu"
      length: 60            # 持续 60 秒
      workers: 4            # 4 个 worker
      cpu: 80              # 80% CPU
      target:
        type: "Tag"
        value: "Service:order-service"

网络攻击

network-attack.yaml]
attacks:
  - name: "network-latency"
    type: "network"
    protocol: "tcp"        # 协议
    port: "8080"           # 端口
    latency: 500           # 延迟 500ms
    length: 60

  - name: "network-loss"
    type: "network"
    protocol: "tcp"
    loss: 10               # 10% 丢包率
    length: 60

  - name: "network-blackhole"
    type: "network"
    protocol: "tcp"
    drop: 100              # 100% 丢包(黑洞)
    length: 60

应用层攻击

application-attack.yaml]
attacks:
  - name: "process-kill"
    type: "process"
    process: "nginx"
    count: 1

  - name: "container-kill"
    type: "container"
    image: "order-service:*"
    limit: 50              # 杀死 50% 的容器

安全机制

自动停止

Gremlin 的自动停止(Safety Server)可以在指标异常时自动终止实验:

safety-server.yaml]
# Gremlin Safety Server 配置
safety:
  server:
    enabled: true
    port: 8080

  # 告警规则
  rules:
    - name: "error-rate-high"
      metric: "error_rate"
      threshold: 0.05        # 错误率超过 5%
      action: "stop"

    - name: "latency-high"
      metric: "p99_latency"
      threshold: 2000        # P99 延迟超过 2s
      action: "stop"

    - name: "availability-low"
      metric: "success_rate"
      threshold: 0.95
      action: "stop"

紧急停止

Gremlin 提供一键紧急停止功能:

# CLI 紧急停止
gremlin attack stop --all

# 或通过 API
curl -X POST https://api.gremlin.com/v1/attacks/stop \
  -H "Authorization: Bearer $GREMLIN_API_KEY"

实验限制

experiment-limits.yaml]
# 限制攻击范围
limits:
  # 最大影响范围
  max_impact:
    cpu: 80                 # CPU 不超过 80%
    memory: 50              # 内存不超过 50%

  # 禁止的故障类型
  prohibited_attacks:
    - "disk-destroy"       # 禁止危险操作
    - "network-partition"

  # 只允许特定标签的资源
  allowed_resources:
    - "Service:*"
    - "Tier:canary"

团队协作

权限管理

Gremlin 支持细粒度的权限控制:

角色权限
Admin完全控制,可管理团队和设置
Editor创建和管理实验
Viewer只读,可查看报告
Operator执行预定义的实验

审批流程

approval-workflow.yaml]
workflows:
  - name: "production-chaos"
    require_approval: true
    approvers:
      - "team-lead"
      - "sre-lead"

    # 审批后才能执行
    stages:
      - name: "staging"
        environment: "staging"
        auto_approved: true

      - name: "production"
        environment: "production"
        requires_approval: true

实验报告

报告内容

experiment-report.json]
{
  "experiment": {
    "id": "exp-001",
    "name": "payment-service-failover",
    "date": "2024-01-15T10:00:00Z",
    "duration_seconds": 300,
    "result": "PASS"
  },

  "metrics": {
    "baseline": {
      "success_rate": 0.9999,
      "p99_latency": 50
    },
    "experiment": {
      "success_rate": 0.9997,
      "p99_latency": 120
    }
  },

  "findings": [
    {
      "type": "observation",
      "description": "切换时有短暂延迟上升"
    }
  ]
}

合规导出

# 导出合规报告
gremlin report export \
  --format SOC2 \
  --date-range "2024-01-01/2024-03-31" \
  --output compliance-report.pdf

与监控系统集成

Prometheus 集成

prometheus-integration.yaml]
# Prometheus 配置
prometheus:
  url: "http://prometheus:9090"

# 自定义指标
metrics:
  - name: "custom_error_rate"
    query: |
      sum(rate(http_requests_total{status=~"5.."}[1m]))
      / sum(rate(http_requests_total[1m]))

Datadog 集成

datadog-integration.yaml]
# Datadog 配置
datadog:
  api_key: "your-api-key"
  app_key: "your-app-key"

# 使用 Datadog 指标作为停止条件
safety_rules:
  - name: "dd-error-rate"
    source: "datadog"
    metric: "error_rate"
    threshold: 0.05
    action: "stop"

Gremlin vs 其他工具对比

维度GremlinChaos MeshChaosBlade
平台多平台K8s 为主多平台
安全机制企业级基础基础
协作功能完善
报告功能完善基础
成本付费免费免费

质量判断标准

一篇「Gremlin 混沌工程平台」的文章是否达标,要看它是否回答了:

  1. ✅ Gremlin 的核心特性是什么?
  2. ✅ 如何安装和配置?
  3. ✅ 有哪些故障类型,具体如何使用?
  4. ✅ 安全机制有哪些?
  5. ✅ 如何进行团队协作和报告分析?
  6. ❌ 只有功能列表,没有深入使用——不达标

本章总结

核心要点

  1. Gremlin 是企业级混沌工程平台:完善的安全机制、团队协作、报告分析
  2. 支持 20+ 种故障类型:覆盖基础设施、网络、应用层
  3. 自动停止机制保障安全:与监控告警集成,指标异常自动停止
  4. 适合大型企业:权限管理、审批流程、合规导出
  5. 与主流监控集成:Prometheus、Datadog 等,方便使用现有监控数据