模拟 AWS 故障
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
本文档介绍如何使用 Chaos Mesh 模拟 AWS 故障。
AWSChaos 介绍
AWSChaos 可帮助您在指定的 AWS 实例上模拟故障场景。当前支持以下故障类型:
-
EC2 停止:停止指定的 EC2 实例。
-
EC2 重启:重启指定的 EC2 实例。
-
卸载存储卷:从指定 EC2 实例卸载存储卷。
Secret 文件
为简化 AWS 集群连接,可预先创建 Kubernetes Secret 文件存储认证信息。
Secret 文件示例如下:
apiVersion: v1
kind: Secret
metadata:
name: cloud-key-secret
namespace: chaos-mesh
type: Opaque
stringData:
aws_access_key_id: your-aws-access-key-id
aws_secret_access_key: your-aws-secret-access-key
aws_session_token: your-aws-session-token
-
name:表示 Kubernetes Secret 对象名称。
-
namespace:表示 Kubernetes Secret 对象所属的命名空间。
-
aws_access_key_id 存储访问 AWS 集群的密钥 ID。
-
aws_secret_access_key 存储访问 AWS 集群的密钥。
-
aws_session_token 存储 AWS 会话令牌(使用临时 AWS 凭证时必需)。
使用 Chaos Dashboard 创建实验
使用 Chaos Dashboard 创建实验前,请确保满足以下条件:
-
已安装 Chaos Dashboard。
-
可通过
kubectl port-forward访问 Dashboard:kubectl port-forward -n chaos-mesh svc/chaos-dashboard 2333:2333随后在浏览器中访问
http://localhost:2333。
-
打开 Chaos Dashboard,点击页面上的 新建实验 按钮:

img -
在 选择目标 区域,选择 AWS 故障 并指定具体行为(如 停止 EC2)。
-
填写实验信息,指定实验范围和计划持续时间。
-
提交实验信息。
使用 YAML 文件创建实验
ec2-stop 配置示例
-
将实验配置写入
awschaos-ec2-stop.yaml文件,示例如下:apiVersion: chaos-mesh.org/v1alpha1
kind: AWSChaos
metadata:
name: ec2-stop-example
namespace: chaos-mesh
spec:
action: ec2-stop
secretName: 'cloud-key-secret'
awsRegion: 'us-east-2'
ec2Instance: 'your-ec2-instance-id'
duration: '5m'此配置将使 Chaos Mesh 向指定 EC2 实例注入
ec2-stop故障,导致该实例在 5 分钟内不可用。有关停止 EC2 实例的更多信息,请参阅 AWS 文档 - 停止和启动实例。
-
配置文件准备完成后,使用
kubectl创建实验:kubectl apply -f awschaos-ec2-stop.yaml
ec2-start 配置示例
-
将实验配置写入
awchaos-ec2-restot.yaml文件:apiVersion: chaos-mesh.org/v1alpha1
kind: AWSChaos
metadata:
name: ec2-restart-example
namespace: chaos-mesh
spec:
action: ec2-restart
secretName: 'cloud-key-secret'
awsRegion: 'us-east-2'
ec2Instance: 'your-ec2-instance-id'根据此配置示例,Chaos Mesh 将向指定 EC2 实例注入
ec2-restart故障,使该 EC2 实例发生重启。有关重启 EC2 实例的更多信息,请参阅 AWS 文档 - 重启实例。
-
配置文件准备就绪后,使用
kubectl创建实验:kubectl apply -f awschaos-ec2-restart.yaml
detach-volume 配置示例
-
将实验配置写入
awschaos-detach-volume.yaml文件:apiVersion: chaos-mesh.org/v1alpha1
kind: AWSChaos
metadata:
name: ec2-detach-volume-example
namespace: chaos-mesh
spec:
action: ec2-stop
secretName: 'cloud-key-secret'
awsRegion: 'us-east-2'
ec2Instance: 'your-ec2-instance-id'
volumeID: 'your-volume-id'
deviceName: '/dev/sdf'
duration: '5m'根据此配置示例,Chaos Mesh 将在 5 分钟内向指定 EC2 实例注入
detail-volume故障,使该实例与指定存储卷分离。有关卸载 Amazon EBS 卷的更多信息,请参阅 AWS 文档 - 从 Linux 实例卸载 Amazon EBS 卷。
-
配置文件准备就绪后,使用
kubectl创建实验:kubectl apply -f awschaos-detach-volume.yaml
字段说明
下表展示了 YAML 配置文件中的字段。
| Parameter | Type | Description | Default value | Required | Example |
|---|---|---|---|---|---|
| action | string | Indicates the specific type of faults. Only ec2-stop, ec2-restore, and detain-volume are supported. | ec2-stop | Yes | ec2-stop |
| mode | string | Specifies the mode of the experiment. The mode options include one (selecting a random Pod), all (selecting all eligible Pods), fixed (selecting a specified number of eligible Pods), fixed-percent (selecting a specified percentage of Pods from the eligible Pods), and random-max-percent (selecting the maximum percentage of Pods from the eligible Pods). | None | Yes | one |
| value | string | Provides parameters for the mode configuration, depending on mode.For example, when mode is set to fixed-percent, value specifies the percentage of Pods. | None | No | 1 |
| secretName | string | Specifies the name of the Kubernetes Secret that stores the AWS authentication information. | None | No | cloud-key-secret |
| awsRegion | string | Specifies the AWS region. | None | Yes | us-east-2 |
| ec2Instance | string | Specifies the ID of the EC2 instance. | None | Yes | your-ec2-instance-id |
| volumeID | string | This is a required field when the action is detach-volume. This field specifies the EBS volume ID. | None | No | your-volume-id |
| deviceName | string | This is a required field when the action is detach-volume. This field specifies the machine name. | None | No | /dev/sdf |
| duration | string | Specifies the duration of the experiment. | None | Yes | 30s |