Skip to content

Commit f6db219

Browse files
authored
Merge pull request #179563 from johnkemnetz/master
Add CLI version of AKS article
2 parents 8efd945 + 7b4d8d7 commit f6db219

8 files changed

+337
-49
lines changed

.openpublishing.redirection.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47460,6 +47460,11 @@
4746047460
"source_path_from_root": "/articles/chaos-studio/chaos-studio-tutorial-service-direct.md",
4746147461
"redirect_url": "/azure/chaos-studio/chaos-studio-tutorial-service-direct-portal",
4746247462
"redirect_document_id": false
47463+
},
47464+
{
47465+
"source_path_from_root": "/articles/chaos-studio/chaos-studio-tutorial-aks.md",
47466+
"redirect_url": "/azure/chaos-studio/chaos-studio-tutorial-aks-portal",
47467+
"redirect_document_id": false
4746347468
}
4746447469
]
4746547470
}

articles/chaos-studio/TOC.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@
4343
- name: CLI
4444
href: chaos-studio-tutorial-agent-based-cli.md
4545
- name: Create and run experiment - Chaos Mesh faults on AKS
46-
href: chaos-studio-tutorial-aks.md
46+
items:
47+
- name: Portal
48+
href: chaos-studio-tutorial-aks-portal.md
49+
- name: CLI
50+
href: chaos-studio-tutorial-aks-cli.md
4751
- name: Troubleshoot common issues
4852
href: troubleshooting.md
4953
- name: Reference

articles/chaos-studio/chaos-studio-fault-library.md

Lines changed: 109 additions & 41 deletions
Large diffs are not rendered by default.

