semiotmic2022
Based on:
https://github.com/docker/labs/blob/master/swarm-mode/beginner-tutorial/README.md
https://docs.docker.com/engine/swarm/
Check out the documentation on Docker Swarm Mode for more information.
Create your own Docker ID at https://hub.docker.com/signup and…
Since this example would require various machine it can be done online using: https://labs.play-with-docker.com/
Docker swarm is a container orchestration tool, meaning that it allows the user to manage multiple containers deployed across multiple host machines.
A swarm consists of multiple Docker hosts which run in swarm mode and act as managers (to manage membership and delegation) and workers (which run swarm services). A given Docker host can be a manager, a worker, or perform both roles. When you create a service, you define its optimal state (number of replicas, network and storage resources available to it, ports the service exposes to the outside world, and more). Docker works to maintain that desired state. For instance, if a worker node becomes unavailable, Docker schedules that node’s tasks on other nodes.
A task is a running container which is part of a swarm service and managed by a swarm manager, as opposed to a standalone container.
One of the key advantages of swarm services over standalone containers is that you can modify a service’s configuration, including the networks and volumes it is connected to, without the need to manually restart the service. Docker will update the configuration, stop the service tasks with the out of date configuration, and create new ones matching the desired configuration.
When Docker is running in swarm mode, you can still run standalone containers on any of the Docker hosts participating in the swarm, as well as swarm services. A key difference between standalone containers and swarm services is that only swarm managers can manage a swarm, while standalone containers can be started on any daemon.
You can use the Docker CLI to create a swarm, deploy application services to a swarm, and manage swarm behavior.
In the same way that you can use Docker Compose to define and run containers, you can define and run Swarm service stacks.
One of the key benefits associated with the operation of a docker swarm is the high level of availability offered for applications.
Swarm Mode itself does not do anything different with volumes, it runs any volume mount command you provide on the node where the container is running. If your volume mount is local to that node, then your data will be saved locally on that node. There is no built in functionality to move data between nodes automatically.
We will not consider this detail here. If interested, a good starting point is this: https://docs.docker.com/engine/swarm/services/#give-a-service-access-to-volumes-or-bind-mounts
or this:
https://devopsian.net/posts/share-persistent-storage-volumes-in-swarm/
We start creating 5 nodes (the maximum allowed) to get something like this (IP addresses will vary):
We will now create a swarm by initializing it on the first node (node1
). This is done using the command:
we have to explicitly indicate the addresses since the nodes we are using have multiple addresses on different interfaces.
We'll get:
So, to get the token for the managers we do:
WARNING: the actual values for the token returned and shown from here on can obviously change!
We will now create a swarm with 3 managers (red boxes) and 2 workers (grey circles) with the command obtained above:
To show the members of swarm, from node1
:
From now on, to help understand what happen during this seminar, we will use a tool called Docker Swarm Visualizer, and since we already have a swarm, we create a service with it with the command:
after a few seconds, we'll have a web page available that looks like this:
We now create a new single service called web
that runs the latest nginx. We are doing this in the manager node node1
:
and:
Now you can see that from any of the nodes we have port 80 open and if we access it, will get the nginx welcome page:
You can actually load any of the node ip addresses and get the same result because of Swarm Mode's Routing Mesh.
Now, let's scale the service:
and:
Docker has spread the 10 services evenly over all of the nodes
We can also do a scale up or down by doing:
In this moment the viz
process is executing in node1
. If we kill this service, the swarm will recreate it automatically in the same node or in another one of the swarm:
We create now another service based on redis
by doing:
With swarm we can update automatically the version of the service that is currently executing, by doing:
You can also drain a particular node, that is remove all services from that node. The services will automatically be rescheduled on the other nodes.
For example, starting from:
if we drain all services from node2
doing:
we will get (after a few seconds):
To bring node2
back online and show it's new availability, we have to do:
With Docker swarm you can also:
docker swarm leave
. After a node leaves the swarm, you can run the docker node rm
command on a manager node to remove the node from the node list.now, if we go to another manager:
… much more!
You don't need to use the Docker CLI to perform these operations. You can use docker stack deploy --compose-file STACKNAME.yml STACKNAME
instead. For an introduction to using a stack file in a compose file format to deploy an app, check out Deploying an app to a Swarm.