Docker Swarm

  • Docker Swarm is a tool that clusters many Docker engines and schedules containers.
  • Docker Swarm decides which host to run the container based on your scheduling methods.

Service Discovery:

  • SD is a key component of most distributed systems and service oriented architectures.

Create a Manager Node:

$docker-machine create -d amazonec2 --swarm --amazonec2-region ap-southeast-1 --amazonec2-zone a --amazonec2-vpc-id vpc-12112 --amazonec2-ssh-keypath [SSH-PRIV-KEY-FILE] master

Note: Public Key should also be present in the same directory.



Create 2 Slave Nodes:

$docker-machine create -d amazonec2 --swarm --amazonec2-region ap-southeast-1 --amazonec2-zone a --amazonec2-vpc-id vpc-121212 --amazonec2-ssh-keypath [SSH-PRIV-KEY-FILE] slave1
$docker-machine create -d amazonec2 --swarm --amazonec2-region ap-southeast-1 --amazonec2-zone a --amazonec2-vpc-id vpc-121212 --amazonec2-ssh-keypath [SSH-PRIV-KEY-FILE] slave2

To default docker commands to Manager machine:

$docker-machine env master

Login to master and configure it as Manager node:

$docker-machine ssh master


$docker swarm init --advertise-addr [PUBLIC-IP]

To add a worker to this swarm, run the following command:

$docker swarm join \
--token SWMTKN-1-34t11111111111111111021crh0xwoktwxzwb \

To add a manager to this swarm, run ‘docker swarm join-token manager’ and follow the instructions.
Login to Slave and execute the above command to add as a worker.






Docker daemons participating in a swarm need the ability to communicate with each other over the following ports:

  • Port 7946 TCP/UDP for container network discovery.
  • Port 4789 UDP for the container overlay network.


NOTE: To resolve to the container based on the service name you provide, you have to create separate networking with overlay driver and use that driver for service creation(use –publish option to expose the port outside).

$docker network create --driver overlay --subnet --gateway my-network
$docker service create --replicas 2 --network my-network -p 80:80 --name web --mount type=bind,src=/etc/hostname,dst=/usr/share/nginx/html/index.html,readonly nginx
  • This will resolve the name “web” to a virtual IP inside the container but it will not be accessible outside the containers.
  • This will load Nginx containers in both master and slave.

Log into one of the containers

$apt-get udpate
$apt-get install dnsutils curl net-tools
$nslookup web

The IPs of nslookup and ifconfig are different, the IP of nslookup is Virtual IP.

This will RR between containers running on the same host only.
Status Check of Service:

$docker service ls
$docker service ps web

To scale up a service:

$docker service scale web=5

To remove the whole setup

$docker service rm web



SWARM has a built-in load balancer, why another load balancer?

SWARM does not have:

  1. SSL Termination
  2. Content Based routing
  3. Access control and authorization
  4. Rewrites and redirects.
  5. More on Nginx – Advance LB Algorithms, Multiprotocol support, Advanced logging, limits, scripting, security.
    (Native mod-security available for Nginx.)

[BLOG Incomplete]


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s