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.
Prerequisites
- Three Nodes at address:
- 172.20.20.10 named Node Gateway
- 172.20.20.11 named Node Agent One
- 172.20.20.12 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
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:
|
sudo apt-get install nginx |
Now, you are ready to run Consul and Consul-Template in Node Gateway
Run Consul as a master node:
|
consul agent -server -bootstrap-expect 1 \ -data-dir /tmp/consul -node=gateway -bind=172.20.20.10 -client=0.0.0.0 \ -config-dir /etc/consul.d -ui-dir /opt/consul/ |
Now, we create a template file for Consul-Template at /opt/consul-template directory
|
mkdir -p /opt/consul-template cd /opt/consul-template |
And create file /opt/consul-template/nginx.ctmpl with the content:
|
upstream angular-admin-seed { {{range service "angular-admin-seed"}} server {{.NodeAddress}}:{{.Port}};{{end}} } server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; server_name localhost; location / { proxy_pass http://angular-admin-seed/; } } |
Next step, run Consul-Template:
|
consul-template \ -consul 127.0.0.1:8500 \ -template "/opt/consul-template/nginx.ctmpl:/etc/nginx/sites-available/default:service nginx reload" \ -retry 30s |
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 http://172.20.20.10:8500 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:
|
consul agent -data-dir /tmp/consul -node=agent-one \ -bind=172.20.20.11 -client=0.0.0.0 \ -config-dir /etc/consul.d \ -retry-join 172.20.20.10 |
Next step, we need to restart Docker Daemon to run on address tcp://0.0.0.0:2375. So we run command:
|
docker daemon -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock |
After running Docker Daamon, we should run Registrator.
|
docker run -d \ --name=registrator \ --net=host \ --volume=/var/run/docker.sock:/tmp/docker.sock \ gliderlabs/registrator:latest \ consul://172.20.20.11:8500 |
And you should run cAdvisor to monitor this node:
|
docker run --volume=/:/rootfs:ro \ --volume=/var/run:/var/run:rw --volume=/sys:/sys:ro \ --volume=/var/lib/docker/:/var/lib/docker:ro \ --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro \ --publish=8080:8080 \ --detach=true --name=cadvisor google/cadvisor:latest |
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:
|
$ docker run --rm swarm create acdb9dfa3ea6da0b0cfb2c819385fcd3 # keep it, we will use this token for our scenario |
So we can run Swarm Joiner:
|
$ docker run -d swarm join --addr=172.20.20.11:2375 token://acdb9dfa3ea6da0b0cfb2c819385fcd3 |
And run Swarm Manager:
|
$ docker run -d -p 12375:2375 swarm manage token://acdb9dfa3ea6da0b0cfb2c819385fcd3 |
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:
|
export DOCKER_HOST=tcp://172.20.20.11:12375 docker pull thanhson1085/angular-admin-seed docker run -d -p 80 --name angular_admin_seed_1 \ thanhson1085/angular-admin-seed |
Now you can take a rest and test your work by access link: http://172.20.20.10

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