跳到主要内容
版本:2.6.7

模拟磁盘故障

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

在创建磁盘故障实验前,可运行以下命令行查看 Chaosd 支持的磁盘故障类型:

chaosd attack disk -h

输出结果如下所示:

disk attack related command

Usage:
chaosd attack disk [command]

Available Commands:
add-payload add disk payload
fill fill disk

Flags:
-h, --help help for disk

Global Flags:
--log-level string the log level of chaosd, the value can be 'debug', 'info', 'warn' and 'error'

Use "chaosd attack disk [command] --help" for more information about a command.

目前 Chaosd 支持创建磁盘读负载实验、磁盘写负载实验、磁盘填充实验。

要使用服务模式创建实验,你需要以服务模式运行 Chaosd,然后向 Chaosd 服务的路径 /api/attack/disk 发送 POST HTTP 请求:

chaosd server --port 31767
curl -X POST 172.16.112.130:31767/api/attack/disk -H "Content-Type:application/json" -d '{fault-configuration}'

在上述命令中,你需要按照故障类型在 fault-configuration 中进行配置。有关对应的配置参数和示例,请参考下文中各个类型故障的相关参数说明。

备注

在运行实验时,请注意保存实验的 UID 信息。当要结束 UID 对应的实验时,需要向 Chaosd 服务的路径 /api/attack/{uid} 发送 DELETE HTTP 请求。

模拟磁盘读负载场景

模拟磁盘读负载相关参数说明

配置项配置缩写服务模式字段说明类型
actionaction实验的行为string 类型设置为 "read-payload"
pathppath指定所读数据的文件路径。如果没有设置此参数,或者设置参数值为空字符串,Chaosd 则从目录 "/" 所挂载的虚拟磁盘文件读取。根据读取文件的权限不同,会需要你使用一定的权限运行本程序。string 类型默认为 ""
process-numnpayload-process-num指定使用多少个并发运行的 dd 进程执行程序。uint8 类型默认值为 1,范围为 1255
sizessize指定读取多少数据。该值为多个 dd 读数据的总量。string 类型默认为 ""必须要设置。合法形式为一个整数加一个单位。例如:1M、512kB。支持的单位有 c=1、w=2、b=512、kB=1000、K=1024、MB=1000*1000,M=1024*1024、GB=1000*1000*1000、G=1024*1024*1024 BYTE 等。

使用命令行模式模拟磁盘读负载场景

模拟磁盘读负载为一次性操作,因此实验不需要恢复。

运行以下命令可查看模拟磁盘读负载支持的配置:

chaosd attack disk add-payload read -h

输出结果如下所示:

read payload

Usage:
chaosd attack disk add-payload read [flags]

Flags:
-h, --help help for read
-p, --path string 'path' specifies the location to read data.If path not provided, payload will read from disk mount on "/"
-n, --process-num uint8 'process-num' specifies the number of process work on reading , default 1, only 1-255 is valid value (default 1)
-s, --size string 'size' specifies how many units of data will read from the file path.'unit' specifies the unit of data, support c=1, w=2, b=512, kB=1000, K=1024, MB=1000*1000,M=1024*1024, , GB=1000*1000*1000, G=1024*1024*1024 BYTESexample : 1M | 512kB

Global Flags:
--log-level string the log level of chaosd, the value can be 'debug', 'info', 'warn' and 'error'

示例如下:

chaosd attack disk add-payload read -s 1000G -n 7 -p /dev/zero

输出结果如下所示:

andrew@LAPTOP-NUS30NQD:~/chaosd/bin$ ./chaosd attack disk add-payload read -s 1000G -n 7 -p /dev/zero
[2021/05/20 13:54:31.323 +08:00] [INFO] [disk.go:128] ["5242880+0 records in\n5242880+0 records out\n5242880 bytes (5.2 MB, 5.0 MiB) copied, 4.13252 s, 1.3 MB/s\n"]
[2021/05/20 13:54:46.977 +08:00] [INFO] [disk.go:147] ["146285+0 records in\n146285+0 records out\n153390940160 bytes (153 GB, 143 GiB) copied, 15.6513 s, 9.8 GB/s\n"]
[2021/05/20 13:54:47.002 +08:00] [INFO] [disk.go:147] ["146285+0 records in\n146285+0 records out\n153390940160 bytes (153 GB, 143 GiB) copied, 15.6762 s, 9.8 GB/s\n"]
[2021/05/20 13:54:47.004 +08:00] [INFO] [disk.go:147] ["146285+0 records in\n146285+0 records out\n153390940160 bytes (153 GB, 143 GiB) copied, 15.6777 s, 9.8 GB/s\n"]
[2021/05/20 13:54:47.015 +08:00] [INFO] [disk.go:147] ["146285+0 records in\n146285+0 records out\n153390940160 bytes (153 GB, 143 GiB) copied, 15.6899 s, 9.8 GB/s\n"]
[2021/05/20 13:54:47.018 +08:00] [INFO] [disk.go:147] ["146285+0 records in\n146285+0 records out\n153390940160 bytes (153 GB, 143 GiB) copied, 15.6914 s, 9.8 GB/s\n"]
[2021/05/20 13:54:47.051 +08:00] [INFO] [disk.go:147] ["146285+0 records in\n146285+0 records out\n153390940160 bytes (153 GB, 143 GiB) copied, 15.7254 s, 9.8 GB/s\n"]
[2021/05/20 13:54:47.074 +08:00] [INFO] [disk.go:147] ["146285+0 records in\n146285+0 records out\n153390940160 bytes (153 GB, 143 GiB) copied, 15.7487 s, 9.7 GB/s\n"]
Read file /dev/zero successfully, uid: 4bc9b74a-5fe2-4038-b4f2-09ae95b57694

