跳至主内容
版本:2.6.7

Chaosd 实验的查询与恢复

非官方测试版翻译

本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →

通过 Chaosd 可以按条件查询实验,并根据指定 UID 恢复相应实验。本文档介绍如何查询和恢复 Chaosd 实验,并提供相关示例。

查询 Chaosd 实验

本节介绍如何使用命令行模式和服务模式查找 Chaosd 实验。

命令行模式查询实验

执行以下命令可查看 search 命令支持的配置项:

$ chaosd search --help
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'

配置说明

Configuration itemAbbreviationDescriptionType
allALists all experimentsbool
ascNoneSorts the experiments in ascending order of the creation time. The default value is false.bool
kindkLists experiments of the specified kindstring. The supported kinds are as follows: network, process, stress, disk, host, jvm
limitlThe number of listed experimentsint
offsetoSearches from the specified offsetint
statussLists experiments with the specified statusstring. The supported types are as follows: created, success, error, destroyed, revoked

示例

./chaosd search --kind network --status destroyed --limit 1

执行此命令可查询类型为 network 且状态为 destroyed(表示实验已恢复)的实验。

命令执行后,结果中仅输出一行数据:

                  UID                     KIND     ACTION    STATUS            CREATE TIME                                                                                                                  CONFIGURATION
--------------------------------------- --------- -------- ----------- --------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1f6c1253-522a-43d9-83f8-42607102b3b9 network delay destroyed 2021-11-02T15:14:07+08:00 {"schedule":"","duration":"","action":"delay","kind":"network","uid":"1f6c1253-522a-43d9-83f8-42607102b3b9","latency":"2s","jitter":"0ms","correlation":"0","device":"eth0","ip-address":"220.181.38.251","ip-protocol":"all"}

服务模式查询实验

当前服务模式仅支持查询全部实验,可通过访问 Chaosd 服务的 /api/experiments/ 路径获取数据。

示例

curl -X GET 127.0.0.1:31767/api/experiments/

结果如下:

[{"id":1,"uid":"ddc5ca81-b677-4595-b691-0ce57bedb156","created_at":"2021-10-18T16:01:18.563542491+08:00","updated_at":"2021-10-18T16:07:27.87111393+08:00","status":"success","kind":"stress","action":"mem","recover_command":"{\"schedule\":\"\",\"duration\":\"\",\"action\":\"mem\",\"kind\":\"stress\",\"uid\":\"ddc5ca81-b677-4595-b691-0ce57bedb156\",\"Load\":0,\"Workers\":0,\"Size\":\"100MB\",\"Options\":null,\"StressngPid\":0}","launch_mode":"svr"}]

恢复 Chaosd 实验

创建实验后,如需撤销该实验造成的影响,可使用实验恢复功能。

命令行模式恢复实验

可通过 Chaosd recover UID 命令恢复实验。

以下示例展示如何使用命令行模式恢复实验:

  1. 使用 Chaosd 创建 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

    保存实验 UID 供后续使用。

  2. 当不再需要模拟 CPU 压力场景时,使用 recover 命令恢复对应 UID 的实验:

    chaosd recover 4f33b2d4-aee6-43ca-9c43-0f12867e5c9c

服务模式恢复实验

向 Chaosd 服务的 /api/attack/{uid} 路径发送 DELETE HTTP 请求即可恢复实验。

以下示例展示如何使用服务模式恢复实验:

  1. 向 Chaosd 服务发送 POST HTTP 请求创建 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"}

    保存实验 UID 供后续使用。

  2. 当不再需要模拟 CPU 压力场景时,运行以下命令恢复对应 UID 的实验:

    curl -X DELETE 172.16.112.130:31767/api/attack/c3c519bf-819a-4a7b-97fb-e3d0814481fa