Balancing Containers

Ray Kahn
3 min readMay 19, 2018

--

As you recall I have been working with manager1 when creating and deploying my micro-services, except when it came to mongoDB (that service has been deployed to all the virtual hosts: manager1, worker1 and worker2). As a result we have ended up with the following:

Not what we want

This is not exactly what I had in mind when I started developing my micro-services. I was looking for something akin to this:

Well balanced

So what’s happening?

We ever really ran our start-service.sh within the manager1 container. To effectively deploy all our services we will need to rewrite our start-service and stop-service for each micro-service so that a service is deployed to all of the containers. And that’s not very hard at all:

#!/usr/bin/env bash 
for server in manager1 worker1 worker2
do
eval `docker-machine env $server`
echo $(env | grep DOCKER)
docker rm -f api-gateway
docker rmi api-gateway
docker image prune
docker volume prune
docker build -t api-gateway .
docker run --name api-gateway -v /Users/Cramirez/.docker/machine/machines/$server:/certs --net='host' --env-file env -d api-gateway
done

As you can see I have combined both start-service.sh and stop-service into a single script and use bash scripting to deploy a micro-service to my virtual hosts. Few things to pint out:

eval `docker-machine env $server`

The above line changes my environment so that the rest of the bash commands take place within the given virtual host ($server).

docker rm -f api-gateway        
docker rmi api-gateway
docker image prune
docker volume prune

I am simply cleaning up, aka stop-service.

docker build -t api-gateway .

Use Dockerfile of the micro-service to create the image.

docker run --name {name of service} ...

And finally start the service.

#!/usr/bin/env bashfor server in manager1 worker1 worker2doeval `docker-machine env $server`echo $(env | grep DOCKER)ip=$(docker-machine ip $server)echo 'server ip >>>> '$ipdocker rm -f doctors-servicedocker rmi doctors-servicedocker image pruneocker volume prunedocker build -t doctors-service .docker run -l=msRoute='/provider' --add-host manager1:192.168.99.100 --name doctors-service -p 3000:3000 -d doctors-servicedone

An the above is my re-write of the start-script.sh for doctors-service. After running it you should check both ps and logs to ensure the service is running. I’ll need to do the same fo all the other services.

And we are done…

What’s Next?

I will tackle bottleneck issue with my setup as my api gateway is woefully slow and I am not sure where the problem lies.

What we have done so far

--

--