作者: 清风明月


Chaosd 组件简介

Chaosd 是 Chaos Mesh 提供的一款混沌工程测试工具(需要单独下载和部署),用于在物理机环境上注入故障,并提供故障恢复功能。

Chaosd 具有以下核心优势:

  • 易用性强:输入简单的 Chaosd 命令即可创建混沌实验,并对实验进行管理。
  • 故障类型丰富:在物理机的不同层次、不同类型上都提供了故障注入的功能,包括进程、网络、压力、磁盘、主机等,且更多的功能在不断扩展中。
  • 支持多种模式:Chaosd 既可作为命令行工具使用,也可以作为服务使用,满足不同场景的使用需求。


支持故障类型

你可以使用 Chaosd 模拟以下故障类型:

  • 进程:对进程进行故障注入,支持进程的 kill、stop 等操作。
  • 网络:对物理机的网络进行故障注入,支持增加网络延迟、丢包、损坏包等操作。
  • 压力:对物理机的 CPU 或内存注入压力。
  • 磁盘:对物理机的磁盘进行故障注入,支持增加读写磁盘负载、填充磁盘等操作。
  • 主机:对物理机本身进行故障注入,支持关机等操作。

对于每种故障类型的详细介绍和使用方式,请参考对应的说明文档。



1、运行环境

linux系统内核必须为2.17及以上版本。

查询如下:

[root@k8s-master ~]# ldd --version
ldd (GNU libc) 2.17
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Roland McGrath and Ulrich Drepper.



2、下载和部署

1、将要下载的Chaosd版本设置为环境变量,例如:v1.1.1
export CHAOSD_VERSION=latest

