Docker Swarm Post 1.12
Docker 1.12 integrates the swarm technology into the engine, so things have changed (for the better) since we last deployed a deployed a docker swarm. This tutorial will:
- Deploy a swarm
- Demonstrate how to deploy an application to the swarm
- Demonstrate scaling up the application.
- Demonstrate the load balancing aspect of scaling up the application.
- Remove the web application from the swarm.
Requirements
- 2 or more hosts running Docker 1.12 or higher.
Deploy the Swarm!
Run the following command to initialize a swarm on one of the nodes. This node will become the manager of the swarm.
docker swarm init
You should get some output like below:
To add a worker to this swarm, run the following command:
docker swarm join \
--token SWMTKN-1-4n3s8pxxxwowgqt3w1o2j97gd33v81w3rm0s2zwv0ppvria1f4-5yks0di13zw0uroip7jx159y9 \
192.168.1.75:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
We will now add the other hosts as workers. SSH into the other hosts and run the docker swarm join
command that was just given to you in the previous output.
List the Nodes
Now if you wish to see what nodes are in your swarm to check that it really worked, you can use the following command on any of the managers (in this example there is only 1):
docker node ls
You should get some output similar to:
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
1e1ychn3w7dtib0jfz3kmj89g worker2.programster.org Ready Active
ac4tq3skv33iossei4k0sztxn worker1.programster.org Ready Active
aek2ghu7vkblso1yq4em7t42r * endpoint.programster.org Ready Active Leader
Deploy A Web Application
Now we are going to deploy a web application which will simply output the container ID on the web page.
docker pull jwilder/whoami
docker service create --name whoami -p 80:8000 jwilder/whoami
docker ps
returns nothing.
Scale Up!
Now we are going to scale up that service so that there are 2 instances of it:
docker service scale whoami=2
Test The Load Balancing
Now if you use the following command to load the website several times, which will hit our "whoami" service that prints the container ID, you will notice that the container ID returned changes:
for i in {1..5}; do curl http://[IP of any of the swarm nodes]/; done
I'm 221c4522b37d
I'm 9a466ee04377
I'm 221c4522b37d
I'm 9a466ee04377
I'm 221c4522b37d
Remove The Service
After we are done, and we want to remove the web application from the cluster, we need to run:
docker service rm whoami
Conclusion
Hopefully this has shown you how easy it is to now deploy a swarm and create a load-balanced web application.
References
First published: 16th August 2018