Microservices with Docker Swarm and Consul – Part 1

In this post, I will show you step by step install Node Gateway and Node Agent One.  After finishing your installation, you will understand the way how to Consul, Swarm, Consul-Template, and Registrator work together.


  • Three Nodes at address:
    • named Node Gateway
    • named Node Agent One
    • named Node Agent Two
  • Three Nodes with configuration Ram 2G, 2 CPUs are installed Ubuntu 14.04
  • Node Agent One and Node Agent Two are installed Docker and Docker Swarm
  • Three Nodes connected each others
Microservice with Docker Swarm and Consul

Microservice with Docker Swarm and Consul

Node Gateway

As my scenario, we will run consul, consul-template and Nginx on this node.

For downloading and installing Consul, Consul-Template on Ubuntu 14.04, please see the link: Install Consul and Consul Template in Ubuntu 14.04

And it is easy to install Nginx:

Now, you are ready to run Consul and Consul-Template in Node Gateway

Run Consul as a master node:

Now, we create a template file for Consul-Template at /opt/consul-template directory

And create file /opt/consul-template/nginx.ctmpl with the content:

Next step, run Consul-Template:

The guide above, I just show you the way to run Consul, Consul-Template. In fact, we should run them in background and on startup. To do that please see link: Setup Consul, Consul-Template run in background

To verify Consul, you should access to see Consul Web Interface

Now, you are done with Node Gateway. Do the next step with Node Agent-One

Node Agent-One

With Node Agent-One, we need to run Docker, Swarm Joiner, Swarm Manager, Consul Agent, Registrator, (cAdvisor for monitor as needed)

At the first, We install Consul on this node (It is same as you do with Node Gateway). After that, we run Consul Agent with command:

Next step, we need to restart Docker Daemon to run on address tcp:// So we run command:

After running Docker Daamon, we should run Registrator.

And you should run cAdvisor to monitor this node:

Now, It is time to run Swarm Manager and Swarm Joiner (Node). But first of all, we need to have a token for Swarm Cluster, you can get it by command:

So we can run Swarm Joiner:

And run Swarm Manager:

Swarm is ready to use for now.  If you need to run Docker Swarm in background, you should read this post: Run Docker Swarm with Upstart

And finally, I will run a service on this node. Try command:

Now you can take a rest and test your work by access link:

clotify_microservice (6)

Nginx, Consul, Consul-Template, Docker Swarm

In the next post, I will show you how to scale a service by using Docker Swarm, Nginx and Consul