Introduction
If you’re new to K6 and want to start with the basics, check out my other blog:
Getting Started with Grafana K6: Performance Testing for Modern Web Applications
This guide covers fundamental concepts to get you familiar with K6 and running simple load tests.
Integrating load testing with K6 into your CI/CD pipeline ensures that your applications are continually tested for performance and can handle real-world loads. In this guide, we’ll set up K6 tests to run as part of a Jenkins CI/CD pipeline on Amazon EKS.
Prerequisites
- Jenkins: Installed on a local server or hosted on a cloud instance.
- Amazon EKS: An EKS cluster set up and ready for deployment.
- K6: Installed on Jenkins or included in a Docker image used by Jenkins.
- kubectl: Installed on Jenkins to manage Kubernetes commands.
Step 1: Prepare Jenkins and EKS for K6 Integration
- Install Jenkins Plugins:
- Go to Manage Jenkins → Manage Plugins.
- Install the Kubernetes and Pipeline plugins, which help with Kubernetes interactions and defining pipeline steps.
2. Set Up Kubernetes Configuration:
- Ensure Jenkins has permissions to access your EKS cluster.
- Set up the Kubernetes configuration (
kubeconfig
) on Jenkins by adding it as a Jenkins credential or configuring it on the Jenkins server.
3. Create a Docker Image with K6:
- To run K6 in a Kubernetes pod, create a custom Docker image with K6 pre-installed
FROM loadimpact/k6
RUN apk add --no-cache curl
- Build and push this image to a container registry, such as Amazon ECR or Docker Hub.
Step 2: Create a K6 Test Script
- Define the Test Script:
- Write a K6 test script (
test.js
) that defines the performance scenario. Here’s a basic example:
import http from 'k6/http';
import { check, sleep } from 'k6';
export let options = {
vus: 10,
duration: '30s',
};
export default function () {
let res = http.get('https://test-api.yourapp.com');
check(res, { 'status was 200': (r) => r.status == 200 });
sleep(1);
}
- Save this script to your version control repository (e.g., GitHub or GitLab).
Step 3: Configure Jenkins Pipeline
- Set Up Jenkinsfile:
- Create a
Jenkinsfile
in your project’s repository to define the pipeline. Here’s an example of the pipeline configuration:
pipeline {
agent {
kubernetes {
label 'k6-test-runner'
defaultContainer 'k6'
yaml """
apiVersion: v1
kind: Pod
metadata:
labels:
app: k6-load-test
spec:
containers:
- name: k6
image: your-docker-repo/k6:latest
command:
- cat
tty: true
"""
}
}
environment {
KUBECONFIG = credentials('eks-kubeconfig') // Ensure this matches your Jenkins credentials ID
}
stages {
stage('Run K6 Load Test') {
steps {
container('k6') {
script {
sh 'k6 run /path/to/your/test.js'
}
}
}
}
}
post {
always {
archiveArtifacts artifacts: '**/*.json', allowEmptyArchive: true
junit 'results.xml'
}
}
}
2. Explanation of the Pipeline:
Agent: Specifies the Kubernetes agent with the k6
container, pulling from your custom Docker image.
Environment: Adds KUBECONFIG
as a Jenkins credential so that kubectl
can interact with EKS.
Stages:
- Run K6 Load Test: Executes the K6 script using the
sh
command to invokek6 run /path/to/test.js
.
Post-Action:
- always: Archives results and uploads JUnit-formatted results for Jenkins to display.
Step 4: Running and Monitoring the Pipeline
- Trigger the Pipeline:
- In Jenkins, trigger the pipeline manually or set up a webhook to start the pipeline automatically upon code push.
2. Monitor K6 Output:
- As the test runs, Jenkins will display the K6 test logs in the console output, showing live results such as response times, throughput, and error rates.
3. Viewing the Results in Jenkins:
- After completion, view detailed results and metrics on Jenkins. You can also integrate the results with Grafana for real-time visualization if needed.
Step 5: Scaling and Optimization
- Configuring More Virtual Users:
- In the K6 script, modify
vus
andduration
values to scale the test scenario.
2. Automating K6 Tests in the CI/CD Pipeline:
- Schedule this Jenkins pipeline to run periodically (e.g., daily) to monitor performance trends over time.
Conclusion
By following these steps, you have successfully integrated K6 into a Jenkins pipeline on Amazon EKS, enabling continuous performance testing for your web applications. This setup ensures that performance remains a priority across deployment cycles, making it easy to identify bottlenecks and maintain a high-quality user experience.