Distributed testing allows you to run a single k6 test across multiple machines, enabling higher load generation and testing from multiple IP addresses. This guide covers using the k6 Kubernetes operator to orchestrate distributed tests.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/grafana/k6-docs/llms.txt
Use this file to discover all available pages before exploring further.
When to Use Distributed Tests
Consider distributed testing when:High Load Requirements
A single optimized node cannot generate the load your test requires
Multiple IP Addresses
Your system under test needs to be accessed from different IP addresses
Geographic Distribution
You need to test from multiple geographic locations simultaneously
Kubernetes Environment
Kubernetes is already your preferred operations environment
Introducing k6-operator
The k6-operator is a Kubernetes operator that automates distributed k6 test execution. It defines a customTestRun resource that specifies:
- The k6 test script to execute
- The number of parallel pods (
parallelism) - Environment configuration
- Resource allocation
TestRun, the operator automatically provisions k6 test jobs across your cluster, splitting the workload using execution segments.
Getting Started
Prerequisites
- Access to a Kubernetes cluster
kubectlinstalled and configured- Appropriate cluster permissions
Step 1: Install the Operator
Install the k6-operator in your cluster:Step 2: Create a Test Script
Create your k6 test script (same as for local testing):test.js
Always validate your script locally with
k6 run test.js before deploying to Kubernetes.Step 3: Add Test Script to Kubernetes
You can provide test scripts to the operator using either ConfigMaps or PersistentVolumes.- ConfigMap (Recommended)
- PersistentVolume
Create a ConfigMap from your test file:Limitations: ConfigMaps have a 1 MiB size limit. For larger scripts, use PersistentVolumes.
Step 4: Create a TestRun Resource
Define aTestRun custom resource to execute your test:
- From ConfigMap
- From PersistentVolume
run-k6-from-configmap.yaml
- Runs the test across 4 parallel pods
- Loads script from the
my-testConfigMap - Automatically splits the workload using execution segments
Advanced Configuration
Environment Variables
Pass configuration through environment variables:Command-Line Arguments
Pass k6 options via command-line arguments:Resource Limits
Control pod resources:Automatic Cleanup
Automatically remove test resources after completion:Running Tests
Complete Example
Here’s a comprehensive distributed test setup:distributed-load-test.yaml
test.js
Monitoring Distributed Tests
Using Prometheus
Using Grafana Cloud k6
Troubleshooting
Pods not starting
Pods not starting
Check pod events and logs:Common issues:
- ConfigMap not found in namespace
- Insufficient cluster resources
- Image pull errors
Test execution errors
Test execution errors
View test runner logs:Check for:
- Script errors (syntax, logic)
- Network connectivity issues
- Resource constraints
Workload not splitting correctly
Workload not splitting correctly
Verify execution segments:Ensure
parallelism is set correctly in the TestRun spec.Results not aggregating
Results not aggregating
For distributed tests, use external outputs:
- Prometheus Remote Write
- Grafana Cloud k6
- InfluxDB
- Other supported outputs
Best Practices
Test Locally First
Always validate scripts with
k6 run before deploying to KubernetesUse External Outputs
Configure real-time outputs for aggregated metrics across all pods
Set Resource Limits
Define appropriate CPU and memory limits to prevent cluster overload
Enable Cleanup
Use
cleanup: post to automatically remove completed test resourcesMonitor During Tests
Watch pod logs and metrics in real-time to catch issues early
Version Test Scripts
Store test scripts in version control and use ConfigMaps for deployment
Example: High-Scale Distributed Test
high-scale-test.yaml
With
parallelism: 20 and TEST_VUS: 10000, each pod will run ~500 VUs. Adjust based on your pod resource limits and cluster capacity.Next Steps
- Learn about k6 scenarios for advanced execution control
- Explore k6 outputs for metrics storage
- Check the k6-operator documentation for advanced features
- Join the k6 Community Forum for help and discussions