<commit_before>---
layout: guides
title: "Creating Distributed Apps with Weave and Docker"
permalink: /guides/weave-and-docker-platform/machine-and-swarm-with-weave-proxy.html

tags: docker, machine, swarm, cli, virtualbox, dns, ipam, proxy, hello-weave-app
---

### ***Using Weave with Docker Machine and Swarm***

> - Part 1: [Launching Weave Net with Docker Machine][ch1]
> - **Part 2: Using Weave with Docker Machine and Swarm**
> - Part 3: [Creating and Scaling Multi-host Docker Deployment with Swarm and Compose using Weave][ch3]

## What You Will Build

In this example, we show how Weave works seamlessly with tools like [Docker Machine](https://docs.docker.com/machine/) and [Docker Swarm](https://docs.docker.com/swarm/).

[Docker Swarm](http://docs.docker.com/swarm/) is a native clustering environment for its Docker engines and it turns a pool of Docker engine into a single, virtual host. [Docker Machine](https://docs.docker.com/machine/) allows you to easily create the Docker hosts (VMs) on your computer, on cloud providers or inside your own data center. With a few commands, it creates servers, installs Docker on them, and then it configures the Docker client to talk to them.

For this tutorial we configure a Docker Swarm cluster and then we make its contents easily discoverable with Weave Net. One advantage to deploying a Weave network within this type of topology is that it is agnostic to the way in which Docker Swarm is organized. Instead Weave deploys a standard network, and it discovers hosts through DNS, so that you can easily manage your distributed containerized apps without the need to deploy any additional services or software.

Weave also enhances the scalability of a Swarm cluster and doesn't rely on experimental builds nor does it require additional containers such as Ambassador containers to deploy and specify a TCP/IP network.

Here we describe how to get started with Swarm and Weave, in the next section you will proceed to a more advanced setup using Docker Compose. 

Specifically in this tutorial, you will: 

  1. Set up a simple Docker Swarm cluster using 3 virtual hosts.
  2. Deploy the Weave Network and discover hosts through DNS.
  3. Deploy a sample app to test that hosts are communicating within the Docker Swarm

This example requires no programming, but does require basic UNIX skills. It will take approximately 15-25 minutes to complete this tutorial. 

## What you will use

  - [Weave](http://weave.works)
  - [Docker, Swarm & Machine](http://docker.com)

## What you will need to complete this chapter

If you are using OSX or Windows, you can install docker, docker machine, virtualbox, compose (OSX only) and kitematix using [Docker Toolbox](https://www.docker.com/toolbox).

For other operating systems, you will need to install and configure the following separately before proceeding:

  - [`docker-machine`](http://docs.docker.com/machine/#installation) binary (_`>= 0.2.0`_)
  - [`docker`](https://docs.docker.com/installation/#installation) binary, at lest the client (_`>= v1.6.x`_)
  - [VirtualBox](https://www.virtualbox.org/wiki/Downloads) (_`>= 4.3.x`_)
  - `curl` (_any version_)

_If you completed [Part one of this tutorial][ch1], you should already have all of these dependencies installed._

## Let's go!

Note that the first several sections of this guide describes how to provision the cluster on the command line manually. If you want to jump ahead and see Weave in action right away, then refer to "Putting It All Together Below" where several scripts are provided which automates the whole process.

First, select the number of VMs to provision. This example limits the number of VMs to 3 to demonstrate Weave in a cluster environment, while at the same time still run comfortably on most laptops. In a production setting however, you can have any number of hosts in a  Docker Swarm and connect them using Weave. 

We will refer to these VM names throughout this tutorial:

  - `weave-1`
  - `weave-2`
  - `weave-3`

### Setting up the Swarm

Among the 3 VMs to be provisioned, chose one that will act as the Swarm master. In this example, we refer to `weave-1` as the head or the bootstrap node. Keep in mind that Weave has no specific knowledge of a Swarm master and its agents, and you can deploy your network in whatever topology you choose. 

But for the pursposes of this tutorial, `weave-1` will act as the bootstrap node where it will provide the initial configuration information to newly joining nodes, in this case, `weave-2` and `weave-3`. 

The workflow is as follows:

  1. create `weave-1` as a Swarm master
  2. create `weave-2` as a Swarm agent
  3. create `weave-3` as a Swarm agent
  4. Generate a Discovery Swarm token, which is the unique cluster id, as described in the [Docker Swarm Documentation](https://docs.docker.com/swarm/install-w-machine/)
  
  
  >>*Note:* In Weave there is no notion of master/slave or any other roles of the nodes. Here we simply
  >>picked `weave-1` as a sort of bootstrap node, but we could also pass all IPs or DNS names to `weave launch`
  >>and thereby also avoid having to set <code>-initpeercount</code> explicitly. However, with Docker Machine on VirtualBox
  >>we do not know the IP addresses and don't have DNS. You should be able to use DNS with one of the cloud drivers, such as
  >>Microsoft Azure or Google Compute Engine.

  >>To obtain the discovery swarm token, and create the VMs, a sample implementation script is provided: [`scripts/1-machine-create.sh`][step1].

### Launching Weave

Next launch Weave onto each of the virtual machines.

The IP addresses of all the peers are not known ahead of time, so you will need to pass `--init-peer-count 3` to `weave launch`. 
`--init-peer-count` is set to 3, as we are specifying a cluster of 3 VMs. 

In this setup, Weave-1 is the bootstrap node, and so its target should be set to 0. Only weave-2 and weave-3 need to have  `--init-peer-count` set to 3 at launch.

On each host, except for `weave-1` which is our bootstrap node we will need to:

  1. launch Weave router with `--init-peer-count 3`
  3. launch proxy with a copy of TLS flags from Docker daemon
  4. Connect the host to weave-1

On weave-1 run: 

~~~bash
weave launch
~~~

Set the weave env for `weave-1`

~~~bash
eval "$(weave env)"
~~~

Next launch weave on `weave-2` with --init-peer-count set to 3:

~~~bash
weave launch-router --init-peer-count 3
~~~

Next, specify the TLS settings if asked: 

~~~bash
weave launch-proxy --tls \
         --tlscacert /var/lib/boot2docker/ca.pem \
         --tlscert /var/lib/boot2docker/server.pem \
         --tlskey /var/lib/boot2docker/server-key.pem
~~~

and  then set the weave env for and `weave-2`

~~~bash
eval "$(weave env)"
~~~

Next on `weave-2` connect the cluster to our bootstrap node, `weave-1`: 

~~~bash
weave connect $(docker-machine ip weave-1)
~~~

check to see that all went well: 

~~~bash
weave status
~~~

Follow the same steps for `weave-3` as you did for `weave-2` above. 


  >>A useful script which launches weave and sets up the hosts and connects the cluster can be found here: [`scripts/2-weave-launch.sh`][step2].


### Setting up Swarm agents against Weave proxy

This step is more involved, and is a work-around to reported Docker Machine issue, which we will refrain from covering in detail. For more information, refer to [issue in Docker Machine](https://github.com/docker/machine/issues/1334). In short, swarm agents are restarted with a new discovery token and registered to TCP port 12375 for Weave Proxy. More information can be found in the following script [`scripts/3-replace-swarm-agents.sh`][step3].

### Put it all together

First clone the `Guides` repository:

    git clone https://github.com/weaveworks/guides weaveworks-guides
    cd ./weaveworks-guides/weave-and-docker-platform/scripts

Now that we understand how this is all provisioned, you can run all 3 scripts:

    ./1-machine-create.sh
    ./2-weave-launch.sh
    ./3-replace-swarm-agents.sh

Once done, check that everything is running properly:<commit_msg>minor edits to prt 2<commit_after>---
layout: guides
title: "Creating Distributed Apps with Weave and Docker"
permalink: /guides/weave-and-docker-platform/machine-and-swarm-with-weave-proxy.html

tags: docker, machine, swarm, cli, virtualbox, dns, ipam, proxy, hello-weave-app
---

### ***Using Weave with Docker Machine and Swarm***

> - Part 1: [Launching Weave Net with Docker Machine][ch1]
> - **Part 2: Using Weave with Docker Machine and Swarm**
> - Part 3: [Creating and Scaling Multi-host Docker Deployment with Swarm and Compose using Weave][ch3]

## What You Will Build

In this example, we show how Weave works seamlessly with tools like [Docker Machine](https://docs.docker.com/machine/) and [Docker Swarm](https://docs.docker.com/swarm/).

[Docker Swarm](http://docs.docker.com/swarm/) is a native clustering environment for its Docker engines and it turns a pool of Docker engine into a single, virtual host. [Docker Machine](https://docs.docker.com/machine/) allows you to easily create the Docker hosts (VMs) on your computer, on cloud providers or inside your own data center. With a few commands, it creates servers, installs Docker on them, and then it configures the Docker client to talk to them.

For this tutorial we configure a Docker Swarm cluster and then we make its contents easily discoverable with Weave Net. One advantage to deploying a Weave network within this type of topology is that it is agnostic to the way in which Docker Swarm is organized. Instead Weave deploys a standard network, and it discovers hosts through DNS, so that you can easily manage your distributed containerized apps without the need to deploy any additional services or software.

Weave also enhances the scalability of a Swarm cluster and doesn't rely on experimental builds nor does it require additional containers such as Ambassador containers to deploy and specify a TCP/IP network.

Here we describe how to get started with Swarm and Weave, in the next section[ch3] you will proceed to a more advanced setup using Docker Compose. 

Specifically in this tutorial, you will: 

  1. Set up a simple Docker Swarm cluster using 3 virtual hosts.
  2. Deploy the Weave Network and discover hosts through DNS.
  3. Deploy a sample app to test that hosts are communicating within the Docker Swarm

This example requires no programming, but does require basic UNIX skills. It will take approximately 15-25 minutes to complete this tutorial. 

## What you will use

  - [Weave](http://weave.works)
  - [Docker, Swarm & Machine](http://docker.com)

## What you will need to complete this chapter

If you are using OSX or Windows, you can install docker, docker machine, virtualbox, compose (OSX only) and kitematix using [Docker Toolbox](https://www.docker.com/toolbox).

For other operating systems, you will need to install and configure the following separately before proceeding:

  - [`docker-machine`](http://docs.docker.com/machine/#installation) binary (_`>= 0.2.0`_)
  - [`docker`](https://docs.docker.com/installation/#installation) binary, at lest the client (_`>= v1.6.x`_)
  - [VirtualBox](https://www.virtualbox.org/wiki/Downloads) (_`>= 4.3.x`_)
  - `curl` (_any version_)

_If you completed [Part one of this tutorial][ch1], you should already have all of these dependencies installed._

## Let's go!

Note that the first several sections of this guide describes how to provision the cluster on the command line manually. If you want to jump ahead and see Weave in action right away, then refer to "Putting It All Together Below" where several scripts are provided which automates the whole process.

First, select the number of VMs to provision. This example limits the number of VMs to 3 to demonstrate Weave in a cluster environment, while at the same time still run comfortably on most laptops. In a production setting however, you can have any number of hosts in a  Docker Swarm and connect them using Weave. 

We will refer to these VM names throughout this tutorial:

  - `weave-1`
  - `weave-2`
  - `weave-3`

### Setting up the Swarm

Among the 3 VMs to be provisioned, chose one that will act as the Swarm master. In this example, we refer to `weave-1` as the head or the bootstrap node. Keep in mind that Weave has no specific knowledge of a Swarm master and its agents, and you can deploy your network in whatever topology you choose. 

But for the pursposes of this tutorial, `weave-1` will act as the bootstrap node where it will provide the initial configuration information to newly joining nodes, in this case, `weave-2` and `weave-3`. 

The workflow is as follows:

  1. create `weave-1` as a Swarm master
  2. create `weave-2` as a Swarm agent
  3. create `weave-3` as a Swarm agent
  4. Generate a Discovery Swarm token, which is the unique cluster id, as described in the [Docker Swarm Documentation](https://docs.docker.com/swarm/install-w-machine/)
  
  
  >>*Note:* In Weave there is no notion of master/slave or any other roles of the nodes. Here we simply
  >>picked `weave-1` as a sort of bootstrap node, but we could also pass all IPs or DNS names to `weave launch`
  >>and thereby also avoid having to set <code>-initpeercount</code> explicitly. However, with Docker Machine on VirtualBox
  >>we do not know the IP addresses and don't have DNS. You should be able to use DNS with one of the cloud drivers, such as
  >>Microsoft Azure or Google Compute Engine.



  >>To obtain the discovery swarm token, and create the VMs, a sample implementation script is provided: [`scripts/1-machine-create.sh`][step1].

### Launching Weave

Next launch Weave onto each of the virtual machines.

The IP addresses of all the peers are not known ahead of time, so you will need to pass `--init-peer-count 3` to `weave launch`. 
`--init-peer-count` is set to 3, as we are specifying a cluster of 3 VMs. 

In this setup, `weave-1` is the bootstrap node, and so its target should be 0. Only `weave-2` and `weave-3` need to have  `--init-peer-count` set to 3 at launch.

On each host, except for `weave-1` which is our bootstrap node we will need to:

  1. launch Weave router with `--init-peer-count 3`
  3. launch proxy with a copy of TLS flags from Docker daemon
  4. Connect the host to weave-1

On weave-1 run: 

~~~bash
weave launch
~~~

Set the weave env for `weave-1`

~~~bash
eval "$(weave env)"
~~~

Next launch weave on `weave-2` with --init-peer-count set to 3:

~~~bash
weave launch-router --init-peer-count 3
~~~

Next, specify the TLS settings if asked: 

~~~bash
weave launch-proxy --tls \
         --tlscacert /var/lib/boot2docker/ca.pem \
         --tlscert /var/lib/boot2docker/server.pem \
         --tlskey /var/lib/boot2docker/server-key.pem
~~~

and then set the weave environment variable for and `weave-2`

~~~bash
eval "$(weave env)"
~~~

Next on `weave-2` connect the cluster to our bootstrap node, `weave-1`: 

~~~bash
weave connect $(docker-machine ip weave-1)
~~~

check to see that all went well: 

~~~bash
weave status
~~~

Follow the same steps for `weave-3` as you did for `weave-2` above. 


  >>A useful script which launches weave and sets up the hosts and connects the cluster can be found here: [`scripts/2-weave-launch.sh`][step2].


### Setting up Swarm agents against Weave proxy

This step is more involved, and is a work-around to reported Docker Machine issue, which we will refrain from covering in detail. For more information, refer to [issue in Docker Machine](https://github.com/docker/machine/issues/1334). In short, swarm agents are restarted with a new discovery token and registered to TCP port 12375 for Weave Proxy. More information can be found in the following script [`scripts/3-replace-swarm-agents.sh`][step3].

### Put it all together

First clone the `Guides` repository:

    git clone https://github.com/weaveworks/guides weaveworks-guides
    cd ./weaveworks-guides/weave-and-docker-platform/scripts

Now that we understand how this is all provisioned, you can automate the whole process by running these 3 scripts:

    ./1-machine-create.sh
    ./2-weave-launch.sh
    ./3-replace-swarm-agents.sh

Once done, check that everything is running properly:
{'fraction_non_alphanumeric': 0.08242469044538903, 'fraction_numerical': 0.010595700117045524, 'mean_word_length': 3.8838748495788207, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 47, 'https://': 18, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6983807', 'n_tokens_mistral': 4859, 'n_tokens_neox': 4518, 'n_words': 2305}