Swarm cluster

This section explains how to create a multi-host swarm cluster using docker-machine and how to deploy Træfɪk on it. The cluster consists of:

  • 2 servers
  • 1 swarm master
  • 2 swarm nodes
  • 1 overlay network (multi-host networking)

Prerequisites

Cluster provisioning

We first follow this guide to create the cluster.

Create machine mh-keystore

This machine is the service registry of our cluster.

  1. docker-machine create -d virtualbox mh-keystore

Then we install the service registry Consul on this machine:

  1. eval "$(docker-machine env mh-keystore)"
  2. docker run -d \
  3. -p "8500:8500" \
  4. -h "consul" \
  5. progrium/consul -server -bootstrap

Create machine mhs-demo0

This machine is a swarm master and a swarm agent on it.

  1. docker-machine create -d virtualbox \
  2. --swarm --swarm-master \
  3. --swarm-discovery="consul://$(docker-machine ip mh-keystore):8500" \
  4. --engine-opt="cluster-store=consul://$(docker-machine ip mh-keystore):8500" \
  5. --engine-opt="cluster-advertise=eth1:2376" \
  6. mhs-demo0

Create machine mhs-demo1

This machine have a swarm agent on it.

  1. docker-machine create -d virtualbox \
  2. --swarm \
  3. --swarm-discovery="consul://$(docker-machine ip mh-keystore):8500" \
  4. --engine-opt="cluster-store=consul://$(docker-machine ip mh-keystore):8500" \
  5. --engine-opt="cluster-advertise=eth1:2376" \
  6. mhs-demo1

Create the overlay Network

Create the overlay network on the swarm master:

  1. eval $(docker-machine env --swarm mhs-demo0)
  2. docker network create --driver overlay --subnet=10.0.9.0/24 my-net

Deploy Træfɪk

Deploy Træfɪk:

  1. docker $(docker-machine config mhs-demo0) run \
  2. -d \
  3. -p 80:80 -p 8080:8080 \
  4. --net=my-net \
  5. -v /var/lib/boot2docker/:/ssl \
  6. traefik \
  7. -l DEBUG \
  8. -c /dev/null \
  9. --docker \
  10. --docker.domain=traefik \
  11. --docker.endpoint=tcp://$(docker-machine ip mhs-demo0):3376 \
  12. --docker.tls \
  13. --docker.tls.ca=/ssl/ca.pem \
  14. --docker.tls.cert=/ssl/server.pem \
  15. --docker.tls.key=/ssl/server-key.pem \
  16. --docker.tls.insecureSkipVerify \
  17. --docker.watch \
  18. --web

Let's explain this command:

  • -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
  • —docker: enable docker backend
  • —docker.endpoint=tcp://172.18.0.1:3376: connect to the swarm master using the docker_gwbridge network
  • —docker.tls: enable TLS using the docker-machine keys
  • —web: activate the webUI on port 8080

Deploy your apps

We can now deploy our app on the cluster, here whoami, a simple web server in GO, on the network my-net:

  1. eval $(docker-machine env --swarm mhs-demo0)
  2. docker run -d --name=whoami0 --net=my-net --env="constraint:node==mhs-demo0" emilevauge/whoami
  3. docker run -d --name=whoami1 --net=my-net --env="constraint:node==mhs-demo1" emilevauge/whoami

Check that everything is started:

  1. docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. ba2c21488299 emilevauge/whoami "/whoamI" 8 seconds ago Up 9 seconds 80/tcp mhs-demo1/whoami1
  4. 8147a7746e7a emilevauge/whoami "/whoamI" 19 seconds ago Up 20 seconds 80/tcp mhs-demo0/whoami0
  5. 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

Access to your apps through Træfɪk

  1. curl -H Host:whoami0.traefik http://$(docker-machine ip mhs-demo0)
  2. Hostname: 8147a7746e7a
  3. IP: 127.0.0.1
  4. IP: ::1
  5. IP: 10.0.9.3
  6. IP: fe80::42:aff:fe00:903
  7. IP: 172.18.0.3
  8. IP: fe80::42:acff:fe12:3
  9. GET / HTTP/1.1
  10. Host: 10.0.9.3:80
  11. User-Agent: curl/7.35.0
  12. Accept: */*
  13. Accept-Encoding: gzip
  14. X-Forwarded-For: 192.168.99.1
  15. X-Forwarded-Host: 10.0.9.3:80
  16. X-Forwarded-Proto: http
  17. X-Forwarded-Server: 8fbc39271b4c
  18. curl -H Host:whoami1.traefik http://$(docker-machine ip mhs-demo0)
  19. Hostname: ba2c21488299
  20. IP: 127.0.0.1
  21. IP: ::1
  22. IP: 10.0.9.4
  23. IP: fe80::42:aff:fe00:904
  24. IP: 172.18.0.2
  25. IP: fe80::42:acff:fe12:2
  26. GET / HTTP/1.1
  27. Host: 10.0.9.4:80
  28. User-Agent: curl/7.35.0
  29. Accept: */*
  30. Accept-Encoding: gzip
  31. X-Forwarded-For: 192.168.99.1
  32. X-Forwarded-Host: 10.0.9.4:80
  33. X-Forwarded-Proto: http
  34. X-Forwarded-Server: 8fbc39271b4c

Swarm 集群 - 图1