Docker Swarm Mode and Machine

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

thoba at xps in ~
$ docker -v
Docker version 1.12.0, build 8eab29e
thoba at xps in ~
$ docker-machine -v
docker-machine version 0.8.0, build b85aac1

Creating our node machines

We will create our machines using docker-machine:

thoba at xps in ~
$ docker-machine create -d virtualbox default
thoba at xps in ~
$ docker-machine create -d virtualbox devel
thoba at xps in ~
$ docker-machine create -d virtualbox dev

Initialising our managers

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

thoba at xps in ~
$ docker `docker-machine config default` swarm init \
  --advertise-addr `docker-machine ip default`:2377

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

You will have a diffrent --token to join your worker nodes

Joining our workers

Here, we add worker nodes to our swarm.

thoba at xps in ~
$ docker `docker-machine config devel` swarm join \
  --token SWMTKN-1-44bmnrfrkfpyb4lgryudjd4uaamkozpien1lr6oddhdiq995pc-0mwe6lfak4wjy828vmt63t9ot \
  `docker-machine ip default`:2377

thoba at xps in ~
$ docker `docker-machine config dev` swarm join \
  --token SWMTKN-1-44bmnrfrkfpyb4lgryudjd4uaamkozpien1lr6oddhdiq995pc-0mwe6lfak4wjy828vmt63t9ot \
  `docker-machine ip default`:2377

Now, if we ssh into our manager node (docker-machine ssh default) and run docker node ls, we should see the following:

docker@default:~$ docker node ls
ID                           HOSTNAME  STATUS  AVAILABILITY  MANAGER STATUS
01x30nznfg6tv95jl1uyr0wrf *  default   Ready   Active        Leader
0w36v8c8z9vxya9p9vuwrdi9o    dev       Ready   Active        
e0fk1094a8ihzp1ien27cogku    devel     Ready   Active        
docker@default:~$

Deploying our service

docker@default:~$ 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 and see which nodes they are deployed/running in, type the following commands:

docker@default:~$ docker service ls
ID            NAME    REPLICAS  IMAGE               COMMAND
cpgo3gc27jdb  flasip  1/1       thoba/flasip:latest  
docker@default:~$ docker service ps flasip
ID                         NAME      IMAGE                NODE  DESIRED STATE  CURRENT STATE          ERROR
e17ahw67f48w2rm20g5td0524  flasip.1  thoba/flasip:latest  zero  Running        Running 3 minutes ago

We can use the docker service scale SERVICE=NUM_OF_TASKS to scale our service to the disired number of tasks:

docker@default:~$ docker service scale flasip=6
flasip scaled to 6
docker@default:~$ docker service ls
ID            NAME    REPLICAS  IMAGE               COMMAND
cpgo3gc27jdb  flasip  6/6       thoba/flasip:latest  

Pointing your browser to $(docker-machine ip default), which is 192.168.99.102 in my case, shall present you with the following:

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.

Refreshing the browser will allow you to see the Load balancing feature that comes with Docker Swarm mode.

I hope this was helpful. :-)

comments powered by Disqus