How to Install and Use Gremlin with Docker on Ubuntu 16.04

Last Updated March 15th, 2018

Introduction

Gremlin is a simple, safe and secure way to use Chaos Engineering to improve system resilience. You can use Gremlin with Docker in a variety of ways. It is possible to attack Docker containers and it is also possible to run Gremlin in a container to create attacks against the host or other containers.

This tutorial will provide a walkthrough of the following:

  • How to install Docker
  • How to create a htop container to monitor the host and containers
  • How to create an Nginx Docker container to attack using Gremlin
  • How to install Gremlin on the host
  • How to create a CPU Attack from the host against an nginx Docker container using the Gremlin Control Panel

To run Gremlin in a Docker container, view the guide on How to Install and Use Gremlin in a Docker Container.

Prerequisites

Before you begin this tutorial, you’ll need the following:

  • An Ubuntu 16.04 server
  • A Gremlin account
  • The apt-transport-https package to be able to install gremlin from our repo via HTTPS.

Step 1 - Installing Docker

In this step, you’ll install Docker.

Add Docker’s official GPG key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Use the following command to set up the stable repository.

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Update the apt package index:

sudo apt-get update

Make sure you are about to install from the Docker repo instead of the default Ubuntu 16.04 repo:

apt-cache policy docker-ce

Install the latest version of Docker CE:

sudo apt-get install docker-ce

Docker should now be installed, the daemon started, and the process enabled to start on boot. Check that it’s running:

sudo systemctl status docker

Make sure you are in the Docker usergroup, replace $USER with your username:

sudo usermod -aG docker $USER

Log out and back in for your permissions to take effect, or type the following:

su - ${USER}

Step 2 - Create an htop container for monitoring

htop is an interactive process viewer for unix.

Create the docker file:

vim Dockerfile

Add the following to the Dockerfile:

