Lab6 Docker SWarm
Lab6 Docker SWarm
The application
The voting app is a multi-container application often used for demo purposes during Docker meetups and conferences. It basically allow users to
vote between two choices, the default being “cat” and “dog”, but could be “space” or “tab”, too, if you feel like it. This application is available on
Github and updated very frequently when new features are developed.
To add a manager to this swarm, run 'docker swarm join-token manager' and follow
the instructions.
In the output of your swarm init, you are given a command in the middle that looks like docker swarm join -token SWMTKN-X-abcdef..... which
you use to join workers nodes to the swarm. You are also given a second command docker swarm join-token manager for adding additional
managers.
We are going to add a worker. Copy the “docker swarm join…” command from your manager’s output and paste it in the 2nd terminal window on
your screen. Make sure to copy the entire command - it’s likely to break across multiple lines - and do not copy the sample command above
because your token will be different.
You now officially have a Docker Swarm! Currently, you have one manager and one worker. As hinted at above, you would almost always have 3
or more manager nodes and several worker nodes in order to maintain high availability and scalability, but one of each is enough to get started.
docker node ls
The above command should output 2 nodes, the first one being the manager, and the second one a worker. You should see that your manager
node is also the “Leader”. This is because you only have one manager node. The Leader is just what it sounds like: the main control node for all
the managers. If your Leader node goes down for some reason, the other manager nodes will elect a new leader; just one reason why you would
always have multiple manager nodes in true production.
Here is a view of managers and workers in Docker Swarm Mode. In our exercise, we have just one manager and one worker, but you can see
1
Deploy a Stack
A stack is a group of services that are deployed together: multiple containerized components of an application that run in separate instances.
Each individual service can actually be made up of one or more containers, called tasks and then all the tasks & services together make up
a stack.
As with Dockerfiles and the Compose files, the file that defines a stack is a plain text file that is easy to edit and track. In our exercise, there is a
file called docker-stack.yml in the current folder which will be used to deploy the voting app as a stack. Enter the following to investigate
the docker-stack.ymlfile:
cat docker-stack.yml
This YAML file defines our entire stack: the architecture of the services, number of instances, how everything is wired together, how to handle
updates to each service. It is the source code for our application design. A few items of particular note:
• Near the top of the file you will see the line “services:”. These are the individual application components. In the voting app we have
redis, db, vote, result, worker, and visualizer as our services.
• Beneath each service are lines that specify how that service should run:
o Notice the familiar term image from earlier labs? Same idea here: this is the container image to use for a particular service.
o Ports and networks are mostly self-explanatory although it is worth pointing out that these networks and ports can be
privately used within the stack or they can allow external communication to and from a stack.2
o Note that some services have a line labeled replicas: this indicates the number of instances, or tasks, of this service that
the Swarm managers should start when the stack is brought up. The Docker engine is intelligent enough to automatically
load balance between multiple replicas using built-in load balancers. (The built-in load balancer can, of course, be swapped
out for something else.)
Ensure you are in the [node1] manager terminal and do the following:
docker stack ls
The output should be the following. It indicates the 6 services of the voting app’s stack (named voting_stack) have been deployed.
NAME SERVICES
voting_stack 6
We can get details on each service within the stack with the following:
2
You should get an output like the following one where the 2 tasks (replicas) of the service are listed.
ID NAME IMAGE
NODE DESIRED STATE CURRENT STATE ERROR PORTS
my7jqgze7pgg voting_stack_vote.1 dockersamples/examplevotingapp_vote:be
fore node1 Running Running 56 seconds ago
3jzgk39dyr6d voting_stack_vote.2 dockersamples/examplevotingapp_vote:be
fore node2 Running Running 58 seconds ago
From the NODE column, we can see one task is running on each node. This app happens to have a built-in SWARM VISUALIZER to show you
how the app is setup and running. You can also access the front-end web UI of the app to cast your vote for dogs or cats, and track how the votes
are going on the result page. Try opening the front-end several times so you can cast multiple votes. You should see that the “container ID” listed
at the bottom of the voting page changes since we have two replicas running.
The SWARM VISUALIZER gives you the physical layout of the stack, but here is a logical interpretation of how stacks, services and tasks are
inter-related:
Scaling An Application
Let us pretend that our cats vs. dogs vote has gone viral and our two front-end web servers are no longer able to handle the load. How can we tell
our app to add more replicas of our vote service? In production you might automate it through Docker’s APIs but for now we will do it manually.
You could also edit the docker-stack.yml file and change the specs if you wanted to make the scale size more permanent. Type the following at
the [node1] terminal:
Conclusion
Using only a couple of commands enables you to deploy a stack of services using Docker Swarm Mode to orchestrate the entire stack, all
maintained in the simple, human readable Docker Compose file format.