2016-04-15 14:27:40 +00:00
# Swarm cluster
2018-10-17 14:24:04 +00:00
This section explains how to create a multi-host [swarm ](https://docs.docker.com/swarm ) cluster using [docker-machine ](https://docs.docker.com/machine/ ) and how to deploy Traefik on it.
2017-09-11 17:10:04 +00:00
2016-10-06 13:48:23 +00:00
The cluster consists of:
2016-04-15 14:27:40 +00:00
- 2 servers
- 1 swarm master
- 2 swarm nodes
- 1 [overlay ](https://docs.docker.com/engine/userguide/networking/dockernetworks/#an-overlay-network ) network (multi-host networking)
## Prerequisites
2016-10-06 13:48:23 +00:00
1. You need to install [docker-machine ](https://docs.docker.com/machine/ )
2. You need the latest [VirtualBox ](https://www.virtualbox.org/wiki/Downloads )
2016-04-15 14:27:40 +00:00
## Cluster provisioning
2016-10-06 13:48:23 +00:00
We first follow [this guide ](https://docs.docker.com/engine/userguide/networking/get-started-overlay/ ) to create the cluster.
2016-04-15 14:27:40 +00:00
### Create machine `mh-keystore`
2016-10-06 13:48:23 +00:00
This machine is the service registry of our cluster.
2016-04-15 14:27:40 +00:00
2017-04-30 18:17:57 +00:00
```shell
2016-04-15 14:27:40 +00:00
docker-machine create -d virtualbox mh-keystore
```
Then we install the service registry [Consul ](https://consul.io ) on this machine:
2017-04-30 18:17:57 +00:00
```shell
2016-04-15 14:27:40 +00:00
eval "$(docker-machine env mh-keystore)"
docker run -d \
-p "8500:8500" \
-h "consul" \
progrium/consul -server -bootstrap
```
### Create machine `mhs-demo0`
2016-10-06 13:48:23 +00:00
This machine is a swarm master and a swarm agent on it.
2016-04-15 14:27:40 +00:00
2017-04-30 18:17:57 +00:00
```shell
2016-04-15 14:27:40 +00:00
docker-machine create -d virtualbox \
--swarm --swarm-master \
--swarm-discovery="consul://$(docker-machine ip mh-keystore):8500" \
--engine-opt="cluster-store=consul://$(docker-machine ip mh-keystore):8500" \
--engine-opt="cluster-advertise=eth1:2376" \
mhs-demo0
```
### Create machine `mhs-demo1`
2016-10-06 13:48:23 +00:00
This machine have a swarm agent on it.
2016-04-15 14:27:40 +00:00
2017-04-30 18:17:57 +00:00
```shell
2016-04-15 14:27:40 +00:00
docker-machine create -d virtualbox \
--swarm \
--swarm-discovery="consul://$(docker-machine ip mh-keystore):8500" \
--engine-opt="cluster-store=consul://$(docker-machine ip mh-keystore):8500" \
--engine-opt="cluster-advertise=eth1:2376" \
mhs-demo1
```
### Create the overlay Network
Create the overlay network on the swarm master:
2017-04-30 18:17:57 +00:00
```shell
2016-04-15 14:27:40 +00:00
eval $(docker-machine env --swarm mhs-demo0)
docker network create --driver overlay --subnet=10.0.9.0/24 my-net
```
2018-10-17 14:24:04 +00:00
## Deploy Traefik
2016-04-15 14:27:40 +00:00
2018-10-17 14:24:04 +00:00
Deploy Traefik:
2016-04-15 14:27:40 +00:00
2017-04-30 18:17:57 +00:00
```shell
2016-04-15 14:27:40 +00:00
docker $(docker-machine config mhs-demo0) run \
-d \
-p 80:80 -p 8080:8080 \
--net=my-net \
-v /var/lib/boot2docker/:/ssl \
traefik \
-l DEBUG \
-c /dev/null \
--docker \
2016-10-01 21:33:16 +00:00
--docker.domain=traefik \
2018-01-02 09:38:03 +00:00
--docker.endpoint=tcp://$(docker-machine ip mhs-demo0):2376 \
2016-04-15 14:27:40 +00:00
--docker.tls \
2016-10-01 21:33:16 +00:00
--docker.tls.ca=/ssl/ca.pem \
--docker.tls.cert=/ssl/server.pem \
--docker.tls.key=/ssl/server-key.pem \
2016-04-15 14:27:40 +00:00
--docker.tls.insecureSkipVerify \
2016-10-01 21:33:16 +00:00
--docker.watch \
2018-01-11 08:46:03 +00:00
--api
2016-04-15 14:27:40 +00:00
```
Let's explain this command:
2017-09-11 17:10:04 +00:00
| Option | Description |
|-------------------------------------------|---------------------------------------------------------------|
| `-p 80:80 -p 8080:8080` | we bind ports 80 and 8080 |
| `--net=my-net` | run the container on the network my-net |
| `-v /var/lib/boot2docker/:/ssl` | mount the ssl keys generated by docker-machine |
| `-c /dev/null` | empty config file |
2018-05-16 17:48:03 +00:00
| `--docker` | enable docker provider |
2018-01-02 09:38:03 +00:00
| `--docker.endpoint=tcp://172.18.0.1:2376` | connect to the swarm master using the docker_gwbridge network |
2017-09-11 17:10:04 +00:00
| `--docker.tls` | enable TLS using the docker-machine keys |
2018-01-11 08:46:03 +00:00
| `--api` | activate the webUI on port 8080 |
2017-09-11 17:10:04 +00:00
2016-04-15 14:27:40 +00:00
## Deploy your apps
2018-10-25 15:04:03 +00:00
We can now deploy our app on the cluster, here [whoami ](https://github.com/containous/whoami ), a simple web server in GO, on the network `my-net` :
2016-04-15 14:27:40 +00:00
2017-04-30 18:17:57 +00:00
```shell
2016-04-15 14:27:40 +00:00
eval $(docker-machine env --swarm mhs-demo0)
2018-10-25 15:04:03 +00:00
docker run -d --name=whoami0 --net=my-net --env="constraint:node==mhs-demo0" containous/whoami
docker run -d --name=whoami1 --net=my-net --env="constraint:node==mhs-demo1" containous/whoami
2016-04-15 14:27:40 +00:00
```
Check that everything is started:
2017-04-30 18:17:57 +00:00
```shell
2016-04-15 14:27:40 +00:00
docker ps
2017-09-05 13:58:03 +00:00
```
```
2016-04-15 14:27:40 +00:00
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2018-10-25 15:04:03 +00:00
ba2c21488299 containous/whoami "/whoamI" 8 seconds ago Up 9 seconds 80/tcp mhs-demo1/whoami1
8147a7746e7a containous/whoami "/whoamI" 19 seconds ago Up 20 seconds 80/tcp mhs-demo0/whoami0
2016-04-15 14:27:40 +00:00
8fbc39271b4c traefik "/traefik -l DEBUG -c" 36 seconds ago Up 37 seconds 192.168.99.101:80->80/tcp, 192.168.99.101:8080->8080/tcp mhs-demo0/serene_bhabha
```
2018-10-17 14:24:04 +00:00
## Access to your apps through Traefik
2016-04-15 14:27:40 +00:00
2017-04-30 18:17:57 +00:00
```shell
2016-04-15 14:27:40 +00:00
curl -H Host:whoami0.traefik http://$(docker-machine ip mhs-demo0)
2017-09-05 13:58:03 +00:00
```
```yaml
2016-04-15 14:27:40 +00:00
Hostname: 8147a7746e7a
IP: 127.0.0.1
IP: ::1
IP: 10.0.9.3
IP: fe80::42:aff:fe00:903
IP: 172.18.0.3
IP: fe80::42:acff:fe12:3
GET / HTTP/1.1
Host: 10.0.9.3:80
User-Agent: curl/7.35.0
Accept: */*
Accept-Encoding: gzip
X-Forwarded-For: 192.168.99.1
X-Forwarded-Host: 10.0.9.3:80
X-Forwarded-Proto: http
X-Forwarded-Server: 8fbc39271b4c
2017-09-05 13:58:03 +00:00
```
2016-04-15 14:27:40 +00:00
2017-09-05 13:58:03 +00:00
```shell
2016-04-15 14:27:40 +00:00
curl -H Host:whoami1.traefik http://$(docker-machine ip mhs-demo0)
2017-09-05 13:58:03 +00:00
```
```yaml
2016-04-15 14:27:40 +00:00
Hostname: ba2c21488299
IP: 127.0.0.1
IP: ::1
IP: 10.0.9.4
IP: fe80::42:aff:fe00:904
IP: 172.18.0.2
IP: fe80::42:acff:fe12:2
GET / HTTP/1.1
Host: 10.0.9.4:80
User-Agent: curl/7.35.0
Accept: */*
Accept-Encoding: gzip
X-Forwarded-For: 192.168.99.1
X-Forwarded-Host: 10.0.9.4:80
X-Forwarded-Proto: http
X-Forwarded-Server: 8fbc39271b4c
```
2018-07-12 16:26:03 +00:00
![GIF Magica ](https://i.giphy.com/ujUdrdpX7Ok5W.gif )