使用服务模式模拟磁盘读负载场景

向 Chaosd 服务的路径 /api/attack/disk 发送 POST HTTP 请求,并配置如下 fault-configuration

curl -X POST 172.16.112.130:31767/api/attack/disk -H "Content-Type:application/json" -d '{"action":"read-payload","path":"/dev/zero", "payload-process-num":7,"size":"1000G"}'

输出结果如下所示:

{"status":200,"message":"attack successfully","uid":"a551206c-960d-4ac5-9056-518e512d4d0d"}

模拟磁盘写负载场景

模拟磁盘写负载相关参数说明

配置项配置缩写服务模式字段说明类型
actionaction实验的行为string 类型设置为 "write-payload"
pathppath指定所写数据的文件路径。如果没有设置此参数,或者设置参数值为空字符串,则会在程序执行目录下创建一个临时文件。根据写入文件的权限不同,会需要你使用一定的权限运行本程序。string 类型默认为 ""
process-numnpayload-process-num指定使用多少个并发运行的 dd 进程执行程序。uint8 类型默认值为 1,范围为 1255
sizessize指定写入多少数据,该值为多个 dd 写数据的总量。string 类型默认为 ""必须要设置。合法形式为一个整数加一个单位。例如:1M、512kB。支持的单位有 c=1、w=2、b=512、kB=1000、K=1024、MB=1000*1000,M=1024*1024、GB=1000*1000*1000、G=1024*1024*1024 BYTE 等。

使用命令行模式模拟磁盘写负载场景

运行以下命令可查看模拟磁盘写负载支持的配置:

chaosd attack disk add-payload write -h

输出结果如下所示:

write payload

Usage:
chaosd attack disk add-payload write [flags]

Flags:
-h, --help help for write
-p, --path string 'path' specifies the location to fill data in.If path not provided, payload will write into a temp file, temp file will be deleted after writing
-n, --process-num uint8 'process-num' specifies the number of process work on writing , default 1, only 1-255 is valid value (default 1)
-s, --size string 'size' specifies how many units of data will write into the file path.'unit' specifies the unit of data, support c=1, w=2, b=512, kB=1000, K=1024, MB=1000*1000,M=1024*1024, , GB=1000*1000*1000, G=1024*1024*1024 BYTESexample : 1M | 512kB

Global Flags:
--log-level string the log level of chaosd, the value can be 'debug', 'info', 'warn' and 'error'

示例如下:

chaosd attack disk add-payload write -s 2G -n 8

输出结果如下所示:

[2021/05/20 14:28:14.452 +08:00] [INFO] [disk.go:128] ["0+0 records in\n0+0 records out\n0 bytes copied, 4.3e-05 s, 0.0 kB/s\n"]
[2021/05/20 14:28:16.793 +08:00] [INFO] [disk.go:147] ["256+0 records in\n256+0 records out\n268435456 bytes (268 MB, 256 MiB) copied, 2.32841 s, 115 MB/s\n"]
[2021/05/20 14:28:16.793 +08:00] [INFO] [disk.go:147] ["256+0 records in\n256+0 records out\n268435456 bytes (268 MB, 256 MiB) copied, 2.3344 s, 115 MB/s\n"]
[2021/05/20 14:28:16.793 +08:00] [INFO] [disk.go:147] ["256+0 records in\n256+0 records out\n268435456 bytes (268 MB, 256 MiB) copied, 2.33312 s, 115 MB/s\n"]
[2021/05/20 14:28:16.793 +08:00] [INFO] [disk.go:147] ["256+0 records in\n256+0 records out\n268435456 bytes (268 MB, 256 MiB) copied, 2.33466 s, 115 MB/s\n"]
[2021/05/20 14:28:16.793 +08:00] [INFO] [disk.go:147] ["256+0 records in\n256+0 records out\n268435456 bytes (268 MB, 256 MiB) copied, 2.33189 s, 115 MB/s\n"]
[2021/05/20 14:28:16.793 +08:00] [INFO] [disk.go:147] ["256+0 records in\n256+0 records out\n268435456 bytes (268 MB, 256 MiB) copied, 2.33752 s, 115 MB/s\n"]
[2021/05/20 14:28:16.793 +08:00] [INFO] [disk.go:147] ["256+0 records in\n256+0 records out\n268435456 bytes (268 MB, 256 MiB) copied, 2.33295 s, 115 MB/s\n"]
[2021/05/20 14:28:16.794 +08:00] [INFO] [disk.go:147] ["256+0 records in\n256+0 records out\n268435456 bytes (268 MB, 256 MiB) copied, 2.3359 s, 115 MB/s\n"]
Write file /home/andrew/chaosd/bin/example255569279 successfully, uid: e66afd86-6f3e-43a0-b161-09447ed84856

