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.
Creating our node machines
We will create our machines using docker-machine:
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.
You will have a diffrent --token to join your worker nodes
Joining our workers
Here, we add worker nodes to our swarm.
Now, if we ssh into our manager node (docker-machine ssh default) and run docker node ls, we should see the following:
Deploying our service
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:
We can use the docker service scale SERVICE=NUM_OF_TASKS to scale our service to the disired number of tasks:
Pointing your browser to $(docker-machine ip default), which is 192.168.99.102 in my case, shall present you with the following:
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.