Chaos Mesh 混沌工程平台介绍、安装及使用指南
Chaos Mesh 混沌工程平台介绍、安装及使用指南
一、Chaos Mesh 简介
Chaos Mesh 是 PingCAP 开源的云原生混沌工程平台,支持在 Kubernetes 环境中模拟各种故障场景,帮助提升系统的容错性和可恢复性。
核心特性
-
丰富的故障类型:
- 资源类:CPU、内存、磁盘、网络故障
- 内核类:杀死进程、网络丢包、文件系统错误
- 应用类:HTTP故障、JVM故障、时钟偏移
- Kubernetes类:Pod故障、节点故障
-
多云平台支持:
- 支持 AWS、GCP、Azure 等多种云平台
- 支持本地 Kubernetes 集群
-
可视化操作:
- 提供 Web UI 界面
- 完善的监控和日志系统
二、安装部署
前提条件
- Kubernetes 集群 (v1.12+)
- Helm 3 (用于安装)
- kubectl 工具
安装步骤
1. 使用 Helm 安装
# 添加 Chaos Mesh 仓库
helm repo add chaos-mesh https://charts.chaos-mesh.org
# 创建命名空间
kubectl create ns chaos-testing
# 安装 Chaos Mesh
helm install chaos-mesh chaos-mesh/chaos-mesh \
--namespace=chaos-testing \
--set dashboard.create=true \
--set dashboard.securityMode=false
2. 验证安装
# 查看 Pod 状态
kubectl get pods -n chaos-testing
# 预期输出应包含如下运行中的 Pod:
# chaos-controller-manager-xxxxx 1/1 Running
# chaos-daemon-xxxxx 1/1 Running
# chaos-dashboard-xxxxx 1/1 Running
3. 访问 Dashboard
# 端口转发
kubectl port-forward -n chaos-testing svc/chaos-dashboard 2333:2333
# 浏览器访问
http://localhost:2333
三、基本使用
1. 通过 Dashboard 创建实验
- 登录 Dashboard (默认无认证)
- 点击 “New Experiment”
- 选择故障类型并配置参数
- 提交实验
2. 通过 YAML 创建实验
示例1:Pod 故障 (杀死指定 Pod)
apiVersion: chaos-mesh.org/v1alpha1
kind: PodChaos
metadata:
name: pod-kill-example
namespace: chaos-testing
spec:
action: pod-kill
mode: one
selector:
namespaces:
- default
labelSelectors:
"app": "nginx"
scheduler:
cron: "@every 10m"
应用配置:
kubectl apply -f pod-kill.yaml
示例2:网络延迟
apiVersion: chaos-mesh.org/v1alpha1
kind: NetworkChaos
metadata:
name: network-delay-example
namespace: chaos-testing
spec:
action: delay
mode: one
selector:
namespaces:
- default
labelSelectors:
"app": "nginx"
delay:
latency: "100ms"
correlation: "100"
jitter: "10ms"
duration: "30s"
scheduler:
cron: "@every 5m"
3. 常用故障类型示例
CPU 压力测试
apiVersion: chaos-mesh.org/v1alpha1
kind: StressChaos
metadata:
name: cpu-stress
namespace: chaos-testing
spec:
mode: one
selector:
namespaces:
- default
labelSelectors:
"app": "nginx"
stressors:
cpu:
workers: 2
load: 80
options: ["--cpu 2", "--timeout 60s"]
duration: "1m"
内存压力测试
apiVersion: chaos-mesh.org/v1alpha1
kind: StressChaos
metadata:
name: memory-stress
namespace: chaos-testing
spec:
mode: one
selector:
namespaces:
- default
labelSelectors:
"app": "nginx"
stressors:
memory:
workers: 1
size: "256MB"
duration: "30s"
四、高级功能
1. 工作流编排
可以编排多个混沌实验按顺序执行:
apiVersion: chaos-mesh.org/v1alpha1
kind: Workflow
metadata:
name: chaos-workflow-example
namespace: chaos-testing
spec:
entry: pod-failure
templates:
- name: pod-failure
type: PodChaos
deadline: 5m
spec:
action: pod-failure
mode: one
selector:
namespaces: ["default"]
labelSelectors:
"app": "nginx"
duration: "1m"
- name: network-delay
type: NetworkChaos
depends: ["pod-failure"]
spec:
action: delay
mode: one
selector:
namespaces: ["default"]
labelSelectors:
"app": "nginx"
delay:
latency: "100ms"
duration: "30s"
2. 与 Prometheus 集成
# 安装时启用 Prometheus 监控
helm upgrade chaos-mesh chaos-mesh/chaos-mesh \
--namespace=chaos-testing \
--set prometheus.create=true
五、安全注意事项
-
生产环境使用:
- 建议开启安全模式
- 限制可操作的命名空间
- 使用 RBAC 严格控制访问权限
-
安全模式配置:
helm upgrade chaos-mesh chaos-mesh/chaos-mesh \ --namespace=chaos-testing \ --set dashboard.securityMode=true \ --set dashboard.createSecret=true
-
清理实验:
# 删除所有实验 kubectl delete chaos -n chaos-testing --all # 卸载 Chaos Mesh helm uninstall chaos-mesh -n chaos-testing
六、最佳实践
- 从小范围开始:先在测试环境验证,再逐步应用到生产环境
- 明确实验目标:每次实验只测试一个特定的故障场景
- 监控指标:实验期间密切监控系统指标
- 团队协作:通知相关团队进行混沌实验
- 文档记录:记录实验过程和结果
通过 Chaos Mesh,您可以系统地验证系统的韧性,提前发现潜在问题,从而提高系统的整体可靠性。