As we previously explained Weave Discovery allows Weave Routers to find each other automatically, solving the initial configuration problem when setting up your overlay container network.

In this article we will examine how Weave Discovery can be used for automating the creation of a Swarm cluster connected with Weave.

Docker Swarm

Swarm is the native clustering solution from Docker. It provides a Docker API in a manager that runs between your Docker client and the Docker servers in your cluster. The manager knows about Docker servers available thanks to Swarm agents that keep advertising these daemons in a discovery backend. When pointing your Docker client to the manager, Swarm can be used for transparently running commands in a distributed manner, like scheduling the execution of containers in the cluster.


Docker Swarm and Weave

The first thing we must do in order to create a Swarm & Weave cluster is to get a Discovery backend where Swarm agents can announce to the manager. We will use a token in this example, but we could use some of the other backends supported by Swarm. The token can be obtained with a POST to some magic URL, or with the equivalent Swarm command:

host$ docker run --rm swarm create<br> 6856663cdefdec325839a4b7e1de38e8<br>

You must then install Weave in each of your machines and launch it (see the docs for more details).

vm1$ weave launch ...<br>


That will launch the Weave Proxy. Make sure the Proxy is listening at port 12375, and that port is open in your firewall. In contrast with a regular Swarm cluster, we will advertise this Proxy (instead of the Docker daemon) to the Swarm manager, so the manager will talk to the Proxy and all new containers will be automatically attached to the Weave network.

Now we are ready for launching the Swarm agent in your machines with:

vm1$ docker run swarm join --advertise=vm1:12375 token://6856663cdefdec325839a4b7e1de38e8<br>

This will add vm1:12375 (by registering it in the backend) as a Docker instance available in the cluster. You can check your agents are doing their job by listing the contents of the Discovery backend:

vm1$ docker run swarm list token://6856663cdefdec325839a4b7e1de38e8<br> 10.246.2.2:12375<br> 10.246.2.3:12375<br>

Once your agents are up and running, you can launch the manager with

vmmaster$ swarm manage -H tcp://0.0.0.0:22375 token://6856663cdefdec325839a4b7e1de38e8<br>

This will launch a Swarm manager that will expose a Docker API at port 22375. You can then point your Docker client to that port and run something like:

vmmaster$ DOCKER_HOST=”tcp://127.0.0.1:2375” docker info<br> Containers: 17<br> Images: 26<br> Storage Driver:<br> Role: primary<br> Strategy: spread<br> Filters: affinity, health, constraint, port, dependency<br> Nodes: 2<br> vm1: 10.246.2.2:12375<br> └ Containers: 6<br> └ Reserved CPUs: 0 / 2<br> └ Reserved Memory: 0 B / 2.052 GiB<br> └ Labels: executiondriver=native-0.2, kernelversion=3.19.0-21-generic, operatingsystem=Ubuntu 15.04, storagedriver=overlay<br> vm2: 10.246.2.3:12375<br> └ Containers: 6<br> └ Reserved CPUs: 0 / 2<br> └ Reserved Memory: 0 B / 2.052 GiB<br> └ Labels: executiondriver=native-0.2, kernelversion=3.19.0-21-generic, operatingsystem=Ubuntu 15.04, storagedriver=overlay<br> Execution Driver:<br> Kernel Version:<br> Operating System:<br> CPUs: 6<br> Total Memory: 6.155 GiB<br> Name:<br> ID:<br>

Keeping things connected

So far we have started a Swarm cluster where every new container will be automatically attached to the Weave network thanks to the Proxy. However, there is something missing here: our Weave Routers are not connected as they know nothing about each other.

Here is where Weave Discovery comes into play. By using the same Discovery backend as Swarm, Weave Discovery can also observe the peers that are being registered or removed in this backend: for each new peer that is announced by a Swarm agent, Discovery can tell the local Router to CONNECT to that peer, and for each peer that disappears from the backend, Discovery will tell the Router to FORGET that peer.


However, the backend stores the IP:port pair that we --advertise’d when we launched the Swarm Agents and, while the IPs are perfectly fine, the port numbers are not the port number of a Weave Router (6783) but of a Weave Proxy (12375), so Discovery would tell the local Router to CONNECT to a Proxy… and that would just fail.

For this reason, we will launch Weave Discovery with the --discovered-port flag that will  replace every port discovered in the backend for 6783:

vm1$ discovery join --discovered-port=6783 token://6856663cdefdec325839a4b7e1de38e8<br>

And you are done! Now you should have a cluster that is connected to a Weave Network.

As we explained in our previous article, you would probably be better served by a persistent backend like etcd or Consul when working on stable infrastructures. You would then start your nodes with weave launch ..., swarm join etcd://... and discovery join etcd://..., pointing to the right ports and so on.