Run a defined workload on a Docker or Kubernetes environment

Sometimes you want to stress test a container environment or you are measuring CPU and memory in such an environment and you want to test, of your metrics reporting is correct.

Introducing docker-stress

This small tool allows to gauge metrics reporting using the  simple command line utility “stress” inside a docker container.

The needed Dockerfile can be found in this repository:

https://github.com/iboernig/docker-stress/

One can use this tool on a local container host or on a Kubernetes cluster. I will show podman for local deployment and OpenShift for the cluster deployment here.

Local usage with podman (or docker if you wish)

For the local deployment i will use “podman” here. As a proud Fedora user, I use the native and easiest tool that can be used by a normal user with no special rights to work with containers locally. If you need more information to podman have a look here: https://podman.io/.

If you prefer docker to podman, feel free to use docker. the command line is exactly the same.

To start, clone the repository:

[iboernig@t470 Projects]$ git clone https://github.com/iboernig/docker-stress.git
[iboernig@t470 Projects]$ cd docker-stress/

Build locally using podman (you can use “docker” with the same arguments, if you do not have podman)

[iboernig@t470 Projects]$ podman build -t stress .
STEP 1: FROM fedora:latest
STEP 2: RUN yum -y install stress procps-ng 
--> Using cache 8f2c95619aeb7799ae9806fd94d83605f894a458d344659910240092ed7efc4d
STEP 3: FROM 8f2c95619aeb7799ae9806fd94d83605f894a458d344659910240092ed7efc4d
^[[OSTEP 4: ENV CPU_LOAD=1 MEM_LOAD=1 MEM_SIZE=256M
--> Using cache 401365dae085f9a53d39e2eb6a2a14e6b21129d0e19d9f75a16503fc780b2bd5
STEP 5: FROM 401365dae085f9a53d39e2eb6a2a14e6b21129d0e19d9f75a16503fc780b2bd5
STEP 6: CMD stress --cpu $CPU_LOAD --vm $MEM_LOAD --vm-bytes $MEM_SIZE
--> Using cache 8f71320d0ecad024efcbf98be0504eaf3354c08e6469e76a028c6f3b55e0bc95
STEP 7: COMMIT

and then run it:

[iboernig@t470 docker-stress]$ podman run -it -rm stress 
stress: info: [1] dispatching hogs: 1 cpu, 0 io, 1 vm, 0 hdd

You can change the parameters by setting the enviroment variables:

[iboernig@t470 docker-stress]$ podman run -it -rm -e CPU_LOAD=2 -e MEM_LOAD=1 -e MEM_SIZE=512M stress
stress: info: [1] dispatching hogs: 2 cpu, 0 io, 1 vm, 0 hdd

Note that each number of CPU_LOAD and each number of MEM_LOAD allocates one thread. The memory in the variable MEM_SIZE is the size one MEM_LOAD thread consumes. So if you use more than one, you will consume a multiple of MEM_SIZE memory. For more infomation see “man stress”.

Cluster deployment in OpenShift

In OpenShift you only have to define a new app based on this repository:

oc new-app https://github.com/iboernig/docker-stress.git

OpenShift then pulls the repository, starts a docker build and deploys the image in one go.

If you need to tune the parameters you can do so by adding the environment variable to the deplyoment config. Either in the webconsole or by changing the deploymentconfig manually:

oc set env dc/docker-stress CPU_LOAD=2 MEM_LOAD=1 MEM_SIZE=512M

Happy stressing!