背景
混沌演练平台支持用户自定义故障动作进行混沌演练,用户可以依据场景需要自行配置动作参数以及执行脚本命令。
实践案例
下面将介绍如何配置混沌平台自定义动作,进行 CVM 单核 CPU 压测混沌演练。
步骤一:演练准备
一台可供故障注入的 CVM 实例(示例实例采用 TencentOS Server 2.6(Final)系统)。
故障动作脚本文件。
步骤二:创建自定义动作
2. 进行自定义动作配置。配置命令内容以及命令参数。配置完成后,单击保存。![](https://qcloudimg.tencent-cloud.cn/image/document/715f0e97891abd42aa9c836a8c93964c.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/715f0e97891abd42aa9c836a8c93964c.png)
3. 保存后,可以在自定义动作列表中查看到对应的自定义动作。![](https://qcloudimg.tencent-cloud.cn/image/document/092787e2ae5da1545b98ef5505c66619.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/092787e2ae5da1545b98ef5505c66619.png)
示例命令内容如下,其中 percentage 以及 timeout 参数可以通过配置在执行过程中传入。
#!/bin/bashuser=$(whoami)if [ !$user == 'root' ]thensudo -ifiecho -e "["`date +"%Y-%m-%d %H:%M:%S"`"] \\c"echo "installing stress-ng..."yum install stress-ng -ypercentage={{percentage}}timeout={{timeout}}stress-ng -c 1 -l $percentage --timeout $timeoutif [[ !$? -eq 0 ]]thenecho -e "["`date +"%Y-%m-%d %H:%M:%S"`"] \\c"echo "Failed"exit 1elseecho -e "["`date +"%Y-%m-%d %H:%M:%S"`"] \\c"echo "Completed"exit 0fi
步骤三:演练编排
1. 单击新建演练,填写演练信息,选择演练 CVM 目标实例。![](https://qcloudimg.tencent-cloud.cn/image/document/f3b219c5e73a02804ed4fabef7b41363.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/f3b219c5e73a02804ed4fabef7b41363.png)
2. 单击左下角添加演练动作后,选择 shell脚本,选中创建的自定义动作,并单击下一步。![](https://qcloudimg.tencent-cloud.cn/image/document/a24227ec294488212fe0f79a03238a3d.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/a24227ec294488212fe0f79a03238a3d.png)
3. 该动作的动作参数保持默认,单击确定即可。![](https://qcloudimg.tencent-cloud.cn/image/document/31c0402c60eaad30661da42d2062a7e3.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/31c0402c60eaad30661da42d2062a7e3.png)
4. 动作参数配置完成后,单击下一步。确定所有配置之后,单击提交,完成演练创建。![](https://qcloudimg.tencent-cloud.cn/image/document/a35d424c186b5c9229e174d8e8db27bb.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/a35d424c186b5c9229e174d8e8db27bb.png)
步骤四:执行演练
1. 执行演练,观察执行结果。![](https://qcloudimg.tencent-cloud.cn/image/document/5296ed840f975664907ce67890c54c11.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/5296ed840f975664907ce67890c54c11.png)
2. 使用 top 命令查看 CPU 占用。CPU 占用已到达配置的数值。![](https://qcloudimg.tencent-cloud.cn/image/document/dfc99e6fbdc701db5af29cdbd6c9ec7e.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/dfc99e6fbdc701db5af29cdbd6c9ec7e.png)
3. 动作将会持续60s, 在执行完成之后,查看 CPU 占用。已经恢复正常。![](https://qcloudimg.tencent-cloud.cn/image/document/3cee3089628113c55a2e37c815f56326.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/3cee3089628113c55a2e37c815f56326.png)