FROM alpine:latest
RUN apk add --update htop && rm -rf /var/cache/apk/*
ENTRYPOINT ["htop"]

Build the Dockerfile and tag the image:

docker build -t htop .

Run htop inside a container:

docker run -it --rm --pid=host htop

To exit htop, use the q key.

Next we will create an nginx container and monitor the new container directly by joining the nginx container’s pid namespace.

Step 3 - Create an nginx Docker container to be used for Gremlin Attacks

First we will create a directory for the html page we will serve using nginx:

mkdir -p ~/docker-nginx/html
cd ~/docker-nginx/html

Create a simple html page:

vim index.html

Paste in the content shown below:

<html>
    <head>
        <title>Docker nginx tutorial</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    </head>
    <body>
        <div class="container">
            <h1>Hello this is your container speaking.</h1>
            <p>This page was created by your Docker container.</p>
            <p>Now it’s time to create a Gremlin attack.</p>
        </div>
    </body>
</html>

Create a container using the nginx Docker image:

sudo docker run -l service=nginx --name docker-nginx -p 80:80 -d -v ~/docker-nginx/html:/usr/share/nginx/html nginx

View the nginx Docker container

sudo docker ps -a

You will see the following:

CONTAINER ID        IMAGE               COMMAND                       CREATED                 STATUS                   PORTS                         NAMES
352609a67e95           nginx               "nginx -g 'daemon of…"   33 seconds ago      Up 32 seconds       0.0.0.0:80->80/tcp   docker-nginx

Step 4 - Use an htop container to monitor an nginx Docker container

htop can be used to monitor Gremlin attacks against the host and Gremlin attacks against individual containers.

Join the docker-nginx container’s pid namespace:

docker run -it --rm --pid=container:docker-nginx htop

Before the attack, htop will show you that CPU is not spiking:

  1  [|                                                               0.7%]   Tasks: 3, 0 thr; 1 running
  2  [||                                                              1.3%]   Load average: 0.07 0.05 0.06
  Mem[||||||||||||||||||||||||||                                141M/3.86G]   Uptime: 02:48:43
  Swp[                                                               0K/0K]

  PID USER      PRI  NI    VIRT   RES   SHR   S   CPU%   MEM%   TIME+   Command
   10 root      20    0    4324   1708  936   R   0.0    0.0    0:00.05  htop
    1 root      20    0    32428  5080  4400  S   0.0    0.1    0:00.03  nginx: master process nginx -g daemon off;
    5 101       20   0     32900  3060  1824  S   0.0    0.1    0:00.00  nginx: worker process

Next we are going to install Gremlin on the host to perform attacks.

Step 5 - Installing Gremlin on the Host

It is possible to install Gremlin on the host or in a Docker container. If you would prefer to install Gremlin in a Docker container view the guide, How to Install and Use Gremlin in a Docker Container on Ubuntu 16.04.

In this step, you’ll install Gremlin on the host.

Add the gremlin repo:

echo "deb https://deb.gremlin.com/ release non-free" | sudo tee /etc/apt/sources.list.d/gremlin.list

Import the GPG key:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C81FC2F43A48B25808F9583BDFF170F324D41134 9CDB294B29A5B1E2E00C24C022E8EF3461A50EF6

Then install the Gremlin client and daemon:

sudo apt-get update && sudo apt-get install -y gremlin gremlind

Step 6 - Validating Installation

Run the following command to confirm you have all the necessary components installed on your host for Gremlin to function correctly:

gremlin syscheck

You’ll see the output from the Gremlin checks in your console:

Checking resource gremlins ...
Checking CPU gremlin ...
Attack on cpu_1 completed successfully
CPU gremlin OK

Step 7 - Registering with Gremlin

Gremlin clients can be tagged with custom key-value pairs, these tags can then be used by the Gremlin control plane to select multiple clients with the same tags for an attack.

Start by initializing Gremlin and assigning tags with the following command. Substitute your desired tags for tag_name1=tag_value1 and tag_name2=tag_value2:

gremlin init
    --tag tag_name1=tag_value1 \
    --tag tag_name2=tag_value2 \

This will create a new Gremlin client with your tags.

Example: Adding tags to a Gremlin client

Suppose that you have a collection of hosts and you want to tag them by their service, service-version and service-type.

Initialize Gremlin and assigning tags with the following command:

gremlin init \
    --tag service=traffic \
    --tag service-version=1.0.0

This will set tags as service=traffic and service-version=1.0.0.
If you are using an AWS EC2 instance, Gremlin will also auto-populate tags for region and zone.

Registering Gremlin with your Team ID and Team Secret

You’ll then be prompted to input your Team Id and Team Secret. You can obtain these credentials from Gremlin Settings in the Gremlin Control Panel.

Login to the Gremlin Control Panel using your Company name and sign-on credentials. These details were emailed to you when you signed up to start using Gremlin.

home

Next click on your name and select Settings in the Gremlin Control Panel.

settings

You will find your Team ID on the left under your team name, then click to generate your Team Secret.

Gremlin does not keep a copy of your secret. If you lose it, you will need to reset it from the settings page

secret

On your computer, open your terminal and paste your Team ID and then Team Secret:

Please input your Team ID:

Please input your Team Secret:

You are now ready to create attacks using the Gremlin Control Panel.

Step 8 - Creating attacks using the Gremlin Control Panel

Example: Creating a CPU Attack from the host against the nginx Docker container using the Gremlin Control Panel

You can use the Gremlin Control Panel or the Gremlin API to trigger Gremlin attacks. You can view the available range of Gremlin Attacks in Gremlin Help.

The “Hello World” of Chaos Engineering is the CPU Resource Attack. To create a CPU Resource Attack select “Resource” and then “CPU” in the dropdown menu.

newcpu

The CPU Resource Attack will consume CPU resources based on the settings you select. The most popular default settings for a CPU Resource Attack are pre-selected, a default attack will utilize 1 core for 60 seconds. Before you can run the Gremlin attack you will need to click either Exact hosts to run the attack on or click the Random attack option.

Click Exact and select the host you created the nginx Docker container on, in this example that is 138.68.226.195.

container

Example: Using Container Labels to Attack Specific Containers

Container labels will enable you to choose containers on your host to attack.

Click to enable container labels, type in the label details of the container.

labels

For this example, the Nginx Docker container label we created is set to service=nginx.

sudo docker run -l service=nginx --name docker-nginx -p 80:80 -d -v ~/docker-nginx/html:/usr/share/nginx/html nginx

Finally select ”Create” to kick off a random Gremlin CPU Resource Attack on the nginx Docker container.

createattack

Your attack will begin to run, you will be able to view its progress via Gremlin Attacks in the Gremlin Control Panel.

activeattack

To view the results of the attack join the docker-nginx container’s pid namespace:

docker run -it --rm --pid=container:docker-nginx htop

You will see the following in htop:

  1  [                                                      0.0%]   Tasks: 4, 1 thr; 2 running
  2  [||||||||||||||||||||||||||||||||||||||||||||||||||||100.0%]   Load average: 0.61 0.17 0.06 
  Mem[||||||||||||||||||||||                          176M/3.86G]   Uptime: 00:37:17
  Swp[                                                     0K/0K]
  PID USER      PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command          
   18 root       20   0 15456 13692  4112 S 100.  0.3  0:26.39 gremlin attack cpu -c 1 -l 60
   13 root       20   0  4324  1988   944 R  0.0  0.0  0:00.12 htop
    1 root       20   0 32428  5184  4504 S  0.0  0.1  0:00.04 nginx: master process nginx -g daemon off;
    8 101        20   0 32900  2948  1712 S  0.0  0.1  0:00.00 nginx: worker process

If you have the Gremlin Slackbot enabled you will also see the bot post that the Gremlin Attack has started. When it’s successful the Gremlin Slackbot will post again. To setup the Gremlin Slackbot follow the guide, How to Setup and Use the Gremlin Slackbot.

slackcpu

When your attack is finished it will move to Completed Attacks in the Gremlin Control Panel.

completedattack

To view the logs of the Attack, click on the Attack in Completed Attacks then click to the arrow to view the logs.

attacklogs

Conclusion

You’ve installed Gremlin on a server running Docker and validated that Gremlin works by running the “Hello World” of Chaos Engineering for Docker Containers, the CPU Resource attack. You now possess tools that make it possible for you to explore additional Gremlin Attacks.

Gremlin’s Developer Guide is a great resource and reference for using Gremlin to do Chaos Engineering. You can also explore the Gremlin Community for more information on how to use Chaos Engineering with your infrastructure.