/k6-influxdb-grafana

We will use k6 as loadtesting tool, influxdb as time series database and grafana for visualization

Primary LanguageShell

k6-influxdb-grafana

We will use k6 as loadtesting tool, influxdb as time series database and grafana for visualization

A. Local Setup

1) Install K6 (Debian/Ubuntu)

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 379CE192D401AB61
$ echo "deb https://dl.bintray.com/loadimpact/deb stable main" | sudo tee -a /etc/apt/sources.list
$ sudo apt-get update
$ sudo apt-get install k6

Verify Installation

$ k6 version

k6 v0.28.0 (2020-09-24T14:33:59+0000/v0.28.0-0-gdee9c4ce, go1.14.9, linux/amd64)

2) Run Loadtest script

Now to run a simple loadtest script, run the following command:

$ k6 run script.js

To run with specfic virtual users for certain duration, run the following command:

$ k6 --vus 10 --duration 30s simple-script.js

3) Install InfluxDB(For Metrics Storage)

$ sudo apt install influxdb

Verify Installation

Start the influxdb.service if not started by default:

$ sudo service influxdb start

Check the status, if it's enabled or not:

$ sudo service influxd status
● influxdb.service - InfluxDB is an open-source, distributed, time series database
   Loaded: loaded (/lib/systemd/system/influxdb.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2020-11-01 17:16:41 +0545; 5h 6min ago
     Docs: man:influxd(1)
 Main PID: 1024 (influxd)
    Tasks: 19
   Memory: 28.1M
      CPU: 27.821s
   CGroup: /system.slice/influxdb.service
           └─1024 /usr/bin/influxd -config /etc/influxdb/influxdb.conf

To stop the influxd, run command:

$ sudo service influxd stop

Access InfluxDB

InfluxDB server runs on localhost, listening on port 8086.It can be access at http://localhost:8086.

To access influxDB database, use http://localhost:8086/database-name.

4) Install grafana(For Visualization)

# To install OSS release
$ sudo apt-get install -y apt-transport-https
$ sudo apt-get install -y software-properties-common wget
$ wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -

# Add this repository for stable releases
$ echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
$ sudo apt-get update
$ sudo apt-get install grafana

Verify Installation

Start the grafana.service

$ sudo service grafana-server start

Check the status, if its enabled or not

$ sudo service grafana-server status

● grafana.service - Starts and stops a single grafana instance on this system
   Loaded: loaded (/lib/systemd/system/grafana.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2020-11-01 17:16:47 +0545; 4h 57min ago
     Docs: http://docs.grafana.org
 Main PID: 1961 (grafana)
    Tasks: 18
   Memory: 6.3M
      CPU: 1.514s
   CGroup: /system.slice/grafana.service
           └─1961 /usr/sbin/grafana --config=/etc/grafana/grafana.ini cfg:default.paths.logs=/var/log/grafana cfg:default.paths.data=/var/lib/grafana

To stop the grafana.service, run command:

$ sudo service grafana-server stop

Access the grafana on browser

Grafana server runs on localhost, listening to port 3000. Grafana dashboard can be accessed on browser at http://localhost:3000.

Use admin or anything you like for userame and password. alt text

- Add Grafana Dashboard

After successfully signing in grafana dashboard, import Grafana dashboard. We will import dashboard with id:2587 for testing.

alt text

- Add InfluxDB as DataSource

Now, configure grafana dashboard to use InfluxDB as datasource. alt text

And, add http://localhost:8086 as influxdb url and myk6db as database name.
alt text

5) Run Loadtest script with Better Visualization

We will run a loadtest script with virtual users, rps and duration.

$ k6 run --out influxdb=http://localhost:8086/myk6db script.js

myk6db is database name, if doesnot exist, k6 will create automatically

The output on grafana-dashboard looks like this: alt text

B. Cloud Setup (Any Vendors)

1) Installation

For cloud environment setup, install three tools mentioned seprately in the cloud instance (ec2, compute engine, etc) just like in Local SetUp.

  • K6
  • Influxdb
  • Grafana

As alternative, you can use script to setup all tools at a time:

$ chmod +x setup.sh        # make the setup script executable
$ ./setup.sh               # run the script

Now, check the status of installed tools and reload if stopped:

$ chmod +x check.sh        # make the check script executable
$ ./check.sh               # run the script

Note: grafana-server daemon often gets shutdown when the computing instance is shutown and needs to be reloaded after starting the instance again.

2) Configuration

All configurations are same like in Local SetUp except for the localhost replaced by static-ip in both influxdb and grafana.

Static-IP is ip address attached to a compute instance that doesnot change, we will use this static-ip to access influxdb and grafana.

Also create and attach a security group with port 8086 and 3000 opened for influxdb and grafana in cloud compute instance.

Access InfluxDB

InfluxDB server now runs on static ip, listening on port 8086.It can be access at http://<static-ip>:8086.

To access influxDB database, use http://<static-ip>:8086/database-name.

Run Loadtest script

We will run a loadtest script with virtual users, rps and duration.

$ k6 run --out influxdb=http://<static-ip>:8086/myk6db script.js

myk6db is database name, if doesnot exist, k6 will create automatically

Access the grafana on browser

Grafana server runs on static-ip, listening to port 3000.

Grafana dashboard can be accessed on browser at http://<static-ip>:3000.