Red Green Repeat Adventures of a Spec Driven Junkie

Scaling Apps with Docker Swarm

From my last post, Working with Multiple Node Docker Swarm, I introduced having multiple nodes in a Docker Swarm.

Let’s get apps running on the swarm, see how we can track them amongst the nodes and scale up the app.

Revel

Requirements

If you would like to follow along, this is what I used in the article:

WORKER_COUNT = 0

to be:

WORKER_COUNT = 1
  • 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:

vagrant@manager:~$ sudo docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
jjwuzi7gxfs2o9b306sqj9mg2 *   manager             Ready               Active              Leader              18.06.1-ce
qma8dzb9zdxaohf8834ngrjjr     worker1             Ready               Active                                  18.06.1-ce

docker service ls reports:

vagrant@manager:~$ sudo docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS

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
vagrant@manager:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

vagrant@manager:~$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              be1f31be9a87        13 days ago         109MB
vagrant@worker1:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

vagrant@worker1:~$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              be1f31be9a87        13 days ago         109MB

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:

vagrant@manager:~$ sudo docker service create nginx

erwxg0xmh25hm79u7m86fqvub
overall progress: 1 out of 1 tasks
1/1: running   [==================================================>]
verify: Service converged

That was easy.

nginx App Status

Next, let’s chack nginx app status, first using: docker service ls:

vagrant@manager:~$ sudo docker service ls
ID                  NAME                  MODE                REPLICAS            IMAGE               PORTS
erwxg0xmh25h        quizzical_zhukovsky   replicated          1/1                 nginx:latest
vagrant@manager:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
794d284225d7        nginx:latest        "nginx -g 'daemon of…"   13 seconds ago      Up 12 seconds       80/tcp              quizzical_zhukovsky.1.g4hbst4i924vf87trvz3xwzhd

We can also check by running: docker ps, the normal Docker process checker on each node:

vagrant@manager:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
794d284225d7        nginx:latest        "nginx -g 'daemon of…"   About a minute ago   Up About a minute   80/tcp              quizzical_zhukovsky.1.g4hbst4i924vf87trvz3xwzhd

vagrant@worker1:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

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:

vagrant@manager:~$ sudo docker service scale quizzical_zhukovsky=2
quizzical_zhukovsky scaled to 2
overall progress: 2 out of 2 tasks
1/2: running   [==================================================>]
2/2: running   [==================================================>]
verify: Service converged

With the app scaled up to two services, what does docker service ls report?

vagrant@manager:~$ sudo docker service ls
ID                  NAME                  MODE                REPLICAS            IMAGE               PORTS
erwxg0xmh25h        quizzical_zhukovsky   replicated          2/2                 nginx:latest

Which is exactly what we expected. Let’s check the number of running containers on each node, on manager:

vagrant@manager:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
794d284225d7        nginx:latest        "nginx -g 'daemon of…"   About a minute ago   Up About a minute   80/tcp              quizzical_zhukovsky.1.g4hbst4i924vf87trvz3xwzhd

and on worker1:

vagrant@worker1:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
589ea09a236d        nginx:latest        "nginx -g 'daemon of…"   26 seconds ago      Up 25 seconds       80/tcp              quizzical_zhukovsky.2.xs06f0q5scyikvo9u9l4lf34y

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:

vagrant@manager:~$ sudo docker service scale quizzical_zhukovsky=5
quizzical_zhukovsky scaled to 5
overall progress: 5 out of 5 tasks
1/5: running   [==================================================>]
2/5: running   [==================================================>]
3/5: running   [==================================================>]
4/5: running   [==================================================>]
5/5: running   [==================================================>]
verify: Service converged

On manager, docker service ls reports:

vagrant@manager:~$ sudo docker service ls
ID                  NAME                  MODE                REPLICAS            IMAGE               PORTS
erwxg0xmh25h        quizzical_zhukovsky   replicated          5/5                 nginx:latest

Oh, it’s definitely five nginx apps, but which nodes are they running on? There’s only two nodes.

Using docker ps, manager reports:

vagrant@manager:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
74c4591afe6c        nginx:latest        "nginx -g 'daemon of…"   54 seconds ago      Up 53 seconds       80/tcp              quizzical_zhukovsky.5.vbm06ov4mfuvtnn2vmf3cxbco
794d284225d7        nginx:latest        "nginx -g 'daemon of…"   24 hours ago        Up 24 hours         80/tcp              quizzical_zhukovsky.1.g4hbst4i924vf87trvz3xwzhd

Oh, so manager only has two nginx containers running. What does docker ps on worker1 report?

vagrant@worker1:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
1c70e2d343fe        nginx:latest        "nginx -g 'daemon of…"   10 seconds ago      Up 9 seconds        80/tcp              quizzical_zhukovsky.4.l8ct5dfpiw2d6mbqtch5y5dz0
3f085bb0dc1e        nginx:latest        "nginx -g 'daemon of…"   10 seconds ago      Up 9 seconds        80/tcp              quizzical_zhukovsky.3.p562s502kpj8u6gw68qhjyyq0
589ea09a236d        nginx:latest        "nginx -g 'daemon of…"   24 hours ago        Up 24 hours         80/tcp              quizzical_zhukovsky.2.xs06f0q5scyikvo9u9l4lf34y

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??