Skip to main content

Client Library for Chaos Mesh

Introduction

Chaos Mesh is an open source cloud-native Chaos Engineering platform that allows you to simulate various faults and orchestrate fault scenarios in your kubernetes cluster. This client is written in Python and provides a single point of entry to create and manage experiments in Chaos Mesh.

Getting Started

To start using Chaos Mesh, please follow the installation steps in the documentation.

To create a Chaos Mesh client, you can use the following code:

from chaosmesh.client import Client, Experiment
from chaosmesh.k8s.selector import Selector

# creating the ChaosMesh client
client = Client(version="v1alpha1")

# target pods selector; by labelSector or by pods in specified namespaces
selector = Selector(labelSelectors={"app": "filebeat"}, pods=None, namespaces=None)

Supported API Version

  • chaos-mesh.org/v1alpha1

Experiment Types

Chaos Mesh supports various types of experiments, including Pod faults, stress tests, JVM faults, and Host faults.

Pod Faults

  • Pod failure
  • Pod kill
  • Container kill

Stress Tests

  • CPU
  • Memory

JVM Faults

  • GC
  • Exception

Host Faults

  • CPU
  • Memory

Host Disk Fault

  • Read payload
  • Write payload
  • Fill

Network Attack

  • Partition
  • Bandwidth

Experiment Examples

Here are some examples of how you can create experiments in Chaos Mesh:

Pod Failure Experiment

# name of the experiment
exp_name = str(uuid.uuid4())

# starting up the pod failure experiment
client.start_experiment(Experiment.POD_FAILURE, namespace="default", name=exp_name, selector=selector)

Pod Kill Experiment

exp_name = str(uuid.uuid4())

# starting up the pod kill experiment
client.start_experiment(Experiment.POD_KILL, namespace="default", name=exp_name, selector=selector)

Container Kill Experiment

exp_name = str(uuid.uuid4())

# starting up the pod kill experiment
client.start_experiment(Experiment.CONTAINER_KILL, namespace="default", name=exp_name, selector=selector, container_names=['main'])

CPU Stress Test Experiment

exp_name = str(uuid.uuid4())

# starting up the pod kill experiment
client.start_experiment(Experiment.POD_STRESS_CPU, namespace="default", name=exp_name, selector=selector, container_names=['main'])

Memory Stress Test Experiment

exp_name = str(uuid.uuid4())

# starting up the pod kill experiment
client.start_experiment(Experiment.POD_STRESS_MEMORY, namespace="default", name=exp_name, selector=selector, container_names=['main'])

GC Experiment

# name of the experiment
exp_name = str(uuid.uuid4())

client.start_experiment(Experiment.GC, namespace="default", name=exp_name, selector=selector, port=8080)

Exception Experiment

exp_name = str(uuid.uuid4())

client.start_experiment(Experiment.RAISE_EXCEPTION, namespace="default",
                        name=exp_name, selector=select

Host CPU stress

exp_name = str(uuid.uuid4())

# starting up the host cpu stress experiment
client.start_experiment(Experiment.HOST_STRESS_CPU, namespace="default", name=exp_name,
                        address=["10.225.66.224", "10.225.67.213", "10.225.66.231", "10.225.66.138", "10.225.66.192", "10.225.67.52", "10.225.67.103"],
                        load=1000)

Host Memory stress

exp_name = str(uuid.uuid4())

# starting up the host memory stress experiment
client.start_experiment(Experiment.HOST_STRESS_MEMORY, namespace="default", name=exp_name,
                        address=["10.225.66.224", "10.225.67.213", "10.225.66.231", "10.225.66.138", "10.225.66.192", "10.225.67.52", "10.225.67.103"],
                        size="30GB")

Host Disk Fault Read payload

exp_name = "disk-fault-read-payload-" + random.randint(0, 1000000).__str__()

# starting up the read payload experiment
client.start_experiment(Experiment.HOST_READ_PAYLOAD, namespace="default", name=exp_name, selector=selector, address=["address"], size="1024K", path="/", payload_process_num=1)

Host Disk Fault Write payload

exp_name = "disk-fault-write-payload-" + random.randint(0, 1000000).__str__()

# starting up the write payload experiment
client.start_experiment(Experiment.HOST_WRITE_PAYLOAD, namespace="default", name=exp_name, selector=selector, address=["address"], size="1024K", path="/",
                        payload_process_num=1)

Host Disk Fill

exp_name = "disk-fault-fill-" + random.randint(0, 1000000).__str__()

# starting up the disk fill experiment
client.start_experiment(Experiment.HOST_DISK_FILL, namespace="default", name=exp_name, selector=selector, address=["address"], size="1024K", path="/", fill_by_fallocate=True)

Network Partition

exp_name = "network-partition-" + random.randint(0, 1000000).__str__()

# starting up the network partition experiment
client.start_experiment(Experiment.NETWORK_PARTITION, namespace="default", name=exp_name, selector=selector, external_targets=["target"], direction="both")

Network Bandwidth

exp_name = "network-bandwidth-" + random.randint(0, 1000000).__str__()

# starting up the network bandwidth experiment
client.start_experiment(Experiment.NETWORK_BANDWIDTH, namespace="default", name=exp_name, selector=selector, rate="1bps", buffer=1, limit=1, direction="to",
                        external_targets=["target"])

Pause an experiment

In order to pause an experiment you can use the following command

# pausing the experiment
client.pause_experiment(Experiment.POD_STRESS_MEMORY, namespace="default", name=exp_name)

Delete the experiment

The experiment can be removed from the k8s cluster using the following command

client.delete_experiment(Experiment.POD_STRESS_MEMORY, namespace="default", name=exp_name)

Schedule experiments

Schedule an experiment using the following command

client.schedule_experiment(Experiment.POD_STRESS_CPU, namespace="default", name=exp_name, cron_schedule="*/2 * * * *", selector=selector, container_names=['main'])

Logging

Initializing the ChaosMesh logger

import logging, sys

logging.getLogger("chaosmesh")
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)

Release files for chaos-mesh 1.2.13

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for chaos-mesh 1.2.13
File Size Uploaded
chaos_mesh-1.2.13.tar.gz 19.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for chaos-mesh 1.2.13
File Interpreter ABI Platform
chaos_mesh-1.2.13-py3-none-any.whl Python 3 none any Details

Total release size: 59.7 kB

Release files / chaos_mesh-1.2.13.tar.gz

Download URL chaos_mesh-1.2.13.tar.gz
Size 19.2 kB
Tags Source
SHA-256 checksum
How to use checksums
09a19338b57ed98418193c729bac9c783ced5f646383b15faccfc12fed241958
BLAKE2b-256 checksum
How to use checksums
5b9c2d192bbcd792b7fae762e502ceac19a1f82214aa4bfe356b152e9731f12b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.5

Release files / chaos_mesh-1.2.13-py3-none-any.whl

Download URL chaos_mesh-1.2.13-py3-none-any.whl
Size 40.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0b768d8d8c425e1f4770a470104ae17055bb5cbd4f2901b3637fd2822be967fe
BLAKE2b-256 checksum
How to use checksums
f10c82bdd9afcefbd3d6c980938ce62e6fdf98d8b0e566d864ee3e215717c1c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.5

Release history Release notifications | RSS feed

This release

1.2.13 This release

2 release files

1.2.11

2 release files

1.2.10

2 release files

1.2.9

2 release files

1.2.8

2 release files

1.2.7

2 release files

1.2.6

2 release files

1.2.5

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page