Philippe
Philippe

Reputation: 1366

How to setup Docker Swarm for an existing Docker Compose file intended to run in 3 different hosts?

I'm trying to figure out the best solution for my (maybe simple) problem.

I have a (one) docker compose file with some services :

The rest api needs to be scalable. Java-1, Java-2, Java-3, etc.

What you see below are 3 different hosts. What's the best solution to script everything when all my hosts are up? I want to be able to do something like docker-compose up -d and spawn my services on 3 differents hosts.

I know docker swarm can do something. I've also read about Weave network with combination with Swarm. Honestly, I'm having a hard time to put everything toghether; understanding how to make it work basically...

The Java Hosts are to be load balanced (of course).

Will my Host 1 be my swarm manager? Host 2 and 3 workers? How can I manage this.?

Do you have any recommendations? Rancher, Portainer, Docker-Machine, other...?

     Host 2
+-------------+                               
|             |                               
|   Java 1    |               Host 1       
|             |---\      +-------------------+
+-------------+    ---\  |                   |
                       --|   Mongo, Redis    |
    Host 3             --|                   |
+-------------+     --/  +-------------------+
|             |  --/                          
|   Java 2    |-/                             
|             |                               
+-------------+                               

    Host #
    ...

EDIT : links between my RestApi services and the database need to be encrypted.

Upvotes: 0

Views: 818

Answers (1)

Ravindu Nirmal Fernando
Ravindu Nirmal Fernando

Reputation: 4822

As you have mentioned Docker Swarm can be used to solve your problem. Initially what you should understand is your Java (API), Mongo and Redis should not be identified as hosts. They would be docker services in the swarm mode. So in your case, you would have three services in your docker swarm. Scaling it on your hosts is a job that's being done with Docker Swarm. As you have mentioned your Java (API) should be scaled, as a starting point imagine you would start with 3 Java (API) services, so imagine you have three host machines in your Docker Swarm, one is a manager other two are workers (deciding which host becomes manager which hosts are workers is up to you) and you just need to create three replicas of Java (API) services and Docker Swarm will run those three replicas on those three hosts, even if one host fails Docker Swarm will automatically redirect the traffic to hosts which are running the containers and will always run the number of replicas that you have told it to run. Which means if a container fails it will be recreated. These services can be easily scaled with simple command based on your purpose. So a good starting point would be to read about Docker Swarm Official documentation itself. Then follow the official swarm tutorial.

In Docker swarm mode, there is a feature called a stack, a stack would enable you to simply deploy a complete application stack to the swarm. You can read about it from here. In your case, as you have mentioned you already have the compose file with you, which can be used to deploy a stack. You would need to update your existing compose file to match with the configurations you need to have in your application stack. It's just a matter of mentioning what do you want and how you want it in the docker-compose.yml file and Docker Swarm would create it for you.

After you have read about those concepts and ready with Docker Swarm setup and hosts with all the Workers and Managers set-up follow this awesome Docker Swarm vote app example. Read the Docker Stack file of that example which will allow you to understand how to properly define your application stack which will provide you a solid understanding about Docker Stacks and use the same example file as a base for designing your application stack.

After that, it's a matter of deploying your stack. Simply by running docker stack deploy -c docker-compose.yml yourAppName.

Adding another point into all this is you can create a separate overlay type network in Docker specifically into your app. This would allow a distributed network among multiple Docker daemon hosts (nodes).

Edit - All swarm service management traffic is encrypted by default, using the AES algorithm in GCM mode. You can encrypt application data as well by add --opt encrypted when creating the overlay network. As the documentation mentions, this will carry a non-negligible performance penalty, so testing is expected before running the feature in production. Refer 6 Encrypt traffic on an overlay network section.

Hope this explanation would help you understand how Docker Swarm would solve your problem and clarify the issues you are facing.

Upvotes: 2

Related Questions