Skip to main content

K6 loadgen

K6 loadgen fault simulates load generation on the target hosts for a specific chaos duration. This fault:

  • Slows down or makes the target host unavailable due to heavy load.
  • Checks the performance of the application or process running on the instance.
  • Supports various types of load testing (ex. spike, smoke, stress)

k6 Loadgen Chaos

Use cases

  • Simulate high traffic to test the performance and reliability of RESTful APIs.
  • Automate performance testing in CI/CD pipelines to catch regressions early.
  • Evaluate the behavior of web applications under heavy user loads.
  • Continuously monitor cloud infrastructure performance by generating synthetic traffic.

Prerequisites

  • Kubernetes > 1.17 is required to execute this fault.
  • The target host should be accessible.
  • Ensure to create a Kubernetes secret that contains the javascript (JS) file within the Chaos Infrastructure's namespace. The easiest way to create a secret object is as follows::

An example of the JS file is as follows:

import http from 'k6/http';
import { check, sleep } from 'k6';

export default function () {
let res = http.get('https://google.com');

check(res, { 'status is 200': (r) => r.status === 200 });

sleep(0.3);
}
kubectl create secret generic k6-script \
--from-file=<<script-path>> -n <<chaos_infrastructure_namespace>>

Mandatory tunables

Tunable Description Notes
SCRIPT_SECRET_NAME Provide the k8s secret name of the JS script to run k6. Default to k6-script. For more information, go to k6 secret
SCRIPT_SECRET_KEY Provide the key of the k8s secret named SCRIPT_SECRET_NAME Default to script.js. For more information, go to k6 secret

Optional tunables

Tunable Description Notes
TOTAL_CHAOS_DURATION The time duration for chaos injection (in seconds). Default: 60s. For more information, go to duration of the chaos.
LOAD_IMAGE Image used in helper pod that contains the chaos injection logic. Default: ghcr.io/grafana/k6-operator:latest-runner. For more information, go to custom load image.
RAMP_TIME Wait period before and after injecting chaos (in seconds). For example, 30s. For more information, go to ramp time.

K6 Secret

It defines the secret and key names for the k6 load generation script. You can adjust them using the SCRIPT_SECRET_NAME and SCRIPT_SECRET_KEY environment variables, respectively..

The following YAML snippet illustrates the use of this environment variable:

apiVersion: litmuschaos.io/v1alpha1
kind: ChaosEngine
metadata:
name: load-nginx
spec:
engineState: "active"
chaosServiceAccount: litmus-admin
experiments:
- name: k6-load-generator
spec:
components:
env:
- name: SCRIPT_SECRET_NAME
value: 'k6-script'
- name: SCRIPT_SECRET_KEY
value: 'script.js'

Custom load image

Image of the k6 load generator. Tune it by using the LOAD_IMAGE environment variable.

The following YAML snippet illustrates the use of this environment variable:

# provid a custom image for load generation
apiVersion: litmuschaos.io/v1alpha1
kind: ChaosEngine
metadata:
name: load-nginx
spec:
engineState: "active"
chaosServiceAccount: litmus-admin
experiments:
- name: k6-load-generator
spec:
components:
env:
- name: LOAD_IMAGE
value: ghcr.io/grafana/k6-operator:latest-runner