模拟压力场景
非官方测试版翻译
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
本文档介绍如何使用 Chaosd 模拟压力场景。该功能通过 stress-ng 在主机上生成 CPU 或内存压力。您可以通过命令行模式或服务模式创建压力实验。
通过命令行模式创建压力实验
本节介绍如何通过命令行模式创建压力实验。
创建压力实验前,可运行以下命令查看 Chaosd 支持的压力实验类型:
chaosd attack stress --help
结果如下:
Stress attack related commands
Usage:
chaosd attack stress [command]
Available Commands:
cpu continuously stress CPU out
mem continuously stress virtual memory out
Flags:
-h, --help help for stress
Global Flags:
--log-level string the log level of chaosd, the value can be 'debug', 'info', 'warn' and 'error'
Use "chaosd attack stress [command] --help" for more information about a command.
当前 Chaosd 支持创建 CPU 压力实验和内存压力实验。
通过命令行模式模拟 CPU 压力
模拟 CPU 压力的命令
查看 CPU 压力模拟支持的配置项,请运行:
chaosd attack stress cpu --help
结果如下:
continuously stress CPU out
Usage:
chaosd attack stress cpu [options] [flags]
Flags:
-h, --help help for cpu
-l, --load int Load specifies P percent loading per CPU worker. 0 is effectively a sleep (no load) and 100 is full loading. (default 10)
-o, --options strings extend stress-ng options.
-w, --workers int Workers specifies N workers to apply the stressor. (default 1)
Global Flags:
--log-level string the log level of chaosd, the value can be 'debug', 'info', 'warn' and 'error'
模拟 CPU 压力的配置说明
| Configuration item | Abbreviation | Description | Type | Value |
|---|---|---|---|---|
load | l | Specifies the percentage of CPU load per CPU worker. 0 means no CPU utilization, and 100 means full CPU utilization. | int | Range: 0 to 100; Default value: 10. |
workers | w | Specifies the number of workers used to create CPU stress. | int | Default value: 1. |
options | o | The extended parameter of stress-ng, usually not configured. | string | Default value: "". |
模拟 CPU 压力的示例
chaosd attack stress cpu --workers 2 --load 10
结果如下:
[2021/05/12 03:38:33.698 +00:00] [INFO] [stress.go:66] ["stressors normalize"] [arguments=" --cpu 2 --cpu-load 10"]
[2021/05/12 03:38:33.702 +00:00] [INFO] [stress.go:82] ["Start stress-ng process successfully"] [command="/usr/bin/stress-ng --cpu 2 --cpu-load 10"] [Pid=27483]
Attack stress cpu successfully, uid: 4f33b2d4-aee6-43ca-9c43-0f12867e5c9c
通过命令行模式模拟内存压力
模拟内存压力的命令
查看内存压力模拟支持的配置项,请运行:
chaosd attack stress mem --help
结果如下:
continuously stress virtual memory out
Usage:
chaosd attack stress mem [options] [flags]
Flags:
-h, --help help for mem
-o, --options strings extend stress-ng options.
-s, --size string Size specifies N bytes consumed per vm worker, default is the total available memory. One can specify the size as % of total available memory or in units of B, KB/KiB, MB/MiB, GB/GiB, TB/TiB..
Global Flags:
--log-level string the log level of chaosd, the value can be 'debug', 'info', 'warn' and 'error'
模拟内存压力的配置说明
| Configuration item | Abbreviation | Description | Type | Value |
|---|---|---|---|---|
size | s | Specifies the size of memory per VM worker. | string | The memory size in B, KB/KiB, MB/MiB, GB/GiB, TB/TiB. If the size is not set, all available memory is used by default. |
options | o | The extended parameter of stress-ng, usually not configured. | string | Default value: "". |
模拟内存压力的示例
chaosd attack stress mem --workers 2 --size 100M
结果如下:
[2021/05/12 03:37:19.643 +00:00] [INFO] [stress.go:66] ["stressors normalize"] [arguments=" --vm 2 --vm-keep --vm-bytes 100000000"]
[2021/05/12 03:37:19.654 +00:00] [INFO] [stress.go:82] ["Start stress-ng process successfully"] [command="/usr/bin/stress-ng --vm 2 --vm-keep --vm-bytes 100000000"] [Pid=26799]
Attack stress mem successfully, uid: c2bff2f5-3aac-4ace-b7a6-322946ae6f13
运行实验时需保存实验的 uid 信息。当不需要压力模拟时,可通过 recover 终止对应 uid 的实验::
chaosd recover c2bff2f5-3aac-4ace-b7a6-322946ae6f13
结果如下:
Recover c2bff2f5-3aac-4ace-b7a6-322946ae6f13 successfully
通过服务模式创建压力实验
要通过服务模式创建实验,请按以下步骤操作:
-
在服务模式下运行 Chaosd:
chaosd server --port 31767 -
向 Chaosd 服务的
/api/attack/{uid}路径发送POSTHTTP 请求:curl -X POST 172.16.112.130:31767/api/attack/jvm -H "Content-Type:application/json" -d '{fault-configuration}'上述命令中的
fault-configuration部分需根据故障类型配置,具体参数请参考后续章节中各故障类型的参数说明及示例。
备注
运行实验时,请务必保存实验的 UID 信息。当需要终止对应 UID 的实验时,需向 Chaosd 服务的 /api/attack/{uid} 路径发送 DELETE HTTP 请求。
通过服务模式模拟 CPU 压力
模拟 CPU 压力的参数
| Parameter | Description | Type | Value |
|---|---|---|---|
action | Actions of the experiment | Set to "cpu" | |
load | Specifies the percentage of CPU load per CPU worker. 0 means no CPU utilization, and 100 means full CPU utilization. | int | Range: 0 to 100; Default value: 10 |
workers | Specifies the number of workers used to create CPU stress | int | Default value: 1 |
options | The extended parameter of stress-ng, usually not configured. | string | Default value: "" |
通过服务模式模拟 CPU 压力的示例
curl -X POST 172.16.112.130:31767/api/attack/stress -H "Content-Type:application/json" -d '{"load":10, "action":"cpu","workers":1}'
结果如下:
{"status":200,"message":"attack successfully","uid":"c3c519bf-819a-4a7b-97fb-e3d0814481fa"}
通过服务模式模拟内存压力
模拟内存压力的参数
| Parameter | Description | Type | Value |
|---|---|---|---|
action | Actions of the experiment | Set to "mem" | |
size | Specifies the size of memory per VM worker | string | the memory size in B, KB/KiB, MB/MiB, GB/GiB, TB/TiB. If the size is not set, all available memory is used by default. |
options | The extended parameter of stress-ng, usually not configured. | string | Default value: "" |
通过服务模式模拟内存压力的示例
curl -X POST 172.16.112.130:31767/api/attack/stress -H "Content-Type:application/json" -d '{"size":"100M", "action":"mem"}'
结果如下:
{"status":200,"message":"attack successfully","uid":"a551206c-960d-4ac5-9056-518e512d4d0d"}