Run a balanced application in a Docker Swarm

Here's a quick and dirty guide to getting a Docker Swarm cluster up and running.

Let's create 3 virtual machines our on local machine to help illustrate this. Make sure you have docker, docker-machine, and virtualbox installed

docker-machine create --driver virtualbox jm01
docker-machine create --driver virtualbox jm02
docker-machine create --driver virtualbox jm03

Use docker-machine to list them and see their ip addresses

docker-machine ls

Now ssh into jm01 so we can set it as the manager node

docker-machine ssh jm01

Run the following command to start is as a manager node for the cluster

docker swarm init --advertise-addr <jm01 ip> 

The command above will give you output you can use to join your other servers (jm02,jm03) as worker nodes in your cluster.

docker swarm join \
    --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \
    192.168.99.100:2377

On jm01 create the service.  We are using this hello world example running on port 80. Note we want 5 instances of the containers, but only have 3 servers to run them on.

docker service create --replicas 5 --name asdf01 -p 80:80 tutum/hello-world

You can watch them come up from jm01

docker service ls

After the containers are launched you can see them running on each node.

docker service ps asdf01

You can now visit port 80 on any of the nodes in the cluster and get a response from a random container in the cluster

curl <jm01 ip address>:80  

Other useful commands

docker service ls
docker service scale asdf01=2
docker service rm asdf01