Package: client-library-for-chaos-mesh
Client Library for Chaos MeshIntroductionChaos 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 StartedTo start using Chaos Mesh, please follow the installation steps in thedocumentation.To create a Chaos Mesh client, you can use the following code:fromchaosmesh.clientimportClient,Experimentfromchaosmesh.k8s.selectorimportSelector# creating the ChaosMesh clientclient=Client(version="v1alpha1")# target pods selector; by labelSector or by pods in specified namespacesselector=Selector(labelSelectors={"app":"filebeat"},pods=None,namespaces=None)Supported API Versionchaos-mesh.org/v1alpha1Experiment TypesChaos Mesh supports various types of experiments, including Pod faults, stress tests, JVM faults, and Host faults.Pod FaultsPod failurePod killContainer killStress TestsCPUMemoryJVM FaultsGCExceptionHost FaultsCPUMemoryHost Disk FaultRead payloadWrite payloadFillNetwork AttackPartitionBandwidthExperiment ExamplesHere are some examples of how you can create experiments in Chaos Mesh:Pod Failure Experiment# name of the experimentexp_name=str(uuid.uuid4())# starting up the pod failure experimentclient.start_experiment(Experiment.POD_FAILURE,namespace="default",name=exp_name,selector=selector)Pod Kill Experimentexp_name=str(uuid.uuid4())# starting up the pod kill experimentclient.start_experiment(Experiment.POD_KILL,namespace="default",name=exp_name,selector=selector)Container Kill Experimentexp_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 Experimentexp_name=str(uuid.uuid4())# starting up the pod kill experimentclient.start_experiment(Experiment.POD_STRESS_CPU,namespace="default",name=exp_name,selector=selector,container_names=['main'])Memory Stress Test Experimentexp_name=str(uuid.uuid4())# starting up the pod kill experimentclient.start_experiment(Experiment.POD_STRESS_MEMORY,namespace="default",name=exp_name,selector=selector,container_names=['main'])GC Experiment# name of the experimentexp_name=str(uuid.uuid4())client.start_experiment(Experiment.GC,namespace="default",name=exp_name,selector=selector,port=8080)Exception Experimentexp_name=str(uuid.uuid4())client.start_experiment(Experiment.RAISE_EXCEPTION,namespace="default",name=exp_name,selector=selectHost CPU stressexp_name=str(uuid.uuid4())# starting up the host cpu stress experimentclient.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 stressexp_name=str(uuid.uuid4())# starting up the host memory stress experimentclient.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 payloadexp_name="disk-fault-read-payload-"+random.randint(0,1000000).__str__()# starting up the read payload experimentclient.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 payloadexp_name="disk-fault-write-payload-"+random.randint(0,1000000).__str__()# starting up the write payload experimentclient.start_experiment(Experiment.HOST_WRITE_PAYLOAD,namespace="default",name=exp_name,selector=selector,address=["address"],size="1024K",path="/",payload_process_num=1)Host Disk Fillexp_name="disk-fault-fill-"+random.randint(0,1000000).__str__()# starting up the disk fill experimentclient.start_experiment(Experiment.HOST_DISK_FILL,namespace="default",name=exp_name,selector=selector,address=["address"],size="1024K",path="/",fill_by_fallocate=True)Network Partitionexp_name="network-partition-"+random.randint(0,1000000).__str__()# starting up the network partition experimentclient.start_experiment(Experiment.NETWORK_PARTITION,namespace="default",name=exp_name,selector=selector,external_targets=["target"],direction="both")Network Bandwidthexp_name="network-bandwidth-"+random.randint(0,1000000).__str__()# starting up the network bandwidth experimentclient.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 experimentIn order to pause an experiment you can use the following command# pausing the experimentclient.pause_experiment(Experiment.POD_STRESS_MEMORY,namespace="default",name=exp_name)Delete the experimentThe experiment can be removed from the k8s cluster using the following commandclient.delete_experiment(Experiment.POD_STRESS_MEMORY,namespace="default",name=exp_name)Schedule experimentsSchedule an experiment using the following commandclient.schedule_experiment(Experiment.POD_STRESS_CPU,namespace="default",name=exp_name,cron_schedule="*/2 * * * *",selector=selector,container_names=['main'])LoggingInitializing the ChaosMesh loggerimportlogging,syslogging.getLogger("chaosmesh")logging.basicConfig(stream=sys.stdout,level=logging.DEBUG)