articles/chaos-studio/chaos-studio-permissions-security.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,4 @@ Chaos Studio encrypts all data by default. Chaos Studio only accepts input for s
6161
## Next steps
6262
Now that you understand how to secure your chaos experiment you are ready to:
6363
- [Create and run your first experiment](chaos-studio-tutorial-service-direct-portal.md)
64-
- [Create and run your first Azure Kubernetes Service experiment](chaos-studio-tutorial-aks.md)
64+
- [Create and run your first Azure Kubernetes Service experiment](chaos-studio-tutorial-aks-portal.md)
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
---
2+
title: Create an experiment that uses an AKS Chaos Mesh fault using Azure Chaos Studio with the Azure CLI
3+
description: Create an experiment that uses an AKS Chaos Mesh fault with the Azure CLI
4+
author: johnkemnetz
5+
ms.topic: how-to
6+
ms.date: 11/11/2021
7+
ms.author: johnkem
8+
ms.service: chaos-studio
9+
ms.custom: template-how-to, ignite-fall-2021
10+
---
11+
12+
# Create a chaos experiment that uses a Chaos Mesh fault with the Azure CLI
13+
14+
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.
15+
16+
Azure Chaos Studio uses [Chaos Mesh](https://chaos-mesh.org/), a free, open-source chaos engineering platform for Kubernetes to inject faults into an AKS cluster. Chaos Mesh faults are [service-direct](chaos-studio-tutorial-aks-portal.md) 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.
17+
18+
## Prerequisites
19+
20+
- An Azure subscription. [!INCLUDE [quickstarts-free-trial-note](../../includes/quickstarts-free-trial-note.md)]
21+
- An AKS cluster. If you do not have an AKS cluster, you can [follow these steps to create one](../aks/kubernetes-walkthrough-portal.md).
22+
23+
## Launch Azure Cloud Shell
24+
25+
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.
26+
27+
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 [https://shell.azure.com/bash](https://shell.azure.com/bash). Select **Copy** to copy the blocks of code, paste it into the Cloud Shell, and select **Enter** to run it.
28+
29+
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]( /cli/azure/install-azure-cli).
30+
31+
## Set up Chaos Mesh on your AKS cluster
32+
33+
Before you can run Chaos Mesh faults in Chaos Studio, you need to install Chaos Mesh on your AKS cluster.
34+
35+
1. Run the following commands in an [Azure Cloud Shell](../cloud-shell/overview.md) 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.
36+
37+
```azurecli-interactive
38+
az aks get-credentials -g $RESOURCE_GROUP -n $CLUSTER_NAME
39+
helm repo add chaos-mesh https://charts.chaos-mesh.org
40+
helm repo update
41+
kubectl create ns chaos-testing
42+
helm install chaos-mesh chaos-mesh/chaos-mesh --namespace=chaos-testing --version 2.0.3 --set chaosDaemon.runtime=containerd --set chaosDaemon.socketPath=/run/containerd/containerd.sock
43+
```
44+
45+
2. Verify that the Chaos Mesh pods are installed by running the following command:
46+
47+
```azurecli-interactive
48+
kubectl get po -n chaos-testing
49+
```
50+
51+
You should see output similar to the following (a chaos-controller-manager and one or more chaos-daemons):
52+
53+
```bash
54+
NAME READY STATUS RESTARTS AGE
55+
chaos-controller-manager-69fd5c46c8-xlqpc 1/1 Running 0 2d5h
56+
chaos-daemon-jb8xh 1/1 Running 0 2d5h
57+
chaos-dashboard-98c4c5f97-tx5ds 1/1 Running 0 2d5h
58+
```
59+
60+
You can also [use the installation instructions on the Chaos Mesh website](https://chaos-mesh.org/docs/production-installation-using-helm/).
61+
62+
63+
## Enable Chaos Studio on your AKS cluster
64+
65+
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](chaos-studio-targets-capabilities.md) 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.).
66+
67+
1. Create a target by replacing `$RESOURCE_ID` with the resource ID of the AKS cluster you are onboarding:
68+
69+
```azurecli-interactive
70+
az rest --method put --url "https://management.azure.com/$RESOURCE_ID/providers/Microsoft.Chaos/targets/Microsoft-AzureKubernetesServiceChaosMesh?api-version=2021-09-15-preview" --body "{\"properties\":{}}"
71+
```
72+
73+
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](chaos-studio-fault-library.md).
74+
75+
```azurecli-interactive
76+
az rest --method put --url "https://management.azure.com/$RESOURCE_ID/providers/Microsoft.Chaos/targets/Microsoft-AzureKubernetesServiceChaosMesh/capabilities/$CAPABILITY?api-version=2021-09-15-preview" --body "{\"properties\":{}}"
77+
```
78+
79+
For example, if enabling the PodChaos capability:
80+
81+
```azurecli-interactive
82+
az rest --method put --url "https://management.azure.com/subscriptions/b65f2fec-d6b2-4edd-817e-9339d8c01dc4/resourceGroups/myRG/providers/Microsoft.ContainerService/managedClusters/myCluster/providers/Microsoft.Chaos/targets/Microsoft-AzureKubernetesServiceChaosMesh/capabilities/PodChaos-2.1?api-version=2021-09-15-preview" --body "{\"properties\":{}}"
83+
```
84+
85+
This must be done for each capability you want to enable on the cluster.
86+
87+
You have now successfully onboarded your AKS cluster to Chaos Studio.
88+
89+
## Create an experiment
90+
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.
91+
92+
1. Create a Chaos Mesh jsonSpec:
93+
1. Visit the Chaos Mesh documentation for a fault type, [for example, the PodChaos type](https://chaos-mesh.org/docs/simulate-pod-chaos-on-kubernetes/#create-experiments-using-yaml-configuration-files).
94+
2. Formulate the YAML configuration for that fault type using the Chaos Mesh documentation.
95+
96+
```yaml
97+
apiVersion: chaos-mesh.org/v1alpha1
98+
kind: PodChaos
99+
metadata:
100+
name: pod-failure-example
101+
namespace: chaos-testing
102+
spec:
103+
action: pod-failure
104+
mode: all
105+
duration: '600s'
106+
selector:
107+
namespaces:
108+
- default
109+
```
110+
3. Remove any YAML outside of the `spec` (including the spec property name), and remove the indentation of the spec details.
111+
112+
```yaml
113+
action: pod-failure
114+
mode: all
115+
duration: '600s'
116+
selector:
117+
namespaces:
118+
- default
119+
```
120+
4. Use a [YAML-to-JSON converter like this one](https://www.convertjson.com/yaml-to-json.htm) to convert the Chaos Mesh YAML to JSON and minimize it.
121+
122+
```json
123+
{"action":"pod-failure","mode":"all","duration":"600s","selector":{"namespaces":["default"]}}
124+
```
125+
5. Use a [JSON string escape tool like this one](https://www.freeformatter.com/json-escape.html) to escape the JSON spec.
126+
127+
```json
128+
{\"action\":\"pod-failure\",\"mode\":\"all\",\"duration\":\"600s\",\"selector\":{\"namespaces\":[\"default\"]}}
129+
```
130+
131+
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](/rest/api/chaosstudio/experiments/create-or-update), the [fault library](chaos-studio-fault-library.md), and the jsonSpec created in the previous step.
132+
133+
```json
134+
{
135+
"location": "centralus",
136+
"identity": {
137+
"type": "SystemAssigned"
138+
},
139+
"properties": {
140+
"steps": [
141+
{
142+
"name": "AKS pod kill",
143+
"branches": [
144+
{
145+
"name": "AKS pod kill",
146+
"actions": [
147+
{
148+
"type": "continuous",
149+
"selectorId": "Selector1",
150+
"duration": "PT10M",
151+
"parameters": [
152+
{
153+
"key": "jsonSpec",
154+
"value": "{\"action\":\"pod-failure\",\"mode\":\"all\",\"duration\":\"600s\",\"selector\":{\"namespaces\":[\"default\"]}}"
155+
}
156+
],
157+
"name": "urn:csci:microsoft:azureKubernetesServiceChaosMesh:podChaos/2.1"
158+
}
159+
]
160+
}
161+
]
162+
}
163+
],
164+
"selectors": [
165+
{
166+
"id": "Selector1",
167+
"type": "List",
168+
"targets": [
169+
{
170+
"type": "ChaosTarget",
171+
"id": "/subscriptions/b65f2fec-d6b2-4edd-817e-9339d8c01dc4/resourceGroups/myRG/providers/Microsoft.ContainerService/managedClusters/myCluster/providers/Microsoft.Chaos/targets/Microsoft-AzureKubernetesServiceChaosMesh"
172+
}
173+
]
174+
}
175+
]
176+
}
177+
}
178+
```
179+
180+
2. 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.
181+
182+
```azurecli-interactive
183+
az rest --method put --uri https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.Chaos/experiments/$EXPERIMENT_NAME?api-version=2021-09-15-preview --body @experiment.json
184+
```
185+
186+
Each experiment creates a corresponding system-assigned managed identity. Note of the `principalId` for this identity in the response for the next step.
187+
188+
## Give experiment permission to your AKS cluster
189+
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](chaos-studio-fault-providers.md) to the target resource for the experiment to run successfully.
190+
191+
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.
192+
193+
```azurecli-interactive
194+
az role assignment create --role "Azure Kubernetes Cluster User Role" --assignee-object-id $EXPERIMENT_PRINCIPAL_ID --scope $RESOURCE_ID
195+
```
196+
197+
## Run your experiment
198+
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.
199+
200+
1. Start the experiment using the Azure CLI, replacing `$SUBSCRIPTION_ID`, `$RESOURCE_GROUP`, and `$EXPERIMENT_NAME` with the properties for your experiment.
201+
202+
```azurecli-interactive
203+
az rest --method post --uri https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.Chaos/experiments/$EXPERIMENT_NAME/start?api-version=2021-09-15-preview
204+
```
205+
206+
2. The response includes a status URL that you can use to query experiment status as the experiment runs.
207+
208+
## Next steps
209+
Now that you have run an AKS Chaos Mesh service-direct experiment, you are ready to:
210+
- [Create an experiment that uses agent-based faults](chaos-studio-tutorial-agent-based-portal.md)
211+
- [Manage your experiment](chaos-studio-run-experiment.md)

articles/chaos-studio/chaos-studio-tutorial-aks.md renamed to articles/chaos-studio/chaos-studio-tutorial-aks-portal.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: Create an experiment that uses an AKS Chaos Mesh fault with Azure Chaos Studio
3-
description: Create an experiment that uses an AKS Chaos Mesh fault
2+
title: Create an experiment that uses an AKS Chaos Mesh fault using Azure Chaos Studio with the Azure portal
3+
description: Create an experiment that uses an AKS Chaos Mesh fault with the Azure portal
44
author: johnkemnetz
55
ms.topic: how-to
66
ms.date: 11/01/2021
@@ -9,11 +9,11 @@ ms.service: chaos-studio
99
ms.custom: template-how-to, ignite-fall-2021
1010
---
1111

12-
# Create a chaos experiment that uses a Chaos Mesh fault to kill AKS pods
12+
# Create a chaos experiment that uses a Chaos Mesh fault to kill AKS pods with the Azure portal
1313

1414
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.
1515

16-
Azure Chaos Studio uses [Chaos Mesh](https://chaos-mesh.org/), a free, open-source chaos engineering platform for Kubernetes to inject faults into an AKS cluster. Chaos Mesh faults are [service-direct](chaos-studio-tutorial-aks.md) 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.
16+
Azure Chaos Studio uses [Chaos Mesh](https://chaos-mesh.org/), a free, open-source chaos engineering platform for Kubernetes to inject faults into an AKS cluster. Chaos Mesh faults are [service-direct](chaos-studio-tutorial-aks-portal.md) 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.
1717

1818
## Prerequisites
1919

articles/chaos-studio/index.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ landingContent:
3737
- text: Create an experiment that uses an agent-based fault with Azure Chaos Studio
3838
url: chaos-studio-tutorial-agent-based-portal.md
3939
- text: Create an experiment that uses an AKS Chaos Mesh fault with Azure Chaos Studio
40-
url: chaos-studio-tutorial-aks.md
40+
url: chaos-studio-tutorial-aks-portal.md
4141

4242
# Card
4343
- title: Reference

articles/chaos-studio/troubleshooting.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Agent-based faults may fail for a variety of reasons related to missing prerequi
5656

5757
### AKS Chaos Mesh faults fail
5858
AKS Chaos Mesh faults may fail for a variety of reasons related to missing prerequisites:
59-
* Chaos Mesh must first be installed on the AKS cluster before using the AKS Chaos Mesh faults. Instructions can be found in the [Chaos Mesh faults on AKS tutorial](chaos-studio-tutorial-aks.md#set-up-chaos-mesh-on-your-aks-cluster).
59+
* Chaos Mesh must first be installed on the AKS cluster before using the AKS Chaos Mesh faults. Instructions can be found in the [Chaos Mesh faults on AKS tutorial](chaos-studio-tutorial-aks-portal.md#set-up-chaos-mesh-on-your-aks-cluster).
6060
* Chaos Mesh must be version 2.0.4 or greater. You can get the Chaos Mesh version by connecting to your AKS cluster and running `helm version chaos-mesh`.
6161
* Chaos Mesh must be installed with the namespace `chaos-testing`. Other namespace names for Chaos Mesh are not supported.
6262
* The Azure Kubernetes Service Cluster Admin role must be assigned to the system-assigned managed identity for the chaos experiment.

0 commit comments

Comments
 (0)