Create a chaos experiment that uses a Chaos Mesh fault with the Azure CLI

You can use a chaos experiment to verify that your application is resilient to failures by causing those failures in a controlled environment. In this guide, you will cause periodic Azure Kubernetes Service pod failures on a namespace using a chaos experiment and Azure Chaos Studio. Running this experiment can help you defend against service unavailability when there are sporadic failures.

Azure Chaos Studio uses Chaos Mesh, a free, open-source chaos engineering platform for Kubernetes to inject faults into an AKS cluster. Chaos Mesh faults are service-direct faults that require Chaos Mesh to be installed on the AKS cluster. These same steps can be used to set up and run an experiment for any AKS Chaos Mesh fault.



AKS Chaos Mesh faults are only supported on Linux node pools.

Launch Azure Cloud Shell

The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. It has common Azure tools preinstalled and configured to use with your account.

To open the Cloud Shell, just select Try it from the upper right corner of a code block. You can also open Cloud Shell in a separate browser tab by going to Select Copy to copy the blocks of code, paste it into the Cloud Shell, and select Enter to run it.

If you prefer to install and use the CLI locally, this tutorial requires Azure CLI version 2.0.30 or later. Run az --version to find the version. If you need to install or upgrade, see Install Azure CLI.


These instructions use a Bash terminal in Azure Cloud Shell. Some commands may not work as described if running the CLI locally or in a PowerShell terminal.

Set up Chaos Mesh on your AKS cluster

Before you can run Chaos Mesh faults in Chaos Studio, you need to install Chaos Mesh on your AKS cluster.

  1. Run the following commands in an Azure Cloud Shell window where you have the active subscription set to be the subscription where your AKS cluster is deployed. Replace $RESOURCE_GROUP and $CLUSTER_NAME with the resource group and name of your cluster resource.

    az aks get-credentials -g $RESOURCE_GROUP -n $CLUSTER_NAME
    helm repo add chaos-mesh
    helm repo update
    kubectl create ns chaos-testing
    helm install chaos-mesh chaos-mesh/chaos-mesh --namespace=chaos-testing --set chaosDaemon.runtime=containerd --set chaosDaemon.socketPath=/run/containerd/containerd.sock
  2. Verify that the Chaos Mesh pods are installed by running the following command:

    kubectl get po -n chaos-testing

You should see output similar to the following (a chaos-controller-manager and one or more chaos-daemons):

NAME                                        READY   STATUS    RESTARTS   AGE
chaos-controller-manager-69fd5c46c8-xlqpc   1/1     Running   0          2d5h
chaos-daemon-jb8xh                          1/1     Running   0          2d5h
chaos-dashboard-98c4c5f97-tx5ds             1/1     Running   0          2d5h

You can also use the installation instructions on the Chaos Mesh website.

Enable Chaos Studio on your AKS cluster

Chaos Studio cannot inject faults against a resource unless that resource has been onboarded to Chaos Studio first. You onboard a resource to Chaos Studio by creating a target and capabilities on the resource. AKS clusters only have one target type (service-direct), but other resources may have up to two target types - one for service-direct faults and one for agent-based faults. Each type of Chaos Mesh fault is represented as a capability (PodChaos, NetworkChaos, IOChaos, etc.).

  1. Create a target by replacing $RESOURCE_ID with the resource ID of the AKS cluster you are onboarding:

    az rest --method put --url "$RESOURCE_ID/providers/Microsoft.Chaos/targets/Microsoft-AzureKubernetesServiceChaosMesh?api-version=2021-09-15-preview" --body "{\"properties\":{}}"
  2. Create the capabilities on the target by replacing $RESOURCE_ID with the resource ID of the AKS cluster you are onboarding and $CAPABILITY with the name of the fault capability you are enabling.

    az rest --method put --url "$RESOURCE_ID/providers/Microsoft.Chaos/targets/Microsoft-AzureKubernetesServiceChaosMesh/capabilities/$CAPABILITY?api-version=2021-09-15-preview"  --body "{\"properties\":{}}"

    For example, if enabling the PodChaos capability:

    az rest --method put --url ""  --body "{\"properties\":{}}"

    This must be done for each capability you want to enable on the cluster.

