Simulate Block Device Latency

BlockChaos Introduction

Chaos Mesh provides the BlockChaos experiment type. You can use this experiment type to simulate a block device latency scenario. This document describes how to install the dependencies of a BlockChaos experiment, and create a BlockChaos.

Simulate Block Device Latency - 图1note

BlockChaos is in an early stage. The installation and configuration experience of it will continue to improve. If you find any issues, please open an issue in chaos-mesh/chaos-mesh to report.

Install kernel module

BlockChaos depends on the chaos-driver kernel module. It can only be injected on a machine with this module installed. Currently, you have to compile and install the module manually.

  1. Download the source code of this module using the following command:

    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. Uncompress the chaos-driver-v0.2.1.tar.gz file:

    1. tar xvf chaos-driver-v0.2.1.tar.gz
  3. Prepare the headers of your current kernel. If you are using CentOS/Fedora, you can install the kernel headers with yum:

    1. yum install kernel-devel-$(uname -r)

    If you are using Ubuntu/Debian, you can install the kernel headers with apt:

    1. apt install linux-headers-$(uname -r)
  4. Compile the module:

    1. cd chaos-driver-v0.2.1
    2. make driver/chaos_driver.ko
  5. Install the kernel module:

    1. insmod ./driver/chaos_driver.ko

The chaos_driver module has to be installed every time after rebooting. To load the module automatically, you can copy the module to a subdirectory in /lib/modules/$(uname -r)/kernel/drivers, run depmod -a, and then add chaos_driver to the /etc/modules.

If you have upgraded the kernel, the module should be recompiled.

Simulate Block Device Latency - 图2note

It is recommended to use DKMS or akmod for automatic kernel module compiling or loading. If you want to help us improve the installation experience, creating a DKMS or akmod package and submitting it to different distribution repositories is very welcome.

Create experiments using the YAML file

  1. Write the experiment configuration to the YAML configuration file. The following uses the block-latency.yaml file as an example.

    1. apiVersion: chaos-mesh.org/v1alpha1
    2. kind: BlockChaos
    3. metadata:
    4. name: hostpath-example-delay
    5. spec:
    6. selector:
    7. labelSelectors:
    8. app: hostpath-example
    9. mode: all
    10. volumeName: hostpath-example
    11. action: delay
    12. delay:
    13. latency: 1s

    Simulate Block Device Latency - 图3note

    Only hostpath or localvolume is supported.

  2. Use kubectl to create an experiment:

    1. kubectl apply -f block-latency.yaml

You can find the following magic happened:

  1. The elevator of the volume is changed to ioem or ioem-mq. You can check it through cat /sys/block/<device>/queue/scheduler.
  2. The ioem or ioem-mq scheduler will receive the latency request and delay the request for the specified time.

The fields in the YAML configuration file are described in the following table:

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
delay.latencystringSpecifies the latency of the block device.NoneYes500ms