Download
thisVagrantfile
to your local folder and modify this line:
to be:
Run: vagrant up to create virtual computers: manager and worker1.
Fresh start
We will start from the same Vagrantfile and configuration as last
time. Let’s make sure your cluster has a similar set up for Docker
Swarm:
docker node ls reports:
docker service ls reports:
Let’s make sure on each node there’s no other app running and the node
has the nginx image:
ssh into each by running vagrant ssh manager or vagrant ssh worker1
use docker ps to list any running apps
use docker images to list currently installed images
use docker pull nginx to get the nginx:latest image from Docker
Hub
Basically, I want to have no apps running and to have the nginx
image loaded on each node.
New nginx Service
I will work with the nginx app as:
easy to work with out of the box
scales well
no additional configuration needed
First, create a new nginx app on the Swarm:
That was easy.
nginx App Status
Next, let’s chack nginx app status, first using: docker service ls:
We can also check by running: docker ps, the normal Docker process
checker on each node:
Notice that docker service ls indicates one is up, and docker ps
only shows the process running on the manager node. On the worker1
node, docker ps indicates nothing running.
Scaling nginx App
Let’s take this app to the next level: by scaling up to two of them!
Here, we use the docker service scale command on manager, which has
the form of:
docker service scale <name of service>=<number to scale to>
In our case, I want to scale up the nginx app to two:
With the app scaled up to two services, what does docker service ls
report?
Which is exactly what we expected. Let’s check the number of running
containers on each node, on manager:
and on worker1:
Wow, each has one nginx container running on it! We only needed to
run: docker service scale.
Scaling up
What happens if we scale the nginx app beyond the number of nodes
available? (Which in this case, is two)
Let’s see what happens when scaling up the number of nginx apps to
FIVE, yeah, that’s more than twice the number of nodes available!
Use the same command:
On manager, docker service ls reports:
Oh, it’s definitely five nginx apps, but which nodes are they running
on? There’s only two nodes.
Using docker ps, manager reports:
Oh, so manager only has two nginx containers running. What does
docker ps on worker1 report?
Oh, worker1 has three containers.
Conclusion
Docker Swarm has important commands when working with apss on
connected nodes:
docker service ls
docker service scale
docker ps
The first allows us to see the number of apps are running on the Swarm
overall.
The last allows us to see in detail, when logged into each node, what
containers are running on that node, initiated by docker service
scale.
Most interesting is thta docker swarm service scale sets up and runs
any number of containers on nodes connected to the Swarm.
The only thing we did was just specify a value. If we want more nginx
containers running, we just have to specify another value.
Isn’t that an insanely simple way to scale up an app??