In the previous part, I showed you step by step install Node Gateway and Node Agent-One. In this post, we will continue to install Node Agent-Two. After finishing this step, you will understand how to scale or replicate you service in Docker Swarm, and how to Nginx load-balance your services with Consul and Consul-Template.
Node Agent-Two
In this node, we also need to install Consul and Swarm. Similar with the last post, we run Consul with command:
1 2 3 4 |
consul agent -data-dir /tmp/consul -node=agent-two \ -bind=172.20.20.12 -client=0.0.0.0 \ -config-dir /etc/consul.d \ -retry-join 172.20.20.10 |
And run Registrator:
1 2 3 4 5 6 |
docker run -d \ --name=registrator \ --net=host \ --volume=/var/run/docker.sock:/tmp/docker.sock \ gliderlabs/registrator:latest \ consul://127.0.0.1:8500 |
Restart Docker Daemon:
1 |
docker daemon -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock |
And run Swarm node to join Swarm Cluster:
1 2 3 |
docker run -d --name swarm_joiner swarm join \ --addr=172.20.20.12:2375 \ token://acdb9dfa3ea6da0b0cfb2c819385fcd3 |
Now, the node is ready for running you microservices:
1 2 |
docker -H tcp://172.20.20.11:12375 run -d -p 80 --name angular_admin_seed_2 \ thanhson1085/angular-admin-seed |
After command above, you are scale your services from one container to two containers running in Docker Swarm
You can check with consul members command:
1 2 3 4 5 |
consul members Node Address Status Type Build Protocol DC agent-one 172.20.20.11:8301 alive client 0.6.0 2 dc1 agent-two 172.20.20.12:8301 alive client 0.6.0 2 dc1 gateway 172.20.20.10:8301 alive server 0.6.0 2 dc1 |
Scale
To scale your service (in my case is angular-admin-seed service), you should access either Node Agent-One or Agent-Two with root privilege, and type commands:
1 2 |
export DOCKER_HOST=tcp://172.20.20.11:12375 docker run -d -p 80 thanhson1085/angular-admin-seed |
And check with docker ps command, the output should be:
1 2 3 4 5 6 7 8 9 10 11 |
~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fd161676367e thanhson1085/angular-admin-seed "/usr/bin/supervisord" 23 seconds ago Up 22 seconds 172.20.20.11:32768->80/tcp agent-one/jovial_bartik 2428787296d9 thanhson1085/angular-admin-seed "/usr/bin/supervisord" 2 minutes ago Up 2 minutes 172.20.20.12:32772->80/tcp agent-two/angular_admin_seed_2 3c264997c80a thanhson1085/angular-admin-seed "/usr/bin/supervisord" 6 minutes ago Up 6 minutes 172.20.20.12:32771->80/tcp agent-two/angular_admin_seed_1 50d11defbad1 gliderlabs/registrator:latest "/bin/registrator con" 12 minutes ago Up 12 minutes agent-one/registrator 4d2a57f1ea0e google/cadvisor:latest "/usr/bin/cadvisor" 30 minutes ago Up 30 minutes 172.20.20.12:8080->8080/tcp agent-two/cadvisor 2b50c7bdf8a7 gliderlabs/registrator:latest "/bin/registrator con" 30 minutes ago Up 30 minutes agent-two/registrator 2a33ca0d1f03 google/cadvisor:latest "/usr/bin/cadvisor" 31 minutes ago Up 31 minutes 172.20.20.11:8080->8080/tcp agent-one/cadvisor fcbfc9b9de02 gliderlabs/registrator:latest "/bin/registrator con" 10 hours ago Up 10 hours trusty-64/registrator 4a89f9f021bc google/cadvisor:latest "/usr/bin/cadvisor" 10 hours ago Up 10 hours 192.168.1.191:8080->8080/tcp trusty-64/cadvisor |
And check /etc/nginx/sites-available/default at node API Gateway:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
upstream angular-admin-seed { server 172.20.20.11:32768; server 172.20.20.12:32772; server 172.20.20.12:32771; } server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; server_name localhost; location / { proxy_pass http://angular-admin-seed/; } } |
It means that Nginx is loading balance between three running services.
Conclusions
After reading, the series of post, we know that there are many open sources support us implement Microservices. However, we can not own a microservices system with one step at this time. We need to deeply understand about tools, open source and Linux as well to implement it.