Scale Your Docker Containers with Docker Swarm

Extending the Hive

Joining a Node to the Swarm

Now it's time to join nodes to the swarm. Remember the token you were given after initializing the manager? You'll need that now. The command to join a node to the swarm is

docker swarm join --token TOKEN 192.168.1.71:2377

where TOKEN is the token generated by the swarm initialization.

When you run the command, on success, you will see that the node has been joined to the swarm (Figure 3). Run that same join command on all the nodes you want in the swarm. Once you're done, head back to the manager and, again, issue the command:

docker info
Figure 3: The first node has been joined to the swarm.

You should now see that the swarm is active, the machine is serving as a manager, and a total of three machines are in the swarm (Figure 4).

Figure 4: A functioning swarm.

To see the nodes listed, go to the manager and enter:

docker node ls

The three nodes are listed (Figure 5), with dockermanager as the Leader .

Figure 5: The list of nodes.

Deploying a Service

With an active swarm, you can now deploy your first Nginx container; go back to the manager and issue the command:

docker service create -p 80:80 --name webserver nginx

The breakdown of the above command is simple:

  • docker service create instructs Docker you're creating a new service.
  • -p 80:80 instructs Docker to use external port 80 for the service and route it to internal (within the container) port 80.
  • --name webserver instructs Docker to name the service webserver .
  • nginx is the image to be used for the service.

To make sure the new service is listed, enter:

docker service ls

You should now see that the webserver service is running on port 80 (Figure 6).

Figure 6: The service is good to go.

Scaling Your Service

At the moment, your service is only running on one node. Because you have three nodes available, why not scale it up to use all three? To do so, you instruct Docker to scale the webserver service to three nodes with the command (run on the manager):

docker service scale webserver=3

Docker will start deploying the service to all three nodes (Figure 7). To verify the service has been scaled to all nodes, enter:

docker service ps webserver
Figure 7: Docker is scaling the service to all three nodes.

You should now see that the three nodes have the service running (Figure 8).

Figure 8: The webserver has been scaled to all three nodes.

If the service goes down on any one node, it will automatically be restarted on the same node or, if the original node is no longer available, a different node.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy ADMIN Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus