github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/docs/developer_docs/fault_injection/http-fault.md (about) 1 --- 2 title: Simulate HTTP faults 3 description: Simulate HTTP faults 4 sidebar_position: 6 5 sidebar_label: Simulate HTTP faults 6 --- 7 8 # Simulate HTTP faults 9 10 HTTPChaos experiments simulate the fault scenarios during the HTTP request and response processing. Currently, HTTPChaos supports simulating the following fault types: 11 12 * Abort: blocks requests and responses. 13 * Delay: injects latency into the request or response. 14 * Replace: replaces part of the content in HTTP request or response messages. 15 * Patch: adds additional content to the HTTP request or response messages. 16 17 HTTP faults support combining different fault types. If you have configured multiple HTTP fault types at the same time when creating HTTPChaos experiments, the order of injecting the faults follows abort -> delay -> replace -> patch. When the abort fault causes short circuits, the connection will be directly interrupted. 18 19 ## Before you start 20 21 Before injecting the faults supported by HTTPChaos, make sure the following requirements are met: 22 23 * There is no control manager of Chaos Mesh running on the target Pod. 24 * The rules affect both clients and servers in the Pod. If you want to affect only one of them, refer to the [official specific side](https://chaos-mesh.org/docs/simulate-http-chaos-on-kubernetes/#specify-side) section. 25 * HTTPS access should be disabled because injecting HTTPS connections is not supported currently. 26 * To make HTTPChaos injection take effect, the client should avoid reusing TCP socket. This is because HTTPChaos does not affect the HTTP requests that are sent via TCP socket before the fault injection. 27 * Use non-idempotent requests (such as most of the POST requests) with caution in production environments. If such requests are used, the target service may not return to normal status by repeating requests after the fault injection. 28 29 ## Simulate fault injections by kbcli 30 31 This table below describes the general flags for network faults. 32 33 📎 Table 1. kbcli fault network http flags description 34 35 | Option | Description | Default value | Required | 36 | :----------------------- | :------------------------ | :------------ | :------- | 37 | `--target` | It specifies whether the target of fault injuection is Request or Response. The target-related fields should be configured at the same time. | Request | No | 38 | `--port` | It specifies the TCP port that the target service listens. | 80 | No | 39 | `--path` | The URL path of the target request. Supports [Matching wildcards](https://www.wikiwand.com/en/Matching_wildcards). | * | No | 40 | `--method` | It specifies the URL that the target requests. | `GET` | No | 41 | `--code` | It specifies the status code responded by the target. It is effective only when `target=response`. | 0 | No | 42 43 ### Abort 44 45 The command below injects one-minute abort chaos to the specified Pod. 46 47 ```bash 48 kbcli fault network http abort --duration=1m 49 ``` 50 51 ### Delay 52 53 The command below injects a 15-second latency chaos to the specified Pod. 54 55 ```bash 56 kbcli fault network http delay --delay=15s 57 ``` 58 59 ### Replace 60 61 The command below replaces part of content in HTTP request or response messages for 1 minute. 62 63 ```bash 64 kbcli fault network http replace --replace-method=PUT --duration=1m 65 ``` 66 67 ### Patch 68 69 The command below adds additional contents to HTTP request or response messages. 70 71 ```bash 72 kbcli fault network http patch --body='{"key":""}' --type=JSON --duration=30s 73 ``` 74 75 ## Simulate fault injections by YAML file 76 77 This section introduces the YAML configuration file examples. You can view the YAML file by adding `--dry-run` at the end of the above kbcli commands. Meanwhile, you can also refer to the [Chaos Mesh official docs](https://chaos-mesh.org/docs/next/simulate-http-chaos-on-kubernetes/#create-experiments-using-yaml-files) for details. 78 79 ### HTTP-abort example 80 81 1. Write the experiment configuration to the `http-abort.yaml` file. 82 83 In the following example, Chaos Mesh injects 1-minute abort chaos to the specified Pod for 1 minute. 84 85 ```yaml 86 apiVersion: chaos-mesh.org/v1alpha1 87 kind: HTTPChaos 88 metadata: 89 creationTimestamp: null 90 generateName: http-chaos- 91 namespace: default 92 spec: 93 abort: true 94 duration: 1m 95 method: GET 96 mode: all 97 path: '*' 98 port: 80 99 selector: 100 namespaces: 101 - default 102 target: Request 103 ``` 104 105 2. Run `kubectl` to start an experiment. 106 107 ```bash 108 kubectl apply -f ./http-abort.yaml 109 ``` 110 111 ### HTTP-delay example 112 113 114 1. Write the experiment configuration to the `http-delay.yaml` file. 115 116 In the following example, Chaos Mesh injects a 15-second latency chaos to the specified Pod. 117 118 ```yaml 119 apiVersion: chaos-mesh.org/v1alpha1 120 kind: HTTPChaos 121 metadata: 122 creationTimestamp: null 123 generateName: http-chaos- 124 namespace: default 125 spec: 126 delay: 15s 127 duration: 10s 128 method: GET 129 mode: all 130 path: '*' 131 port: 80 132 selector: 133 namespaces: 134 - default 135 target: Request 136 ``` 137 138 2. Run `kubectl` to start an experiment. 139 140 ```bash 141 kubectl apply -f ./http-delay.yaml 142 ``` 143 144 ### HTTP-replace example 145 146 1. Write the experiment configuration to the `http-replace.yaml` file. 147 148 In the following example, Chaos Mesh replaces part of content in HTTP request or response messages for 1 minute. 149 150 ```yaml 151 apiVersion: chaos-mesh.org/v1alpha1 152 kind: HTTPChaos 153 metadata: 154 creationTimestamp: null 155 generateName: http-chaos- 156 namespace: default 157 spec: 158 duration: 1m 159 method: GET 160 mode: all 161 path: '*' 162 port: 80 163 replace: 164 method: PUT 165 selector: 166 namespaces: 167 - default 168 target: Request 169 ``` 170 171 2. Run `kubectl` to start an experiment. 172 173 ```bash 174 kubectl apply -f ./http-replace.yaml 175 ``` 176 177 ### HTTP-patch example 178 179 1. Write the experiment configuration to the `http-patch.yaml` file. 180 181 In the following example, Chaos Mesh adds additional contents to HTTP request or response messages. 182 183 ```yaml 184 apiVersion: chaos-mesh.org/v1alpha1 185 kind: HTTPChaos 186 metadata: 187 creationTimestamp: null 188 generateName: http-chaos- 189 namespace: default 190 spec: 191 duration: 30s 192 method: GET 193 mode: all 194 patch: 195 body: 196 type: JSON 197 value: '{"key":""}' 198 path: '*' 199 port: 80 200 selector: 201 namespaces: 202 - default 203 target: Request 204 ``` 205 206 2. Run `kubectl` to start an experiment. 207 208 ```bash 209 kubectl apply -f ./http-patch.yaml 210 ```