如果要查看所有已发布的 Chaosd 版本,请参阅 releases(https://github.com/chaos-mesh/chaosd/releases) 
如果要下载最新的非稳定版本,则使用 latest:

2、下载Chaosd
curl -fsSL -o chaosd-$CHAOSD_VERSION-linux-amd64.tar.gz https://mirrors.chaos-mesh.org/chaosd-$CHAOSD_VERSION-linux-amd64.tar.gz

3、解压 Chaosd 文件并转移到 /usr/local 目录下:
tar zxvf chaosd-$CHAOSD_VERSION-linux-amd64.tar.gz && sudo mv chaosd-$CHAOSD_VERSION-linux-amd64 /usr/local/

4、将 Chaosd 目录加到环境变量 PATH 中:
export PATH=/usr/local/chaosd-$CHAOSD_VERSION-linux-amd64:$PATH



3、运行模式

可以通过以下模式使用Chaosd

命令行模式:将 Chaosd 作为命令行工具,直接运行即可注入故障、恢复故障。

服务模式:将 Chaosd 作为服务运行在后台,通过发送 HTTP 请求来注入故障、恢复故障。 nohup chaosd server --port 31767 &



3.1、命令行模式

直接运行命令来创建、管理实验

1、创建实验
[root@k8s-master ~]# chaosd attack process kill --process  sleep -s 15
Attack process sleep successfully, uid: 6eaf29c0-dbf0-4612-bbcf-bcab3be1e706
2、恢复实验
[root@k8s-master ~]# chaosd recover 6eaf29c0-dbf0-4612-bbcf-bcab3be1e706
[2022/04/07 11:25:15.995 +08:00] [WARN] [recover.go:75] ["error.core.non_recoverable_attack: only SIGSTOP process attack and process attack with the recover-cmd are supported to recover"] [uid=6eaf29c0-dbf0-4612-bbcf-bcab3be1e706] [kind=process]
Recover 6eaf29c0-dbf0-4612-bbcf-bcab3be1e706 successfully



3.2、服务模式

让chaosd在后台运行,通过发送http请求的方式来创建、管理实验。

1、启动服务
[root@k8s-master ~]# chaosd server --port 12345
2、创建实验
[root@k8s-master ~]# curl -XPOST 127.0.0.1:12345/api/attack/stress -H "Content-Type:application/json" -d '{"action":"cpu", "load": 100, "wo
{"status":200,"message":"attack successfully","uid":"86f18a92-7636-451e-9ab0-b3fc0c07a0ef"}
3、删除实验
[root@k8s-master ~]# curl -X DELETE "127.0.0.1:12345/api/attack/6eaf29c0-dbf0-4612-bbcf-bcab3be1e706"
{"status":200,"message":"attack recover successfully","uid":"6eaf29c0-dbf0-4612-bbcf-bcab3be1e706"}



4、使用示例



4.1、进程

具体文档请参考https://chaos-mesh.org/zh/docs/simulate-process-chaos-in-physical-nodes/
1、启动测试进程
[root@k8s-master ~]# sleep 1000
2、创建kill实验
[root@k8s-master ~]# chaosd attack process kill --process sleep -s 15
Attack process sleep successfully, uid: c820427d-6035-4644-9672-06cfb28d7ef4
###第一个创建的窗口
[root@k8s-master ~]# sleep 1000
Terminated
3、恢复实验:
[root@k8s-master ~]# chaosd recover c820427d-6035-4644-9672-06cfb28d7ef4
[2022/04/07 11:52:40.087 +08:00] [WARN] [recover.go:75] ["error.core.non_recoverable_attack: only SIGSTOP process attack and process attack with the recover-cmd are supported to recover"] [uid=c820427d-6035-4644-9672-06cfb28d7ef4] [kind=process]
Recover c820427d-6035-4644-9672-06cfb28d7ef4 successfully

4、创建stop实验,通过指定uid
[root@k8s-master ~]# chaosd attack process stop --process sleep --uid 12345
Attack process sleep successfully, uid: 12345
 ##查看进程
[root@k8s-master ~]# ps -aux|grep sleep
root     27457  0.0  0.0 108064   668 pts/2    T    12:02   0:00 sleep 1000
root     27918  0.0  0.0 108064   736 pts/2    T    12:03   0:00 sleep 1000
root     28622  0.0  0.0 112820  2128 pts/1    S+   12:04   0:00 grep --color=auto sleep
####恢复实验
[root@k8s-master ~]# chaosd recover 12345
Recover 12345 successfully

###杀进程
[root@k8s-master ~]# ps -ef|grep sleep
root     30841  4545  0 12:08 pts/2    00:00:00 sleep 1000
root     31841  4545  0 12:10 pts/2    00:00:00 grep --color=auto sleep
[root@k8s-master ~]# chaosd attack process  kill --process  30841
Attack process 30841 successfully, uid: 3c63fcdd-7881-4623-b202-4727b0e1c4f4



4.2、网络

本章主要介绍如何使用Chaosd模拟网络故障的场景,该功能主要使用iptables、ipsets、tc等工具修改网络路由、流量控制来模拟网络故障。

https://chaos-mesh.org/zh/docs/simulate-network-chaos-in-physical-nodes/


通过命令行的方式:
1、搜索网络相关的任务
chaosd search --kind network --status success

2、丢包演示
chaosd attack network loss --correlation 10 --percent 50 -H baidu.com -d eth0
或
chaosd  attack network loss -d eth0 -i 172.16.5.146 --percent 60

#ping baidu.com
[root@k8s-master ~]# ping baidu.com
PING baidu.com (220.181.38.251) 56(84) bytes of data.
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=2 ttl=48 time=5.41 ms
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=3 ttl=48 time=6.04 ms
64 bytes from 220.181.38.251 (220.181.38.251): 
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=37 ttl=48 time=5.31 ms
--- baidu.com ping statistics ---
37 packets transmitted, 14 received, 62% packet loss, time 36576ms
rtt min/avg/max/mdev = 5.271/5.413/6.046/0.207 ms
##查看丢失60%左右的包,把实验recover
chaosd recover b5f0491d-96e0-4474-912e-d5b6b307e181

2、延迟
##延迟2s
 sudo chaosd attack network delay -H baidu.com --device eth0 --latency 2s
##ping baidu.com
[root@k8s-master ~]# ping baidu.com
PING baidu.com (220.181.38.251) 56(84) bytes of data.
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=1 ttl=48 time=2005 ms

3、重复包
sudo chaosd attack network duplicate -H baidu.com --device eth0 --percent 50
##ping baidu.com
[root@k8s-master ~]# ping baidu.com
PING baidu.com (220.181.38.251) 56(84) bytes of data.
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=4 ttl=48 time=5.35 ms
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=4 ttl=48 time=5.35 ms (DUP!)

4、损坏包
sudo chaosd attack network corrupt  -H baidu.com --device eth0 --percent 100

5、分区
sudo chaosd attack network partition -H baidu.com -d eth0 --direction from
#direction:指定分区的方向,对来自/发送到 hostname 指定的主机或者 ip 指定的地址的数据包进行分区    

6、DNS
 chaosd attack network dns -i 1.1.1.1 -d baidu.com
##ping baidu.com
[root@k8s-master ~]# ping baidu.com
PING baidu.com (1.1.1.1) 56(84) bytes of data.
64 bytes from baidu.com (1.1.1.1): icmp_seq=1 ttl=48 time=53.5 ms

#备注
-H 只影响到指定的域名
-i:只影响到指定的IP地址

常见问题:
1. correlation 和 jitter 是什么意思?
correlation 表示与前一次数据包的相关性
jitter 表示延迟时间的变化范围
2. 如何对一批 ip 地址生效?
设置 ip 为网段,例如:192.168.1.1/24
3. 恢复实验后网络仍然有问题?
查看是否有其他网络实验;根据日志中的命令排查。



4.3、主机

Chaosd 支持模拟主机关机故障 。文档:https://chaos-mesh.org/zh/docs/next/simulate-host-console-in-physical-nodes/
#慎用
chaosd attack host shutdown



4.4、压力

该功能通过使用 stress-ng 在主机上生成 CPU 或者内存压力,支持通过命令行模式或服务模式创建压力实验。

1、模拟CPU压力命令
chaosd attack stress cpu --load 90 --workers 4
或
chaosd attack stress cpu -l 100 -w 5
通过top进行查看

2、模拟内存
chaosd attack stress mem --size 1GB
通过free -h查看



4.5、JVM

文档:https://github.com/chaos-mesh/website-zh/blob/main/docs/simulate-jvm-application-chaos-in-physical-nodes.md 
Chaosd 通过 Byteman 模拟 JVM 应用故障,主要支持以下几种故障类型:
抛出自定义异常
触发垃圾回收
增加方法延迟
修改方法返回值
设置 Byteman 配置文件触发故障
增加 JVM 压力
1、启动测试进程
代码:https://github.com/WangXiangUSTC/byteman-example/blob/main/example.helloworld/HelloWorld/Main.java
2、下载和启动
[root@k8s-master chaos-mesh]# wget https://github.com/WangXiangUSTC/byteman-example/blob/main/example.helloworld/HelloWorld/Main.java
1、修改返回值
./chaosd attack jvm submit return --class Main --method getnum --port 9288  --value 123
2、增加延迟
./chaosd attack jvm submit latency  --class Main --method sayhello --latency 5000
3、抛出异常
chaosd attack jvm submit exception -c Main -m sayhello --exception 
4、增加压力
./chaosd attack jvm submit stress --cpu-count 2



4.5、磁盘

本文主要介绍如何使用 Chaosd 模拟磁盘故障场景。使用该功能,你可以在物理机器上模拟磁盘读写负载(通过 dd),或磁盘填充(通过 dd,或 fallocate)。

1、写负载
chaosd attack disk add-payload write --path /tmp/temp --size 100MB
2、读负载
chaosd attack disk add-payload read --path /tmp/temp  --size 100MB
3、填充磁盘
chaosd attack disk fill -p /tmp/123 -s 500MB --fallocate=false



4,6、时间

chaosd attack clock -p $PID -t 11s



5、查找和恢复实验

运行以下命令可查看搜索命令 (

search) 所支持的配置:
[root@k8s-master chaos-mesh]# chaosd search -h
Search chaos attack, you can search attacks through the uid or the state of the attack

Usage:
  chaosd search UID [flags]

Flags:
  -A, --all             list all chaos attacks
      --asc             order by CreateTime, default value is false that means order by CreateTime desc
  -h, --help            help for search
  -k, --kind string     attack kind, supported value: network, process, stress, disk, host, jvm
  -l, --limit uint32    limit the count of attacks
  -o, --offset uint32   starting to search attacks from offset
  -s, --status string   attack status, supported value: created, success, error, destroyed, revoked

Global Flags:
      --log-level string   the log level of chaosd. The value can be 'debug', 'info', 'warn' and 'error'
运行以下命令可查看搜索命令 (search) 所支持的配置:

search

1、查看网络任务

chaosd search --kind network --status success

2、查找实验

chaosd search --kind process --asc --limit 2 -o 1 --status destoryed

在创建完实验后,如果想撤销实验造成的影响,可以使用实验的恢复功能

Chaosd recover UID 的方式恢复实验。



6、出现的问题

如果通过dashboard创建的实验,通过chaosd recover id,dashboard会出现

Failed to recover chaos: : Delete "http://172.16.4.169:31767/api/attack/a5adf7bd-72a0-4465-8b64-f37b8b785932": dial tcp 172.16.4.169:31767: connect: connection refused

解决步骤,如下:

1、在dashboard上把改实验停止。
2、查看通过这个命令看MY_CRD_NAME
[root@k8s-master ~]# kubectl get physicalmachinechaos.chaos-mesh.org -A
NAMESPACE   NAME                  ACTION         DURATION
default     master                stress-cpu     5m
default     physical-stress-cpu   stress-cpu     10m
default     test-pd-01            network-loss   5m
default     test-pd-03            network-loss   3m
default     test-pd-04            network-loss   5m

3、强制删除(kubectl patch crd/MY_CRD_NAME -p '{"metadata":{"finalizers":[]}}' --type=merge。crd是physicalmachinechaos.chaos-mesh.org,MY_CRD_NAME通过1查看的name)
[root@k8s-master ~]# kubectl patch physicalmachinechaos.chaos-mesh.org/physical-stress-cpu -p '{"metadata":{"finalizers":[]}}' --type=merge
physicalmachinechaos.chaos-mesh.org/physical-stress-cpu patched

kubectl patch physicalmachinechaos.chaos-mesh.org/master -p '{"metadata":{"finalizers":[]}}' --type=merge