Skip to main content

Setup Jenkins Pipeline for JMeter Kubernetes

Jenkins Setup


Jenkins offers a simple way to set up a continuous integration or continuous delivery (CI/CD) environment for almost any combination of languages and source code repositories using pipelines, as well as automating other routine development tasks. 

I am using Jenkins LTS war file to deploy Jenkins, which is downloaded and deployed using jenkins.sh script which is present under jenkins folder.

The script perform below actions
  • Download Jenkins.war file if not present in directory from which script is being run
  • Start Jenkins with Java installed on local machine
Once Jenkins start you can see "pid" file and "jenkins.log" file which will contain secret password if you are starting Jenkins for the first time, follow the instructions from Jenkins to complete setup. Complete setup using instructions provided to customize to your requirement. I have used a simple setup with default plugins for this example.

JMeter Kubernetes Pipeline

Once Jenkins setup is complete we can configure Pipeline to perform actions to Perform Load test,

Pipeline has multiple steps as below:
  1. Cleanup of existing results\logs for new run
  2. Pull JMeter image that was created, if not please refer to link
  3. Get latest test script from repository(I used Github)
  4. Build JMeter Kubernetes Config for Master and Slaves
  5. Deploy JMeter Slaves in Kubernetes cluster
  6. Deploy JMeter Master in Kubernetes cluster and start test
  7. Stop JMeter Pods at end of test
  8. Archive test results for further reference
  9. Publish results to team
I have created a pipeline that to do the above, you can make required changes to match your requirement.

Note: Before we can execute the build we need to configure Grafana and Influx DB for live monitoring.

Below are the options that can be modified as per your requirement.

JMeter Container Image Config

Docker image to be used for JMeter Container karthiksurabathula/jmeter, the same image must be configured in kubernetes config from step 4.

            stage('Pull Jmeter Image from registery') {
steps {
    sh 'docker pull karthiksurabathula/jmeter'
}
}

Build Config Options - JMeter Script and Number of Nodes for Slaves

stage('Build Kubernetese Config') { steps { sh 'mkdir -p `pwd`/kubernetes-init/data/;cp `pwd`/data/script.jmx `pwd`/kubernetes-init/data/script.jmx;' sh 'cd `pwd`/kubernetes-init/;chmod 775 create.sh;./create.sh 2 script.jmx' }     }

In below I am copying script.jmx from git data folder to kubernetes-init/data folder as I configured JMeter pods to use it as persistence, you can add multiple files or copy all the files for JMeter test from git data folder to kubernetes-init\data folder.

cp `pwd`/data/script.jmx `pwd`/kubernetes-init/data/script.jmx;

In below step we are configuring number of slaves to be created for JMeter, I am creating "2" indicates two slaves and by one Master node will be created and JMeter script to be used is script.jmx. You can change script name to your preferred name

./create.sh 2 script.jmx'

Note: If script name is not specified JMeter container will be looking for script.jmx as default script as it is configured as part of launch.sh in step where we create docker container for JMeter.

Kubernetes Slave Instances Deployment Config:

Below stage starts JMeter slaves, number of slaves should be same as in earlier config above.

stage('Deploying Jmeter Slave Pods') {
steps {
sh 'cd `pwd`/kubernetes-init/config/; ls; i=1;while [ "$i" -le 2 ]; do  microk8s kubectl apply -f slave$i.yaml; i=$(( i + 1 )); done'
}
}