Docker Swarm is native clustering for Docker. It allows you create and access to a pool of Docker hosts using the full suite of Docker tools. Because Docker Swarm serves the standard Docker API, any tool that already communicates with a Docker daemon can use Swarm to transparently scale to multiple hosts.
Docker Swarm has a Manager, a pre-defined Docker Host, and is a single point for all administration. Swarm manager orchestrates and schedules containers on the entire cluster, and can be configured in High Availability. The containers are deployed on Nodes that are additional Docker Hosts.
Swarm talks to a hosted Discovery Service that maintains a list of IPs in your cluster. For development, its easy to use the default discovery service hosted on Docker Hub. Complete instructions for that are available in Install and Create Docker Swarm. This blog will show how to setup Docker Swarm Cluster using Consul.
Lets get started!
Create Consul Discovery Service
- Create a Machine that will host discovery service:
12345678910111213docker-machine create -d=virtualbox consul-machineRunning pre-create checks...Creating machine...Waiting for machine to be running, this may take a few minutes...Machine is running, waiting for SSH to be available...Detecting operating system of created instance...Provisioning created instance...Copying certs to the local machine directory...Copying certs to the remote machine...Setting Docker configuration on the remote daemon...To see how to connect Docker to this machine, run: docker-machine env consul-machine - Connect to this Machine:
123eval $(docker-machine env consul-machine) - Run Consul service using the following Compose file:
123456789myconsul:image: progrium/consulrestart: alwayshostname: consulports:- 8500:8500command: "-server -bootstrap"The service is started as:
1234567891011121314151617181920212223242526docker-compose up -dPulling myconsul (progrium/consul:latest)...latest: Pulling from progrium/consul3b4d28ce80e4: Pull completee5ab901dcf2d: Pull complete30ad296c0ea0: Pull complete3dba40dec256: Pull completef2ef4387b95e: Pull complete53bc8dcc4791: Pull complete75ed0b50ba1d: Pull complete17c3a7ed5521: Pull complete8aca9e0ecf68: Pull complete4d1828359d36: Pull complete46ed7df7f742: Pull completeb5e8ce623ef8: Pull complete049dca6ef253: Pull completebdb608bc4555: Pull complete8b3d489cfb73: Pull completec74500bbce24: Pull complete9f3e605442f6: Pull completed9125e9e799b: Pull completeDigest: sha256:8cc8023462905929df9a79ff67ee435a36848ce7a10f18d6d0faba9306b97274Status: Downloaded newer image for progrium/consul:latestCreating consul_myconsul_1Started container can be verified as:
12345docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf05d8dd11e7f progrium/consul "/bin/start -server -" 30 seconds ago Up 29 seconds 53/tcp, 53/udp, 8300-8302/tcp, 8400/tcp, 0.0.0.0:8500->8500/tcp, 8301-8302/udp consul_myconsul_1
Create Docker Swarm Cluster using Consul
Swarm is fully integrated with Machine, and so is the easiest way to get started.
- Create a Swarm Master using the Consul discovery service:
1234567891011121314docker-machine create -d virtualbox --swarm --swarm-master --swarm-discovery="consul://$(docker-machine ip consul-machine):8500" --engine-opt="cluster-store=consul://$(docker-machine ip consul-machine):8500" --engine-opt="cluster-advertise=eth1:2376" swarm-masterRunning pre-create checks...Creating machine...Waiting for machine to be running, this may take a few minutes...Machine is running, waiting for SSH to be available...Detecting operating system of created instance...Provisioning created instance...Copying certs to the local machine directory...Copying certs to the remote machine...Setting Docker configuration on the remote daemon...Configuring swarm...To see how to connect Docker to this machine, run: docker-machine env swarm-master--swarm-discovery
defines address of the discovery service--cluster-advertise
advertise the machine on the network--cluster-store
designate a distributed k/v storage backend for the cluster
In addition,
--swarm
configures the Machine with Swarm,--swarm-master
configures the created Machine to be Swarm master. - Connect to this newly created master and find some information about it:
1234eval "$(docker-machine env swarm-master)"docker info
This will show the output as:
123456789101112131415161718192021222324252627282930313233docker infoContainers: 2Images: 8Server Version: 1.9.0Storage Driver: aufsRoot Dir: /mnt/sda1/var/lib/docker/aufsBacking Filesystem: tmpfsDirs: 12Dirperm1 Supported: trueExecution Driver: native-0.2Logging Driver: json-fileKernel Version: 4.1.12-boot2dockerOperating System: Boot2Docker 1.9.0 (TCL 6.4); master : 16e4a2a - Tue Nov 3 19:49:22 UTC 2015CPUs: 1Total Memory: 996.2 MiBName: swarm-masterID: 2EDA:WPOD:YVWO:GGLZ:ZUHY:WCBU:ZERW:OWBE:6MPQ:IPXN:BS2V:QCSIDebug mode (server): trueFile Descriptors: 30Goroutines: 67System Time: 2015-11-29T02:08:10.636829121ZEventsListeners: 1Init SHA1:Init Path: /usr/local/bin/dockerDocker Root Dir: /mnt/sda1/var/lib/dockerUsername: arunguptaRegistry: https://index.docker.io/v1/Labels:provider=virtualboxCluster store: consul://192.168.99.109:8500Cluster advertise: 192.168.99.111:2376 - Create a new Machine to be part of this Swarm cluster:
1234567891011121314docker-machine create -d virtualbox --swarm --swarm-discovery="consul://$(docker-machine ip consul-machine):8500" --engine-opt="cluster-store=consul://$(docker-machine ip consul-machine):8500" --engine-opt="cluster-advertise=eth1:2376" swarm-node-01Running pre-create checks...Creating machine...Waiting for machine to be running, this may take a few minutes...Machine is running, waiting for SSH to be available...Detecting operating system of created instance...Provisioning created instance...Copying certs to the local machine directory...Copying certs to the remote machine...Setting Docker configuration on the remote daemon...Configuring swarm...To see how to connect Docker to this machine, run: docker-machine env swarm-node-01
Machine talks to the Discovery Service using
--swarm-discovery
. - Create a second node in this cluster:
1234567891011121314docker-machine create -d virtualbox --swarm --swarm-discovery="consul://$(docker-machine ip consul-machine):8500" --engine-opt="cluster-store=consul://$(docker-machine ip consul-machine):8500" --engine-opt="cluster-advertise=eth1:2376" swarm-node-02Running pre-create checks...Creating machine...Waiting for machine to be running, this may take a few minutes...Machine is running, waiting for SSH to be available...Detecting operating system of created instance...Provisioning created instance...Copying certs to the local machine directory...Copying certs to the remote machine...Setting Docker configuration on the remote daemon...Configuring swarm...To see how to connect Docker to this machine, run: docker-machine env swarm-node-02
- List all the created Machines:
123456789docker-machine lsNAME ACTIVE DRIVER STATE URL SWARMconsul-machine - virtualbox Running tcp://192.168.99.109:2376default - virtualbox Running tcp://192.168.99.100:2376swarm-master * virtualbox Running tcp://192.168.99.111:2376 swarm-master (master)swarm-node-01 - virtualbox Running tcp://192.168.99.112:2376 swarm-masterswarm-node-02 - virtualbox Running tcp://192.168.99.113:2376 swarm-master
The machines that are part of the cluster have cluster’s name in the SWARM column, blank otherwise. For example, “
default
” and “consul-machine
” are standalone machines where as all other machines are part of the “swarm-master
” cluster. The Swarm master is also identified by (master) in the SWARM column. - Connect to the Swarm cluster and find some information about it:
1234eval "$(docker-machine env --swarm swarm-master)"docker info
The main difference here is
--swarm
when finding information about Swarm cluster as opposed to a single Machine.This shows the output as:
123456789101112131415161718192021222324252627docker infoContainers: 4Images: 3Role: primaryStrategy: spreadFilters: health, port, dependency, affinity, constraintNodes: 3swarm-master: 192.168.99.111:2376└ Containers: 2└ Reserved CPUs: 0 / 1└ Reserved Memory: 0 B / 1.021 GiB└ Labels: executiondriver=native-0.2, kernelversion=4.1.12-boot2docker, operatingsystem=Boot2Docker 1.9.0 (TCL 6.4); master : 16e4a2a - Tue Nov 3 19:49:22 UTC 2015, provider=virtualbox, storagedriver=aufsswarm-node-01: 192.168.99.112:2376└ Containers: 1└ Reserved CPUs: 0 / 1└ Reserved Memory: 0 B / 1.021 GiB└ Labels: executiondriver=native-0.2, kernelversion=4.1.12-boot2docker, operatingsystem=Boot2Docker 1.9.0 (TCL 6.4); master : 16e4a2a - Tue Nov 3 19:49:22 UTC 2015, provider=virtualbox, storagedriver=aufsswarm-node-02: 192.168.99.113:2376└ Containers: 1└ Reserved CPUs: 0 / 1└ Reserved Memory: 0 B / 1.021 GiB└ Labels: executiondriver=native-0.2, kernelversion=4.1.12-boot2docker, operatingsystem=Boot2Docker 1.9.0 (TCL 6.4); master : 16e4a2a - Tue Nov 3 19:49:22 UTC 2015, provider=virtualbox, storagedriver=aufsCPUs: 3Total Memory: 3.064 GiBName: 10edc606d097There are 3 nodes – one Swarm master and 2 Swarm _worker_ nodes. There is a total of 4 containers running in this cluster – one Swarm agent on master and each node, and there is an additional swarm-agent-master running on the master. This can be verified by connecting to the master and listing all the containers.
- List nodes in the cluster with the following command:
123456docker run swarm list consul://$(docker-machine ip consul-machine):8500192.168.99.111:2376192.168.99.112:2376192.168.99.113:2376
Subsequent blog will explain how to deploy applications to this Docker Swarm Cluster.
Enjoy!
Thank you Arun! Very interesting and useful. Could you include in the future any method for load balancing in Swarm, please? Regards.
Marcelo,
Thanks for your comments. Load balancing in Swarm is planned for a future blog.
Cool and how would you secure consul for production?
Laurent,
Here is a possible way: https://www.digitalocean.com/community/tutorials/how-to-configure-consul-in-a-production-environment-on-ubuntu-14-04
Hi Arun,
I’m wondering if you are available for consultation on Docker, or a quick remote call, on Docker Swarm Cluster concept and deployment.
cheers.
Patrick,
Can try to help, send me email at arun . gupta at gmail.com.
Is it possible to run the consul server on the swarm master ?
I’m going to have a very small cluster, as cheap as possible, and would rather not have an extra box just running Consul.
what about docker service?when i run this command 404 for me.why?
gb, Not sure I understand the question. Which service?
You should definitely check https://essaydragon.com/blog/paraphrasing-tools out if you want to know more about paraphrasing tools. I had such experience so I know what I am talking about
If you are thinking of ordering thread answering help, choose this writing service https://specialessays.com/write-my-discussion-board-post/