Updating Docker output and curl for sticky sessions

This commit is contained in:
Jiri Tyr 2017-09-22 16:22:03 +01:00 committed by Traefiker
parent 058fa1367b
commit dd23ceeead

View file

@ -7,14 +7,15 @@ The cluster consists of:
- 3 servers - 3 servers
- 1 manager - 1 manager
- 2 workers - 2 workers
- 1 [overlay](https://docs.docker.com/engine/userguide/networking/dockernetworks/#an-overlay-network) network - 1 [overlay](https://docs.docker.com/engine/userguide/networking/dockernetworks/#an-overlay-network) network (multi-host networking)
(multi-host networking)
## Prerequisites ## Prerequisites
1. You will need to install [docker-machine](https://docs.docker.com/machine/) 1. You will need to install [docker-machine](https://docs.docker.com/machine/)
2. You will need the latest [VirtualBox](https://www.virtualbox.org/wiki/Downloads) 2. You will need the latest [VirtualBox](https://www.virtualbox.org/wiki/Downloads)
## Cluster provisioning ## Cluster provisioning
First, let's create all the required nodes. First, let's create all the required nodes.
@ -26,7 +27,7 @@ docker-machine create -d virtualbox worker1
docker-machine create -d virtualbox worker2 docker-machine create -d virtualbox worker2
``` ```
Then, let's setup the cluster, in order : Then, let's setup the cluster, in order:
1. initialize the cluster 1. initialize the cluster
1. get the token for other host to join 1. get the token for other host to join
@ -60,9 +61,9 @@ docker-machine ssh manager docker node ls
``` ```
``` ```
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
2a770ov9vixeadep674265u1n worker1 Ready Active 013v16l1sbuwjqcn7ucbu4jwt worker1 Ready Active
dbi3or4q8ii8elbws70g4hkdh * manager Ready Active Leader 8buzkquycd17jqjber0mo2gn8 worker2 Ready Active
esbhhy6vnqv90xomjaomdgy46 worker2 Ready Active fnpj8ozfc85zvahx2r540xfcf * manager Ready Active Leader
``` ```
Finally, let's create a network for Træfik to use. Finally, let's create a network for Træfik to use.
@ -71,11 +72,11 @@ Finally, let's create a network for Træfik to use.
docker-machine ssh manager "docker network create --driver=overlay traefik-net" docker-machine ssh manager "docker network create --driver=overlay traefik-net"
``` ```
## Deploy Træfik ## Deploy Træfik
Let's deploy Træfik as a docker service in our cluster. Let's deploy Træfik as a docker service in our cluster.
The only requirement for Træfik to work with swarm mode is that it needs to run on a manager node — we are going to use a The only requirement for Træfik to work with swarm mode is that it needs to run on a manager node - we are going to use a [constraint](https://docs.docker.com/engine/reference/commandline/service_create/#/specify-service-constraints-constraint) for that.
[constraint](https://docs.docker.com/engine/reference/commandline/service_create/#/specify-service-constraints-constraint) for that.
```shell ```shell
docker-machine ssh manager "docker service create \ docker-machine ssh manager "docker service create \
@ -103,6 +104,7 @@ Let's explain this command:
| `--docker` | enable docker backend, and `--docker.swarmmode` to enable the swarm mode on Træfik. | | `--docker` | enable docker backend, and `--docker.swarmmode` to enable the swarm mode on Træfik. |
| `--web` | activate the webUI on port 8080 | | `--web` | activate the webUI on port 8080 |
## Deploy your apps ## Deploy your apps
We can now deploy our app on the cluster, here [whoami](https://github.com/emilevauge/whoami), a simple web server in Go. We can now deploy our app on the cluster, here [whoami](https://github.com/emilevauge/whoami), a simple web server in Go.
@ -124,7 +126,7 @@ docker-machine ssh manager "docker service create \
``` ```
!!! note !!! note
We set whoami1 to use sticky sessions (`--label traefik.backend.loadbalancer.sticky=true`). We set `whoami1` to use sticky sessions (`--label traefik.backend.loadbalancer.sticky=true`).
We'll demonstrate that later. We'll demonstrate that later.
!!! note !!! note
@ -136,55 +138,52 @@ Check that everything is scheduled and started:
docker-machine ssh manager "docker service ls" docker-machine ssh manager "docker service ls"
``` ```
``` ```
ID NAME REPLICAS IMAGE COMMAND ID NAME MODE REPLICAS IMAGE PORTS
ab046gpaqtln whoami0 1/1 emilevauge/whoami moq3dq4xqv6t traefik replicated 1/1 traefik:latest *:80->80/tcp,*:8080->8080/tcp
cgfg5ifzrpgm whoami1 1/1 emilevauge/whoami ysil6oto1wim whoami0 replicated 1/1 emilevauge/whoami:latest
dtpl249tfghc traefik 1/1 traefik --docker --docker.swarmmode --docker.domain=traefik --docker.watch --web z9re2mnl34k4 whoami1 replicated 1/1 emilevauge/whoami:latest
``` ```
## Access to your apps through Træfik ## Access to your apps through Træfik
```shell ```shell
curl -H Host:whoami0.traefik http://$(docker-machine ip manager) curl -H Host:whoami0.traefik http://$(docker-machine ip manager)
``` ```
```yaml ```yaml
Hostname: 8147a7746e7a Hostname: 5b0b3d148359
IP: 127.0.0.1 IP: 127.0.0.1
IP: ::1 IP: 10.0.0.8
IP: 10.0.9.3 IP: 10.0.0.4
IP: fe80::42:aff:fe00:903 IP: 172.18.0.5
IP: 172.18.0.3
IP: fe80::42:acff:fe12:3
GET / HTTP/1.1 GET / HTTP/1.1
Host: 10.0.9.3:80 Host: whoami0.traefik
User-Agent: curl/7.35.0 User-Agent: curl/7.55.1
Accept: */* Accept: */*
Accept-Encoding: gzip Accept-Encoding: gzip
X-Forwarded-For: 192.168.99.1 X-Forwarded-For: 10.255.0.2
X-Forwarded-Host: 10.0.9.3:80 X-Forwarded-Host: whoami0.traefik
X-Forwarded-Proto: http X-Forwarded-Proto: http
X-Forwarded-Server: 8fbc39271b4c X-Forwarded-Server: 77fc29c69fe4
``` ```
```shell ```shell
curl -H Host:whoami1.traefik http://$(docker-machine ip manager) curl -H Host:whoami1.traefik http://$(docker-machine ip manager)
``` ```
```yaml ```yaml
Hostname: ba2c21488299 Hostname: 3633163970f6
IP: 127.0.0.1 IP: 127.0.0.1
IP: ::1 IP: 10.0.0.14
IP: 10.0.9.4 IP: 10.0.0.6
IP: fe80::42:aff:fe00:904 IP: 172.18.0.5
IP: 172.18.0.2
IP: fe80::42:acff:fe12:2
GET / HTTP/1.1 GET / HTTP/1.1
Host: 10.0.9.4:80 Host: whoami1.traefik
User-Agent: curl/7.35.0 User-Agent: curl/7.55.1
Accept: */* Accept: */*
Accept-Encoding: gzip Accept-Encoding: gzip
X-Forwarded-For: 192.168.99.1 X-Forwarded-For: 10.255.0.2
X-Forwarded-Host: 10.0.9.4:80 X-Forwarded-Host: whoami1.traefik
X-Forwarded-Proto: http X-Forwarded-Proto: http
X-Forwarded-Server: 8fbc39271b4c X-Forwarded-Server: 77fc29c69fe4
``` ```
!!! note !!! note
@ -194,43 +193,39 @@ X-Forwarded-Server: 8fbc39271b4c
curl -H Host:whoami0.traefik http://$(docker-machine ip worker1) curl -H Host:whoami0.traefik http://$(docker-machine ip worker1)
``` ```
```yaml ```yaml
Hostname: 8147a7746e7a Hostname: 5b0b3d148359
IP: 127.0.0.1 IP: 127.0.0.1
IP: ::1 IP: 10.0.0.8
IP: 10.0.9.3 IP: 10.0.0.4
IP: fe80::42:aff:fe00:903 IP: 172.18.0.5
IP: 172.18.0.3
IP: fe80::42:acff:fe12:3
GET / HTTP/1.1 GET / HTTP/1.1
Host: 10.0.9.3:80 Host: whoami0.traefik
User-Agent: curl/7.35.0 User-Agent: curl/7.55.1
Accept: */* Accept: */*
Accept-Encoding: gzip Accept-Encoding: gzip
X-Forwarded-For: 192.168.99.1 X-Forwarded-For: 10.255.0.3
X-Forwarded-Host: 10.0.9.3:80 X-Forwarded-Host: whoami0.traefik
X-Forwarded-Proto: http X-Forwarded-Proto: http
X-Forwarded-Server: 8fbc39271b4c X-Forwarded-Server: 77fc29c69fe4
``` ```
```shell ```shell
curl -H Host:whoami1.traefik http://$(docker-machine ip worker2) curl -H Host:whoami1.traefik http://$(docker-machine ip worker2)
``` ```
```yaml ```yaml
Hostname: ba2c21488299 Hostname: 3633163970f6
IP: 127.0.0.1 IP: 127.0.0.1
IP: ::1 IP: 10.0.0.14
IP: 10.0.9.4 IP: 10.0.0.6
IP: fe80::42:aff:fe00:904 IP: 172.18.0.5
IP: 172.18.0.2
IP: fe80::42:acff:fe12:2
GET / HTTP/1.1 GET / HTTP/1.1
Host: 10.0.9.4:80 Host: whoami1.traefik
User-Agent: curl/7.35.0 User-Agent: curl/7.55.1
Accept: */* Accept: */*
Accept-Encoding: gzip Accept-Encoding: gzip
X-Forwarded-For: 192.168.99.1 X-Forwarded-For: 10.255.0.4
X-Forwarded-Host: 10.0.9.4:80 X-Forwarded-Host: whoami1.traefik
X-Forwarded-Proto: http X-Forwarded-Proto: http
X-Forwarded-Server: 8fbc39271b4c X-Forwarded-Server: 77fc29c69fe4
``` ```
## Scale both services ## Scale both services
@ -246,79 +241,93 @@ Check that we now have 5 replicas of each `whoami` service:
docker-machine ssh manager "docker service ls" docker-machine ssh manager "docker service ls"
``` ```
``` ```
ID NAME REPLICAS IMAGE COMMAND ID NAME MODE REPLICAS IMAGE PORTS
ab046gpaqtln whoami0 5/5 emilevauge/whoami moq3dq4xqv6t traefik replicated 1/1 traefik:latest *:80->80/tcp,*:8080->8080/tcp
cgfg5ifzrpgm whoami1 5/5 emilevauge/whoami ysil6oto1wim whoami0 replicated 5/5 emilevauge/whoami:latest
dtpl249tfghc traefik 1/1 traefik --docker --docker.swarmmode --docker.domain=traefik --docker.watch --web z9re2mnl34k4 whoami1 replicated 5/5 emilevauge/whoami:latest
``` ```
## Access to your whoami0 through Træfik multiple times.
## Access to your `whoami0` through Træfik multiple times.
Repeat the following command multiple times and note that the Hostname changes each time as Traefik load balances each request against the 5 tasks: Repeat the following command multiple times and note that the Hostname changes each time as Traefik load balances each request against the 5 tasks:
```shell ```shell
curl -H Host:whoami0.traefik http://$(docker-machine ip manager) curl -H Host:whoami0.traefik http://$(docker-machine ip manager)
``` ```
```yaml ```yaml
Hostname: 8147a7746e7a Hostname: f3138d15b567
IP: 127.0.0.1 IP: 127.0.0.1
IP: ::1 IP: 10.0.0.5
IP: 10.0.9.3 IP: 10.0.0.4
IP: fe80::42:aff:fe00:903
IP: 172.18.0.3 IP: 172.18.0.3
IP: fe80::42:acff:fe12:3
GET / HTTP/1.1 GET / HTTP/1.1
Host: 10.0.9.3:80 Host: whoami0.traefik
User-Agent: curl/7.35.0 User-Agent: curl/7.55.1
Accept: */* Accept: */*
Accept-Encoding: gzip Accept-Encoding: gzip
X-Forwarded-For: 192.168.99.1 X-Forwarded-For: 10.255.0.2
X-Forwarded-Host: 10.0.9.3:80 X-Forwarded-Host: whoami0.traefik
X-Forwarded-Proto: http X-Forwarded-Proto: http
X-Forwarded-Server: 8fbc39271b4c X-Forwarded-Server: 77fc29c69fe4
``` ```
Do the same against whoami1: Do the same against `whoami1`:
```shell ```shell
curl -H Host:whoami1.traefik http://$(docker-machine ip manager) curl -c cookies.txt -H Host:whoami1.traefik http://$(docker-machine ip manager)
``` ```
```yaml ```yaml
Hostname: ba2c21488299 Hostname: 348e2f7bf432
IP: 127.0.0.1 IP: 127.0.0.1
IP: ::1 IP: 10.0.0.15
IP: 10.0.9.4 IP: 10.0.0.6
IP: fe80::42:aff:fe00:904 IP: 172.18.0.6
IP: 172.18.0.2
IP: fe80::42:acff:fe12:2
GET / HTTP/1.1 GET / HTTP/1.1
Host: 10.0.9.4:80 Host: whoami1.traefik
User-Agent: curl/7.35.0 User-Agent: curl/7.55.1
Accept: */* Accept: */*
Accept-Encoding: gzip Accept-Encoding: gzip
X-Forwarded-For: 192.168.99.1 X-Forwarded-For: 10.255.0.2
X-Forwarded-Host: 10.0.9.4:80 X-Forwarded-Host: whoami1.traefik
X-Forwarded-Proto: http X-Forwarded-Proto: http
X-Forwarded-Server: 8fbc39271b4c X-Forwarded-Server: 77fc29c69fe4
``` ```
Wait, I thought we added the sticky flag to `whoami1`? Because the sticky sessions require cookies to work, we used the `-c cookies.txt` option to store the cookie into a file.
Traefik relies on a cookie to maintain stickyness so you'll need to test this with a browser. The cookie contains the IP of the container to which the session sticks:
First you need to add `whoami1.traefik` to your hosts file:
```shell ```shell
if [ -n "$(grep whoami1.traefik /etc/hosts)" ]; cat ./cookies.txt
then ```
echo "whoami1.traefik already exists (make sure the ip is current)"; ```
else # Netscape HTTP Cookie File
sudo -- sh -c -e "echo '$(docker-machine ip manager)\twhoami1.traefik' >> /etc/hosts"; # https://curl.haxx.se/docs/http-cookies.html
fi # This file was generated by libcurl! Edit at your own risk.
whoami1.traefik FALSE / FALSE 0 _TRAEFIK_BACKEND http://10.0.0.15:80
``` ```
Now open your browser and go to http://whoami1.traefik/ If you load the cookies file (`-b cookies.txt`) for the next request, you will see that stickyness is maintained:
You will now see that stickyness is maintained. ```shell
curl -b cookies.txt -H Host:whoami1.traefik http://$(docker-machine ip manager)
```
```yaml
Hostname: 348e2f7bf432
IP: 127.0.0.1
IP: 10.0.0.15
IP: 10.0.0.6
IP: 172.18.0.6
GET / HTTP/1.1
Host: whoami1.traefik
User-Agent: curl/7.55.1
Accept: */*
Accept-Encoding: gzip
Cookie: _TRAEFIK_BACKEND=http://10.0.0.15:80
X-Forwarded-For: 10.255.0.2
X-Forwarded-Host: whoami1.traefik
X-Forwarded-Proto: http
X-Forwarded-Server: 77fc29c69fe4
```
![](https://i.giphy.com/ujUdrdpX7Ok5W.gif) ![](https://i.giphy.com/ujUdrdpX7Ok5W.gif)