使用服务模式模拟磁盘写负载场景

向 Chaosd 服务的路径 /api/attack/disk 发送 POST HTTP 请求,并配置如下 fault-configuration

curl -X POST 172.16.112.130:31767/api/attack/disk -H "Content-Type:application/json" -d '{"action":"write-payload","path":"/tmp/test", "payload-process-num":7,"size":"1000G"}'

输出结果如下所示:

{"status":200,"message":"attack successfully","uid":"a551206c-960d-4ac5-9056-518e512d4d0d"}

模拟磁盘填充场景

模拟磁盘填充相关参数说明

配置项配置缩写服务模式字段说明类型
actionaction实验的行为string 类型设置为 "fill"
destroyddestroy如果此参数为 true,则在填充文件后立即删除填充文件bool 类型默认为 false
fallocateffill-by-fallocate如果此参数为 true,Chaosd 则使用 Linux 调用 fallocate 来快速申请磁盘空间,此时 size 必须大于 0。如果此参数为 false,Chaosd 则使用 Linux 调用 dd 以相对较慢速度填充磁盘。bool 类型默认为 true
pathppath指定所写数据的文件路径。如果没有设置此参数,或者设置参数值为空字符串,则会在程序执行目录下创建一个临时文件。根据写入文件的权限不同,会需要你使用一定的权限运行本程序。string 类型默认为 ""
percentcpercent指定填充多少百分比磁盘。string 类型默认为 "",可以填入 uint 类型的正整数。必须要设置 sizepercent 中的一项,两个配置项的值不能同时为 ""
sizessize指定写入多少数据。string 类型默认为 "",合法形式为一个整数加一个单位。例如:1M、512kB。支持的单位有 c=1、w=2、b=512、kB=1000、K=1024、MB=1000*1000,M=1024*1024、GB=1000*1000*1000、G=1024*1024*1024 BYTE 等。必须要设置 sizepercent 中的一项,两个配置项的值不能同时为 ""

使用命令行模式模拟磁盘填充场景

运行以下命令可查看模拟磁盘填充支持的配置:

chaosd attack disk fill -h

输出结果如下所示:

fill disk

Usage:
chaosd attack disk fill [flags]

Flags:
-d, --destroy destroy file after filled in or allocated
-f, --fallocate fill disk by fallocate instead of dd (default true)
-h, --help help for fill
-p, --path string 'path' specifies the location to fill data in.If path not provided, a temp file will be generated and deleted immediately after data filled in or allocated
-c, --percent string 'percent' how many percent data of disk will fill in the file path
-s, --size string 'size' specifies how many units of data will fill in the file path.'unit' specifies the unit of data, support c=1, w=2, b=512, kB=1000, K=1024, MB=1000*1000,M=1024*1024, , GB=1000*1000*1000, G=1024*1024*1024 BYTESexample : 1M | 512kB

Global Flags:
--log-level string the log level of chaosd, the value can be 'debug', 'info', 'warn' and 'error'

示例如下:

chaosd attack disk fill -c 50 -d

输出结果如下所示:

[2021/05/20 14:30:02.943 +08:00] [INFO] [disk.go:215]
Fill file /home/andrew/chaosd/bin/example623832242 successfully, uid: 097b4214-8d8e-46ad-8768-c3e0d8cbb326

使用服务模式模拟磁盘填充场景

向 Chaosd 服务的路径 /api/attack/disk 发送 POST HTTP 请求,并配置如下 fault-configuration

curl -X POST 172.16.112.130:31767/api/attack/disk -H "Content-Type:application/json" -d '{"action":"fill","path":"/tmp/test", "fill-by-fallocate":true,"percent":"50"}'

输出结果如下所示:

{"status":200,"message":"attack successfully","uid":"a551206c-960d-4ac5-9056-518e512d4d0d"}