Initialize Swarm with `docker swarm init`.
Creating a service
Using `docker service create`, create a service from the image `traefik/whoami` accessible on port 9999 and connected to port 80 with 5 replicas.
Access your service and refresh the page several times. The displayed information changes. Why?
The example-voting-app stack
-
Read the architecture schema of the example-voting-app on GitHub. Note that the worker service exists in two versions using different programming languages (Java or .NET), and all services have images for both Windows and Linux containers. These versions can be deployed interchangeably and do not change the functionality of the multi-container application. It's a demonstration of the usefulness of containerization paradigm and “micro-service” architecture.
Carefully read the files docker-compose.yml, docker-compose-simple.yml, docker-stack-simple.yml, and docker-stack.yml. These are all classic Docker Compose files with different options related to deployment via Swarm. Which options seem specific to Docker Swarm? These options allow configuring orchestration features.
Quickly draw the architecture schema associated with the `docker-compose-simple.yml` file, then the one associated with `docker-stack.yml`, indicating which service belongs to which network.
With `docker swarm init`, transform your Docker installation into a Swarm-compatible installation. Read carefully the message returned to you.
Deploy the stack from the `docker-stack.yml` file: docker stack deploy –compose-file docker-stack.yml vote.
docker stack ls indicates 6 services for the`vote stack. Also observe the output of docker stack ps vote and `docker stack services vote`. What is a service in Swarm terminology?
Access the different front-ends of the stack using the information from the previous commands. On the vote-linked front-end, refresh the page several times. What does the line “Processed by container ID […]” mean? Why does it vary?
Scale the stack by adding replicas of the vote-linked front-end using `docker service –help`. Access this front-end and verify that it has worked by refreshing several times.
Clustering among friends
With a service
Group in pairs or threes to create a cluster from your respective VMs (use a Swarm command to retrieve the necessary instructions).
If grouping several VMs is not possible, you can create a multi-node cluster very simply with the interface of the Play With Docker site, you must log in with your Docker Hub credentials.
You can run `docker swarm –help` to get missing information, or run `docker swarm leave –force` to reset your Docker Swarm configuration if necessary.
Feel free to look in the logs with `systemctl status docker` how the node leader election is going, from the moment you have more than one manager.
Launch the following service: `docker service create –name whoami –replicas 5 –publish published=80,target=80 traefik/whoami`.
Access the service from one node, and from the other. Refresh the page several times. The displayed information changes. Which ones, and why?
With the example-voting-app stack
-
Add in the Compose file instructions to scale two services differently (3 replicas for the front-end service, for example). Don't forget to redeploy your Compose file.
Then specify some orchestration options exclusive to Docker Swarm: what does `mode: global` do? Don't forget to redeploy your Compose file.
With Portainer or with docker-swarm-visualizer, explore the cluster thus created (the `docker-stack.yml` file of the example-voting-app already contains a copy of docker-swarm-visualizer).
Find the command to demote and promote one of your manager nodes to worker and vice versa.
Then remove a node from the cluster (drain): docker node update –availability drain <node-name>.
Optional: debug the Docker config of example-voting-app
Did you notice? We successfully deployed a great voting application stack, but if you test the vote, you'll see that it doesn't work, it's not counted. Besides being a living plea against electronic voting, you could try to debug it now (it's pretty easy).
Solution / explanations:
Someone abandoned the Docker Hub repository linked to this app, and the person who has access to it is unreachable! It's a great example of the reality of the Docker ecosystem, and the fact that you should be wary of images created by others. Fortunately, it's just a matter of:
rebuilding the different images from their Dockerfile,
then editing your Docker Compose file (`docker-stack.yml`) to base it on the image you just rebuilt.
Introduction to Kubernetes
The `kube-deployment.yml` file of the example-voting-app describes the same app for deployment in Kubernetes rather than Docker Compose or Docker Swarm. Try to find some equivalences between Docker Compose / Swarm and Kubernetes by carefully reading this file describing a Kubernetes deployment.
Back to top