|
| 1 | +This repo contains useful things for playing with Docker Swarm Mode. |
| 2 | + |
| 3 | +There is automation here for running a swarm with multiple hosts on a single machine using containers as managers and workers. |
| 4 | + |
| 5 | +There are also several stack files which serve as a starting point for understanding the scheduler and playing with different use-cases. |
| 6 | + |
| 7 | + |
| 8 | +# Running a Swarm in Containers |
| 9 | +```bash |
| 10 | +cd swarm |
| 11 | +source start |
| 12 | +``` |
| 13 | +This exports some environment vars into your shell. |
| 14 | +If you are already part of a swarm, ignore the error. |
| 15 | +```bash |
| 16 | +docker stack up admin -c ../admin/docker-compose.yaml |
| 17 | +# alternatively, `cd ../admin && docker-compose up -d` |
| 18 | +``` |
| 19 | +In a few moments, you should see a visualization @ [localhost:8090](http://localhost:8090) showing your host and any swarm services. |
| 20 | +```bash |
| 21 | +docker-compose up -d worker |
| 22 | +# you should see 1 worker node join |
| 23 | +docker-compose scale worker=4 |
| 24 | +# 3 more should join in a few seconds |
| 25 | +``` |
| 26 | +You now have a multi-host swarm that you can play around in. |
| 27 | +For speed, configure your docker engine to use: |
| 28 | +```json |
| 29 | +"registry-mirrors": [ |
| 30 | + "http://localhost:5000" |
| 31 | +] |
| 32 | +``` |
| 33 | +When you're done, `cd swarm && ./kill`. |
| 34 | + |
| 35 | +## Some Helpful Commands |
| 36 | +``` |
| 37 | +export DOCKER_HIDE_LEGACY_COMMANDS=true |
| 38 | +docker --help |
| 39 | +docker node ls |
| 40 | +docker node --help |
| 41 | +docker stack ls |
| 42 | +docker stack --help |
| 43 | +docker service ls |
| 44 | +docker service --help |
| 45 | +docker service inspect <SERVICE> --pretty |
| 46 | +``` |
| 47 | + |
| 48 | +## How it Works |
| 49 | +The `start` script: |
| 50 | +- ensures a swarm exists |
| 51 | +- exports the join tokens as env vars |
| 52 | +- exports the `{{.Swarm.NodeAddr}}` as `$HOST_IP` |
| 53 | + |
| 54 | +`swarm/docker-compose.yaml` depends on these env vars. |
| 55 | +It contains two services for managers and workers. |
| 56 | +These services run **docker in docker** (`dind`) as priviledged containers on the host. |
| 57 | +The `entrypoint` is overridden to run an inline shell script that: |
| 58 | +- starts the container's `dockerd` |
| 59 | +- traps interrupts |
| 60 | +- waits until `docker info` succeeds |
| 61 | +- joins the host's swarm with the service's `$TOKEN` |
| 62 | + |
| 63 | + |
| 64 | +# Using Stacks |
| 65 | +Stack files allow you to use the [docker-compose schema](https://docs.docker.com/compose/compose-file/) to declare a desired state for many related services. |
| 66 | +These services will achieve their desired state through the swarm scheduler. |
| 67 | + |
| 68 | +This repo contains a few different stack files that demonstrate neat use cases. |
| 69 | + |
| 70 | +You can deploy a stack by running: |
| 71 | +```bash |
| 72 | +docker stack up -c stack_name/docker-compose.yaml stack_name |
| 73 | +``` |
| 74 | +You can change the file and run the command again to watch swarm remediate the state. |
| 75 | + |
| 76 | +The **admin** stack is recommended since it runs services that help you understand and speed up what you are doing. |
| 77 | + |
| 78 | +Try the **minio** stack if you want to play with a self-hosted, distributed, S3 compatible object store. |
| 79 | + |
| 80 | +The **routing** stack is great for trying `traefik`, a dynamically configured load-balancer. |
0 commit comments