Now that we have a Swarm cluster, it's time to start using it. We'll show that the spread strategy algorithm will decide to place containers to the less loaded hosts. In this example, it's really easy, as we start with four empty nodes. So, we're connected to Swarm and Swarm will put containers on hosts. We start one nginx container, mapping its port tcp/80 to the host (machine) port tcp/80
.
$ docker run -d -p 80:80 nginx 2c049db55f9b093d19d575704c28ff57c4a7a1fb1937bd1c20a40cb538d7b75c
In this example, we see that the Swarm scheduler decided to place this container onto node1
:
Since we have to bind a port tcp/80
to any host, we will have only four chances, four containers on four different hosts. Let's create new nginx containers and see what happens:
$ docker run -d -p 80:80 nginx 577b06d592196c34ebff76072642135266f773010402ad3c1c724a0908a6997f $ docker run -d -p 80:80 nginx 9fabe94b05f59d01dd1b6b417f48155fc2aab66d278a722855d3facc5fd7f831 $ docker run -d -p 80:80 nginx 38b44d8df70f4375eb6b76a37096f207986f325cc7a4577109ed59a771e6a66d
Now we have 4 nginx containers placed on our 4 Swarm hosts:
Now we try to create a new nginx:
$ docker run -d -p 80:80 nginx docker: Error response from daemon: Unable to find a node that satisfies the following conditions [port 80 (Bridge mode)]. See 'docker run --help'.
What happened is just that Swarm wasn't able to find a suitable host to place a new container on, because on all hosts, port tcp/80
are all occupied. After running these 4 nginx containers, plus the four swarm containers (for the infrastructure management), as we expected, we have eight running containers on this Swarm cluster:
This is how Swarm v1 was intended to work (and still does its job).