Distributed Systems Practice Notes

Docker and Containers - Swarm Mode Intro Lab

November 01, 2018

This lab demonstrated how to use Swarm to orchestrate multiple containers to form services and work as a stack. The stack exposes its functional components as services, each service could be distributed to multiple worker nodes as task replicas (one container per replica). At last, we see how to dynamically scale up the task replicas of a service.

Learning Outcomes

  • What is a Docker stack
  • How to use Swarm to orchestrate multiple containers to form services and work as a stack
  • How to dynamically scale up the task replicas of a service in Swarm

Operations

0: Start Swarm and Join

To build up architecture as below,

swarm arch

  • On manager node, start Swarm
docker swarm init --advertise-addr $(hostname -i)
  • On worker node, join Swarm
docker swarm join --token <join token> <manager node IP:port>

1: Deploy a Stack

stack

A stack is a group of services that are deployed together: multiple containerized components of an application that run in separate instances. Each individual service can actually be made up of one or more containers, called tasks and then all the tasks & services together make up a stack.

  • Clone demo repository on manager node
git clone https://github.com/docker/example-voting-app
cd example-voting-app

The docker-stack.yml YAML file defines our entire stack: the architecture of the services, number of instances, how everything is wired together, how to handle updates to each service. It is the source code for our application design.

  • Deploy Stack Using the YAML file on manager node
docker stack deploy --compose-file=docker-stack.yml voting_stack
  • See all services in stack
docker stack ls
docker stack services voting_stack
  • List the tasks of the vote service, see 2 replicas
docker service ps voting_stack_vote

visualizer

2: Scaling An Application

scaled stack

  • As shown above, we will scale the vote service up to 5 replicas
docker service scale voting_stack_vote=5

visualizer scaled

Official Links

Lab: Swarm Mode Introduction


Warren

Written by Warren who studies distributed systems at George Washington University. You might wanna follow him on Github