跳至主内容
版本:下一版本

模拟块设备故障

非官方测试版翻译

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

BlockChaos 介绍

Chaos Mesh 提供 BlockChaos 实验类型,可用于模拟块设备延迟或冻结场景。本文档将介绍如何安装 BlockChaos 实验的依赖项并创建 BlockChaos 实验。

备注

BlockChaos 目前处于早期阶段,其安装和配置体验将持续优化。如发现任何问题,请在 chaos-mesh/chaos-mesh 提交 issue 报告。

备注

BlockChaos 的 freeze 操作会影响使用该块设备的所有进程,而不仅限于目标容器。

安装内核模块

BlockChaos 的 delay 操作依赖于 chaos-driver 内核模块,该实验只能在已安装此模块的机器上注入。目前您需要手动编译安装此模块。

  1. 使用以下命令下载模块源码:

    curl -fsSL -o chaos-driver-v0.2.1.tar.gz https://github.com/chaos-mesh/chaos-driver/archive/refs/tags/v0.2.1.tar.gz
  2. 解压 chaos-driver-v0.2.1.tar.gz 文件:

    tar xvf chaos-driver-v0.2.1.tar.gz
  3. 准备当前内核的头文件:

    • CentOS/Fedora 系统使用 yum 安装:
      yum install kernel-devel-$(uname -r)
    • Ubuntu/Debian 系统使用 apt 安装:
      apt install linux-headers-$(uname -r)
  4. 编译模块:

    cd chaos-driver-v0.2.1
    make driver/chaos_driver.ko
  5. 安装内核模块:

    insmod ./driver/chaos_driver.ko

chaos_driver 模块需在每次重启后重新安装。要实现自动加载,可将模块复制到 /lib/modules/$(uname -r)/kernel/drivers 的子目录,运行 depmod -a,然后在 /etc/modules 中添加 chaos_driver

升级内核后需重新编译该模块。

备注

建议使用 DKMS 或 akmod 实现内核模块的自动编译加载。欢迎通过创建 DKMS/akmod 包并提交至各发行版仓库来帮助我们改进安装体验。

使用 YAML 文件创建实验

  1. 将实验配置写入 YAML 文件,以下以 block-latency.yaml 为例:

    apiVersion: chaos-mesh.org/v1alpha1
    kind: BlockChaos
    metadata:
    name: hostpath-example-delay
    spec:
    selector:
    labelSelectors:
    app: hostpath-example
    mode: all
    volumeName: hostpath-example
    action: delay
    delay:
    latency: 1s
    备注

    仅支持 hostpath 或 localvolume 类型卷。

  2. 使用 kubectl 创建实验:

    kubectl apply -f block-latency.yaml

您将观察到以下变化:

  1. 卷的 I/O 调度器将切换为 ioemioem-mq,可通过 cat /sys/block/<device>/queue/scheduler 验证。

  2. ioemioem-mq 调度器将接收延迟请求,并将请求延迟指定的时间。

YAML 配置文件中的字段如下表所述:

ParameterTypeNoteDefault valueRequiredExample
modestringSpecifies 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).NoneYesone
valuestringProvides parameters for the mode configuration, depending on mode. For example, when mode is set to fixed-percent, value specifies the percentage of Pods.NoneNo1
selectorstructSpecifies the target Pod. For details, refer to Define the experiment scope.NoneYes
volumeNamestringSpecifies the volume to inject in the target pods. There should be a corresponding entry in the pods' .spec.volumes.NoneYeshostpath-example
actionstringIndicates the specific type of faults. The available fault types include delay and freeze. delay will simulate the latency of block devices, and freeze will simulate that the block device cannot handle any requestsNoneYesdelay
delay.latencystringSpecifies the latency of the block device.NoneYes (if action is delay)500ms