Introduction to k6 Load Chaos in LitmusChaos
Tags: chaosengineering, litmuschaos, tutorial, kubernetes
Published: 2024-05-27 03:06:13

In today's complex digital landscape, application resilience is crucial. Chaos engineering, using tools like LitmusChaos, intentionally introduces faults to uncover systemic issues missed by traditional tests. From now on, we can use k6, a load-testing tool with LitmusChaos. This post explores chaos engineering with LitmusChaos and demonstrates a k6 load chaos experiment.

---

### Table of Contents

- What are chaos engineering and LitmusChaos
- Introduction to k6
- Injecting k6 load chaos with LitmusChaos (demo)

---

### What are chaos engineering and LitmusChaos

What if our systems suddenly experience an outage? It's difficult to pinpoint the problem these days, especially since our systems are on Kubernetes, meaning they are microservices. While unit tests and integration tests can detect our application's weaknesses, they cannot detect weaknesses in our overall platform.

![Importance of Resilience](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/66s9si3jmaqf77t9o29g.png)

[The above diagram](https://docs.litmuschaos.io/docs/introduction/what-is-litmus#importance-of-resilience) shows the impact of resilience. Using chaos engineering can be a great way to achieve more than 90% resilience. Chaos Engineering is the discipline of experimenting on a system in order to build confidence in the system’s capability to withstand turbulent conditions in production [1]. Chaos engineering involves intentionally injecting faults into a system to test its resilience. LitmusChaos makes this process easier to implement by simplifying Chaos Engineering.

[LitmusChaos](https://litmuschaos.io/) (CNCF incubating project) is a Cloud-Native Chaos Engineering Framework with cross-cloud support. You can use Litmus to inject controlled chaos and run chaos experiments in staging and production environments, allowing SREs to identify bugs and vulnerabilities. If you want to know more about Litmuschaos, Check out the [documentation](https://docs.litmuschaos.io/)!

### Introduction to k6
k6 is an open source load testing tool designed for developers to allow teams to create tests-as-code, integrate performance tests as part of the software development lifecycle, and help users test, analyze, and fix application performance issues. k6 supports [various types](https://grafana.com/docs/k6/latest/testing-guides/test-types/) of load testing (ex. spike, smoke, stress).

Now LitmusChaos supports k6 load testing as a chaos fault so that we can simulate load generation to the target application as a part of chaos testing on Kubernetes.

To know more about it, check out this [documentation](https://litmuschaos.github.io/litmus/experiments/categories/load/k6-loadgen/). You can also find our integration in the [k6 documentation](https://grafana.com/docs/k6/latest/misc/integrations/#chaos-engineering) 🚀


### Injecting k6 load chaos with LitmusChaos (demo)

Let us run the k6-loadgen chaos experiment. For simplicity, we will be injecting chaos into an [OpenTelemetry Demo](https://opentelemetry.io/docs/demo/).

#### Prerequisites
- [Docker](https://docs.docker.com/engine/install/), [minikube](https://minikube.sigs.k8s.io/docs/start/) (If you use your k8s cluster, skip this)
- Check out the otel-demo's [Prerequisites](https://opentelemetry.io/docs/demo/kubernetes-deployment/#prerequisites)
- If you haven't installed LitmusChaos yet, check out this [documentation](https://docs.litmuschaos.io/docs/getting-started/installation).

#### Install opentelemetry demo
After installing Minikube and LitmusChaos, We now install the opentelemetry demo. All you have to do is enter the code below.

```bash
helm repo add open-telemetry https://open-telemetry.github.io/opentelemetry-helm-charts
helm install my-otel-demo open-telemetry/opentelemetry-demo
```
If you want to customized a deployment setting, check out this [documentation](https://opentelemetry.io/docs/demo/kubernetes-deployment/).

We cannot directly access the otel-demo service, so we are using minikube's command to get an external URL

```bash
minikube service my-otel-demo-frontendproxy --url
```
the result is like one below

![external url](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ia3e4at2hz7xbqq7kgk0.png)

We now access the frontend service

![otel-demo-frontend](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i05e2j0azq4l0ywajszp.png)

You can access Grafana using given URL

```bash
http://<<given_url>>/grafana
```

We will use `Home > Dashboards > Demo Dashboard` today.


![grafana-dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0z2gu9mhj3yjgldn3neh.png)

#### Setup Probes
You can easily create a probe following this [documentation](https://docs.litmuschaos.io/docs/user-guides/create-resilience-probe). Enter a value like below.

```bash
// URL: http://my-otel-demo-frontendproxy.default.svc.cluster.local:8080
// METHOD: GET
// CRITERIA: ==
// Response Code: 200
```

![result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m2ql5hisdxk8ef20f8r9.png)


#### Writing a k6 script
You don't have to install a k6 engine! We just have to write a k6 script. We will use the below code and save it as `script.js`

```js
import http from 'k6/http';
import { sleep } from 'k6';
export const options = {
  vus: 1000,
  duration: '30s',
};
export default function () {
  http.get('http://my-otel-demo-frontendproxy.default.svc.cluster.local:8080');
  sleep(1);
}
```

If you'd like to write a more professional script, you can check out this [documentation](https://grafana.com/docs/k6/latest/).

and let's make a secret

```bash
kubectl create secret generic k6-script \
    --from-file=<<script-path>>/script.js -n <<chaos_infrastructure_namespace>>
```

Now that all the preliminary work is done, let's create a chaos experiment.

#### Let's make a chaos experiment

Click `Chaos Experiments` > `+ New Experiment` to create a new chaos experiment.

![int](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2zjcbncaee0p4t7ummfy.png)

After clicking the `Blank Canvas` and `Add` buttons, we can choose chaos fault in ChaosHub. We use `k6-loadgen`
![hub](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0qdoiw7nbogns5npgq1y.png)

We don't have to enter any values into the `Target Application` (we will fix it later). In the `Tune Fault` tab, we enter the secret name and key we created before.

![tune](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qyi9evbnjy2itl8vrlpw.png)

Lastly, We add a probe that was created before.
![probe](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ydexol36gztshdx3ndpm.png)

Okay, all done! Let's execute a chaos experiment 🚀

A few minutes later, Our chaos experiment succeeded. Congratulations 🎉 
![result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z1ukk56bn2995tov22y6.png)

We can see the load testing result on the Grafana dashboard. Go to `Dashboards` > `Demo Dashboard` > `Requests Rate for frontend by span name` > `frontend-proxy`. We can see the result below.

![chaos-experiment-result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9zl58h7wgho2fwg70bpf.png)

### Summary
k6-loadgen fault simulates load generation on the target hosts for a specific chaos duration. The effects of chaos engineering can be maximized by designing experiments with k6-loadgen like other chaos faults in LitmusChaos.

If you are interested in LitmusChaos, Join the community! You can join the LitmusChaos community on [GitHub](https://github.com/litmuschaos/litmus) and [Slack](https://kubernetes.slack.com/?redir=%2Farchives%2FCNXNB0ZTN).

Thank you for reading 🙏

Namkyu Park
Maintainer of LitmusChaos
[LinkedIn](https://www.linkedin.com/in/namkyupark1999/?locale=en_US) | [GitHub](https://github.com/namkyu1999)

### Reference
[1] [PRINCIPLES OF CHAOS ENGINEERING](https://principlesofchaos.org)