You have now successfully onboarded your AKS cluster to Chaos Studio.

Create an experiment

With your AKS cluster now onboarded, you can create your experiment. A chaos experiment defines the actions you want to take against target resources, organized into steps, which run sequentially, and branches, which run in parallel.

  1. Create a Chaos Mesh jsonSpec:

    1. Visit the Chaos Mesh documentation for a fault type, for example, the PodChaos type.

    2. Formulate the YAML configuration for that fault type using the Chaos Mesh documentation.

      kind: PodChaos
        name: pod-failure-example
        namespace: chaos-testing
        action: pod-failure
        mode: all
        duration: '600s'
            - default
    3. Remove any YAML outside of the spec (including the spec property name), and remove the indentation of the spec details.

      action: pod-failure
      mode: all
      duration: '600s'
          - default
    4. Use a YAML-to-JSON converter like this one to convert the Chaos Mesh YAML to JSON and minimize it.

    5. Use a JSON string escape tool like this one to escape the JSON spec.

  2. Create your experiment JSON starting with the JSON sample below. Modify the JSON to correspond to the experiment you want to run using the Create Experiment API, the fault library, and the jsonSpec created in the previous step.

      "location": "centralus",
      "identity": {
        "type": "SystemAssigned"
      "properties": {
        "steps": [
            "name": "AKS pod kill",
            "branches": [
                "name": "AKS pod kill",
                "actions": [
                    "type": "continuous",
                    "selectorId": "Selector1",
                    "duration": "PT10M",
                    "parameters": [
                          "key": "jsonSpec",
                          "value": "{\"action\":\"pod-failure\",\"mode\":\"all\",\"duration\":\"600s\",\"selector\":{\"namespaces\":[\"default\"]}}"
                    "name": "urn:csci:microsoft:azureKubernetesServiceChaosMesh:podChaos/2.1"
        "selectors": [
            "id": "Selector1",
            "type": "List",
            "targets": [
                "type": "ChaosTarget",
                "id": "/subscriptions/b65f2fec-d6b2-4edd-817e-9339d8c01dc4/resourceGroups/myRG/providers/Microsoft.ContainerService/managedClusters/myCluster/providers/Microsoft.Chaos/targets/Microsoft-AzureKubernetesServiceChaosMesh"
  3. Create the experiment using the Azure CLI, replacing $SUBSCRIPTION_ID, $RESOURCE_GROUP, and $EXPERIMENT_NAME with the properties for your experiment. Make sure you have saved and uploaded your experiment JSON and update experiment.json with your JSON filename.

    az rest --method put --uri$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.Chaos/experiments/$EXPERIMENT_NAME?api-version=2021-09-15-preview --body @experiment.json

    Each experiment creates a corresponding system-assigned managed identity. Note of the principalId for this identity in the response for the next step.

Give experiment permission to your AKS cluster

When you create a chaos experiment, Chaos Studio creates a system-assigned managed identity that executes faults against your target resources. This identity must be given appropriate permissions to the target resource for the experiment to run successfully.

Give the experiment access to your resource(s) using the command below, replacing $EXPERIMENT_PRINCIPAL_ID with the principalId from the previous step and $RESOURCE_ID with the resource ID of the target resource (in this case, the AKS cluster resource ID). Run this command for each resource targeted in your experiment.

az role assignment create --role "Azure Kubernetes Cluster Admin Role" --assignee-object-id $EXPERIMENT_PRINCIPAL_ID --scope $RESOURCE_ID

Run your experiment

You are now ready to run your experiment. To see the impact, we recommend opening your AKS cluster overview and going to Insights in a separate browser tab. Live data for the Active Pod Count will show the impact of running your experiment.

  1. Start the experiment using the Azure CLI, replacing $SUBSCRIPTION_ID, $RESOURCE_GROUP, and $EXPERIMENT_NAME with the properties for your experiment.

    az rest --method post --uri$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.Chaos/experiments/$EXPERIMENT_NAME/start?api-version=2021-09-15-preview
  2. The response includes a status URL that you can use to query experiment status as the experiment runs.

Next steps

Now that you have run an AKS Chaos Mesh service-direct experiment, you are ready to: