Running K6 Test And And Putting Metrics To Cloudwatch Using Container

Pawan Kumar
5 min readJul 2, 2021

This article is for running k6-test and uploading its metrics to aws cloudwatch using a docker container.

K6-basics

To Install k6 follow this link:- k6 Installation

Suppose we have a simple script saved as script.js

import http from ‘k6/http’;import { sleep } from ‘k6’;export default function () {http.get(‘https://test.k6.io');sleep(1);}

And to run this code run the following command..

k6 run script.js

It will yield output as below..

K6 test output

Here’s the step by step plan to create a image for running k6 and uploading to cloudwatch metrics:

  1. Create a script to run the service of cloudwatch-agent and basic configurations.
  2. Create statsd.json file which include agent details, metrics and logs details.
  3. Create a directory which would contain aws credential(to conect aws), aws config file and all the scripts to test.
  4. Put a sample script in the current directory to test.
  5. Create a centos docker image with k6 and aws cloudwatch agent installed.
  6. Then Will finally see a real use case

1. Script File

mkdir /root/.awstouch /root/.aws/credentialstouch /root/.aws/configcp /tmp/credentials /root/.aws/credentialscp /tmp/config /root/.aws/configcp statsd.json /opt/aws/amazon-cloudwatch-agent/bin/default_linux_config.jsoncp statsd.json /opt/aws/amazon-cloudwatch-agent/etc/statsd.json/opt/aws/amazon-cloudwatch-agent/bin/start-amazon-cloudwatch-agent&sleep 10K6_STATSD_ENABLE_TAGS=true k6 run — out statsd $1sleep 120

2. Statsd.json file

{   “metrics”: {       “namespace”: “name”,       “metrics_collected”: {             “statsd”: {                “service_address”: “:8125”,                “metrics_collection_interval”: 1,                “metrics_aggregation_interval”: 0              }         }    }}

3. Directory

It should contain following file:

a. aws config file

[AmazonCloudWatchAgent]region = region_id

b. aws credential

[AmazonCloudWatchAgent]aws_access_key_id = [your access key]aws_secret_access_key = [your secret key]

c. All the scripts to be run

4. Sample script

It could be anything just to test. I have used the official k6 example.

import http from ‘k6/http’;import { sleep } from ‘k6’;export let options = {vus: 10,duration: ‘30s’,};export default function () {http.get(‘http://test.k6.io');sleep(1);}

5. Dockerfile

FROM centos:latest#To install aws-cloudwatch-agentRUN yum install https://s3.amazonaws.com/amazoncloudwatch-agent/redhat/amd64/latest/amazon-cloudwatch-agent.rpm -y#To setup repo for k6 and install k6RUN yum install https://dl.k6.io/rpm/repo.rpm -yRUN yum install k6 -y#Copying script.jsCOPY script.js .ENV RUN_IN_CONTAINER=”True”#copying start.shADD start.sh .#Making start.sh executableRUN chmod +x start.shENTRYPOINT [ “/bin/bash”, “start.sh” ]CMD [ “script.js” ]

Now that you have created the docker file, build an image and push it to an ECR/Dockerhub repository, here’s how I pushed it to Dockerhub.

docker build command builds your image according to your docker file (dockerfile is the docker file name in the below example), I’ve tagged my image as sparrow59/k6-statsd:v3 in the example below(Here sparrow59 is the username of dockerhub). Don’t forget the . at the end of the docker build command, execute these commands in the same directory where your dockerfile exists.

docker build -t sparrow59/k6-statsd:v3 .

Now that our image is hosted and ready we can use this image to run our k6 test and upload the metrics to cloudwatch. After that you can also push your image to dockerhub using the following command.

docker push sparrow59/k6-statsd:v3

Run the container with this image now with the directory(that we have created) attached as below.

docker run -i -v /path/to/dir:/tmp/ sparrow59/k6-statsd:v3 /tmp/script.js

It will run the test and put the metrics to your aws-cloudwatch.

Real Use Case

So let’s use this image in real use case scenario. Where I m using Devtron(know more about Devtron) tool which is made on kubernetes and made for kubernetes. So scenario is something like when we wants to deploy our app before that we wants to run k6 test so that we have a track of metrics.

Installing Devtron Guide

Start using Devtron Guide

1. Create a new app

2. Git Material

Give the repo in which above configuration have pushed.

3. Docker Build Config

Here we define the repo where the image will be puched after build. It is useful when we are deploying any app.

4. Deployment Template

This is configured when we wants to deploy our app. We just define our variable values like on which port app is running, which type of service we wants to expose etc. For now i’m keeping it default as I don’t wants to deploy any app as of now.

5. Workflow

A. Create CI pipeline

Select New build pipeline then choose Continues Integration

Then choose advance option>>Go to pre build stage and paste the docker run command.

Save it and click update pipeline.

6. Trigger

Now time to trigger the pipeline since we have chosen manual trigger so we manually have to trigger. Else if you want to trigger the pipeline automatically then choose automatic option after that on every new commit it will triggered automatically.

Click on select image.

Then choose the commit with you want to trigger. For latest commit choose the top one.

Click on start build.

Now in build history you can see the logs of running pipeline

It will looks like this when we have a successful build.

After the successful build this image will be available for deployment using deployment pipeline in Devtron. For more detail visit Deployment.

Thank you.

--

--