readme.fr

Hot opensource news

Docker is also cool for Ops !

As we know docker is for Build, Ship, Run applications. Usually developers use docker to dev new app and host them with applications like OpenShift.

docker4

You can find a lot of article about develop and build application in docker containers. I decided to write a small article on an other aspect of docker that I often use.

Application testing for Ops purpose !

I’m used to do that kind of tests with containers like OpenVz but I recently try to do that with docker.

The last time I use this, it was for the Rabbitmq split brain article. I needed to spawn a rabbitmq cluster, break it, test things and do it again in others versions.

I would like to share with you, how I used docker for this and also a script to simplify the workflow.

What I needed :

  • Several VM with network
  • Preinstalled packages
  • Run startup script to setup the rabbit cluster
  • Access to node to do some tests
  • Reset all to do it again

My workflow was :

  1. Get a basic image
  2. Create a container with it to install required packages
  3. Save this container as an image
  4. Write a shared bash script to initiate the rabbitmq cluster
  5. Start a bunch of containers with my image
  6. And then execute the init script on each containers

Generate my image from a base image

Start new docker container, in my case a debian one. Install and configure all you will need after in the image. For me it was just setup package like rabbitmq.

docker run --name sample -it debian /bin/bash
root@84896fac0a7d:/# apt-get install git
docker run

When you finish that, exit your docker container and commit your modification to generate a base image (baseimage).

# Display docker container ID
docker ps -a | grep sample
84896fac0a7d debian "/bin/bash" 5 minutes ago Exited (0) 2 minutes ago sample

# Commit container
docker commit 84896fac0a7d baseimage:01
docker commit

Now you can find this image in your docker images list.

docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
baseimage 01 2a76cbf5b6bf 10 seconds ago 125.1 MB
docker images

Create my first boot script

All packages I required are present in my base image. Now I write a script myinit_script.sh to initialize and start the app I want to test (rabbitmq cluster for me).

#!/bin/sh

... do something.

cp /opt/rabbitmq.config /etc/rabbitmq/
/etc/init.d/rabbitmq-server start
myinit_script.sh

In fact it was just a simple script to apply rabbitmq config and start the cluster.

Start a bunch of containers and do the job

docker3

Start to play with docker_setup.py and create all your containers.

./docker_setup.py  --host-volume /.../foo:/opt/ --create --image baseimage:01
New stack status:
# | Id | Name | Status | Image | VolumesFrom | Binds |
# | 2a7f0c... | node4 | Up 1 second | debian | | /.../foo:/opt/:rw |
# | 66aaf2... | node3 | Up 1 second | debian | | /.../foo:/opt/:rw |
# | 7695f4... | node2 | Up 1 second | debian | | /.../foo:/opt/:rw |
# | 64f097... | node1 | Up 1 second | debian | | /.../foo:/opt/:rw |
docker_setup.py --create

 

The script will create nodes, update /etc/hosts of each containers to be able to use hostname of each containers and mount the /…/foo direcfory from the host in /opt on each containers.

docker_nodes

You can access to each containers and ping them. It’s time to test your app :

docker attach node1
root@node1:/#
root@node1:/# ping node2
PING node2 (172.17.0.10): 56 data bytes
docker attach

For the rabbitmq article, my rabbitmq cluster at this state was up and running. It was the moment to create a split brain and try to repair the cluster.

# Example of commande you can use

for i in $(seq 1 4); do docker exec node$i pkill -9 -f beam; done
docker exec

 

That it, with that you have all informations to test a new app. if you want to reset the stack just call again the script with –create. You also can commit modifications inside a container and then recreate the stack from this new commit.

When you finish to test your app, simply do a cleanup to remove the container stack.

./docker_setup.py --cleanup
docker_setup.py --cleanup

 

Additionnals informations about the script

To give you more informations about this script, it don’t do magic stuff. It’s a very small script who use the docker-py module.

import docker.client
import docker.client

 

Main content of the script is call of docker.client class methods and override some of them to force default values. Also provide commands like creates to call multiple time create with a list of containers.

class Docker(docker.Client):
    """Override of docker client.
       Usage :
           cli = Docker(base_url='unix://var/run/docker.sock')"""

    def create_container(self, *args, **kwargs):
        "Override create_container to force default value"
        kwargs['tty'] = True
        kwargs['stdin_open'] = True
        kwargs['hostname'] = kwargs.get('name')
        super(Docker, self).create_container(*args, **kwargs)

    def create_containers(self, names, *args, **kwargs):
        "Call create multiple times"
        for name in names:
            kwargs['name'] = name
            self.create_container(*args, **kwargs)
docker_setup.py sample

 

I let you read the script to know more about. I tried to add a lot of comments.

To discover others feature like share directory go to the readme in github.

gaelL
gaelL on GithubgaelL on LinkedingaelL on Wordpress

, , ,

Leave a Reply

Your email address will not be published. Required fields are marked *