Docker Swarm Mode and Machine

2 minute read

In this post we will be making use of Docker Engine 1.12 Swarm mode for natively managing a cluster of Docker Engines called a Swarm.

We will be using docker-machine to create our nodes and deploy a simple Flask application that returns the hostname and ip address of the container/host it’s running on.

Prerequisites

docker -v
Docker version 1.12.0, build 8eab29e
docker-machine -v
docker-machine version 0.8.0, build b85aac1

Creating our nodes

We will create our nodes using docker-machine:

for node in manager-1 worker-1 worker-2; do
    docker-machine create -d virtualbox $node
done

Initializing our managers

Here, we are creating a swarm on the manager-1 node and telling it to advertise it’s address using the --advertise-addr flag.

eval `docker-machine env manager-1`
docker swarm init \
 --advertise-addr `docker-machine ip manager-1`:2377

You should see the below output after executing the swarm init command

To add a worker to this swarm, run the following command:
docker swarm join \
 --token SWMTKN-1-44bmnrfrkfpyb4lgryudjd4uaamkozpien1lr6oddhdiq995pc-0mwe6lfak4wjy828vmt63t9ot \
 192.168.99.102:2377

NOTE: Your --token will differ.

Joining our workers

Here, we add worker nodes to our swarm.

WORKER_JOIN_TOKEN=`docker swarm join-token -q worker`
for worker in 1 2; do
    eval `docker-machine env worker-$worker`
    
    docker swarm join \
    --token $WORKER_JOIN_TOKEN \
    `docker-machine ip manager-1`:2377
done

Let’s execute docker node ls on our manager to confirm

eval `docker-machine env manager-1`
docker node ls

You should see the following

ID                           HOSTNAME    STATUS  AVAILABILITY  MANAGER STATUS
01x30nznfg6tv95jl1uyr0wrf *  manager-1   Ready   Active        Leader
0w36v8c8z9vxya9p9vuwrdi9o    worker-1    Ready   Active
e0fk1094a8ihzp1ien27cogku    worker-2    Ready   Active

The * tells us which node we are currently using and MANAGER indicating manager-1 is the Leader.

Deploying our service

docker service create --name flasip \
 --replicas=1 \
 -p 80:5000 \
 thoba/flasip:latest

The above command creates a single instance (--replicas=1) service named flasip (--name flasip) and mapping port 80 to 5000 (-p 80:5000) using the thoba/flasip:latest Docker image.

To check for running services, type the following commands:

docker service ls

We can use docker service scale SERVICE_NAME=NUM_TASKS to scale our service to the desired number of tasks:

docker service scale flasip=6
flasip scaled to 6

To see which node(s) these are deployed/running in, execute:

docker service ps flasip

Get the manager’s IP by executing the following command:

docker-machine ip manager-1
192.168.99.102

To access the flasip service, point your browser to 192.168.99.102 and :boom:

FlasIp_App

Internally, Docker makes use of Linux IPVS, an in-kernel Layer 4 multi-protocol load balancer. With IPVS routing packets inside the kernel, swarm’s routing mesh delivers high performance container-aware load-balancing.

Refresh the browser to see the load balancing feature.

Hope someone finds this helpful. :smiley: