Load Testing in Kubernetes with k6 and Testkube

May 30, 2022
Est. reading time:
4 min.
Learn how to harness the power of k6 load testing in Kubernetes with Testkube's CLI and API.
Bruno Lopes
Product Leader
Table of Contents
Load Testing in Kubernetes with k6 and Testkube

## Introduction: k6 Load Testing

Kubernetes clusters are designed for reliability and a degree of “self-healing,” including scaling horizontally with more pods running in parallel, to handle sudden bursts of demand. But like all other parts of your application, from the UI to the APIs that power each service, you can enhance your baseline availability by load testing your infrastructure and understanding what happens.

One tool that helps you with that — and undoubtedly one of the most popular load testing tools today — is [k6](https://k6.io/). The open-source project is cloud native-friendly and supported by a large community of developers and testers — not to mention the 2021 [acquisition](https://k6.io/blog/joining-grafana-labs/) by [Grafana Labs](https://grafana.com/), the leader in infrastructure observability tools. The open-source project has 16,000+ stars on GitHub and more than 100 active contributors.

The state of testing Kubernetes clusters with k6

<p>The k6 team has previously announced their intention to make k6 more friendly toward cloud-native technology like Kubernetes, but support is still in experimental stages and its integration is fairly complex if you don’t have a strong DevOps background.</p>

In the current best practices guide ([Running distributed k6 tests on Kubernetes](https://k6.io/blog/running-distributed-tests-on-k8s/)), you need to clone a GitHub repository to use an operator which then you can deploy manually to your cluster using Make and Kustomize.

To view the results of a particular load test, you can run `kubectl logs [POD]` to see k6’s output but there are no instructions for seeing detailed performance testing metrics and dashboards for your tests, although it’s certainly possible with enough customization.

Working with an operator like this one will add complexity to your testing process and your CI/CD pipelines. Also it still won’t allow you to store or aggregate your results over time. Good news is that Testkube does a great job of adapting K6 into Kubernetes, solving all of those problems mentioned above.

How Testkube does Kubernetes load testing differently

<p>[Testkube](http://www.testkube.io) tries to solve both of those problems by tackling the process of executing a k6 load test through the Testkube CLI and API.</p>

With Testkube, users create and run commands with `kubectl testkube create test…` and `kubectl testkube run test…`, respectively. When running the Testkube CLI — or when the CI/CD pipeline does it for you — it sends these tests to the Testkube API running on the cluster, which in turn manages the resources required to run them. Once your tests finish, users can use Testkube’s UI to see the results if they would like.

With this process, you can aggregate any number of k6 test results in one place, and they’re stored alongside any other application tests you might be running, like Postman, Cypress, or SoapUI. You simplify the chain of load testing -> trying improvements -> testing your improvements, or even automate it entirely using CI/CD, to dramatically improve your performance testing lifecycle.

And you’ll get a much better sense of how your application will perform if you get the “[hug of death](https://en.wikipedia.org/wiki/Slashdot_effect)” from Hacker News or Reddit.

Let’s walk through all the steps together.

Get Testkube

<p>The fastest way to install Testkube is using Homebrew for macOS, APT for Linux, or chocolatey if you use windows. You can [click on this link](https://testkube.kubeshop.io/download) to see how.</p>
In the case of Mac OS you can do it by typing using brew:

-- CODE language-bash --
brew install testkube

Once you have Testkube on your command line, and a [Kubernetes cluster](https://kubernetes.io/docs/tasks/tools/) you could install it on. You can then [install Testkube’s components](https://kubeshop.github.io/testkube/installing/#install-testkube-components-in-your-cluster) to your cluster:

-- CODE language-bash --
kubectl testkube install

With only these commands, you’re now ready to start testing against your cluster!

Our [k6 executor](https://github.com/kubeshop/testkube-executor-k6), which takes your k6 load testing scripts and launches them directly inside of your cluster, comes enabled by default, so there’s nothing else for you to manage or administer.

Create your k6 load test and run it

<p>I won’t cover the nuances of writing a k6 load testing script here—the [k6 documentation](https://k6.io/docs/getting-started/running-k6/) is a fantastic resource for that—but instead, let’s use an [example script](https://k6.io/docs/getting-started/running-k6/#stages-ramping-up-down-vus) to showcase how Kubernetes load testing works with Testkube and k6.</p>

-- CODE language-bash --
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
 stages: [
   { duration: '5s', target: 20 },
   { duration: '5s', target: 10 },
export default function () {
 const res = http.get('https://httpbin.test.k6.io/');
 check(res, { 'status was 200': (r) => r.status == 200 });

Save this file on your local machine as `k6-test.js`.

Now that you have the k6 load test script, you can execute it in just two steps:

First you need to define the test as the “k6/script” type and then you can run it:

-- CODE language-bash --
$ kubectl testkube create test --file path/to/your/k6-test.js --type "k6/script" --name k6-test
$ kubectl testkube run test k6-test -f

View logs and results in the Testkube dashboard

<p>Testkube handles every part of running, managing, storing, and visualizing your k6 load testing scripts. Once a single execution finishes, it’s available for viewing on your Testkube dashboard.</p>

What’s next?

<p>Now that you’ve run a k6 load test against your Kubernetes application for the first time, you’re ready to add it as a step in your CI/CD pipeline alongside any other UI/API tests you might have already run.</p>

With load testing built into your development lifecycle and all your results condensed into a single UI, you can start to understand how your cloud-native application performs over time and through significant change. Discover regressions, dig into root causes, and make optimizations.

I would love to hear all about your k6 load testing results and wins—the entire [Testkube](https://testkube.kubeshop.io/) team, plus a vibrant community of fellow Kubernetes testers, are on [Discord](https://discord.com/invite/6zupCZFQbe). Let me know what’s working for you and where you see more opportunities in the future. We’re just getting started in building the most comprehensive (and friendliest!) cloud-native testing framework for Kubernetes so feel free to follow us on [Twitter @testkube_io](https://mobile.twitter.com/Testkube_io).

Open source

Recent Articles

Subscribe to stay up to date by getting the blog direct to your inbox
Or join the Kubeshop Community in one of these channels