Skip to content

Instantly share code, notes, and snippets.

@bresnow
Last active October 14, 2021 17:31
Show Gist options
  • Save bresnow/41a89d0a8163a0c164e20b9b5ad99708 to your computer and use it in GitHub Desktop.
Save bresnow/41a89d0a8163a0c164e20b9b5ad99708 to your computer and use it in GitHub Desktop.
Docker Swarm deployment of a Gun database peer with traefik labels.
version: '3'
services:
# Docker Swarm Gun Peer. To deploy traefik follow recipe at https://dockerswarm.rocks/traefik/
# NOTE: This certresolver is 'http' not 'le'
gun:
image: gundb/gun:latest
networks:
- gun
- traefik-public
volumes:
- gun02:/work # TODO: NFS4 mount at directory. S3 container sharing this volume.
deploy:
placement:
constraints:
- node.labels.node2 == true
labels:
- traefik.enable=true
- traefik.docker.network=traefik-public
- traefik.constraint-label=traefik-public
- traefik.http.routers.gun${NUMBER}-http.rule=Host(`$DOMAIN`)
- traefik.http.routers.gun${NUMBER}-http.entrypoints=http
- traefik.http.routers.gun${NUMBER}-http.middlewares=https-redirect
- traefik.http.routers.gun${NUMBER}-https.rule=Host(`$DOMAIN`)
- traefik.http.routers.gun${NUMBER}-https.entrypoints=https
- traefik.http.routers.gun${NUMBER}-https.tls=true
- traefik.http.routers.gun${NUMBER}-https.tls.certresolver=http # <---- certresolver
- traefik.http.services.gun${NUMBER}.loadbalancer.server.port=8765
traefik:
# Use the latest v2.2.x Traefik image available
image: traefik:v2.5
command:
# Enable Docker in Traefik, so that it reads labels from Docker services
- --providers.docker
# Add a constraint to only use services with the label "traefik.constraint-label=traefik-public"
- --providers.docker.constraints=Label(`traefik.constraint-label`, `traefik-public`)
# Do not expose all Docker services, only the ones explicitly exposed
- --providers.docker.exposedbydefault=false
# Enable Docker Swarm mode
- --providers.docker.swarmmode
# Create an entrypoint "http" listening on port 80
- --entrypoints.http.address=:80
# Create an entrypoint "https" listening on port 443
- --entrypoints.https.address=:443
# Create the certificate resolver "le" for Let's Encrypt, uses the environment variable EMAIL
- --certificatesresolvers.http.acme.email=${EMAIL?Variable not set}
# Store the Let's Encrypt certificates in the mounted volume
- --certificatesresolvers.http.acme.storage=/certificates/acme.json
# Use the TLS Challenge for Let's Encrypt
- --certificatesresolvers.http.acme.tlschallenge=true
# Enable the access log, with HTTP requests
- --accesslog
# Enable the Traefik log, for configurations and errors
- --log
# Enable the Dashboard and API
- --api
- --api.dashboard
- --api.insecure
ports:
# Listen on port 80, default for HTTP, necessary to redirect to HTTPS
- 80:80
# Listen on port 443, default for HTTPS
- 443:443
deploy:
placement:
constraints:
# Make the traefik service run only on the node with this label
# as the node with it has the volume for the certificates
- node.labels.traefik-public.traefik-public-certificates == true
labels:
# Enable Traefik for this service, to make it available in the public network
- traefik.enable=true
# Use the traefik-public network (declared below)
- traefik.docker.network=traefik-public
# Use the custom label "traefik.constraint-label=traefik-public"
# This public Traefik will only use services with this label
# That way you can add other internal Traefik instances per stack if needed
- traefik.constraint-label=traefik-public
# admin-auth middleware with HTTP Basic auth
# Using the environment variables USERNAME and HASHED_PASSWORD
- traefik.http.middlewares.admin-auth.basicauth.users=${USERNAME?Variable not set}:${HASHED_PASSWORD?Variable not set}
# https-redirect middleware to redirect HTTP to HTTPS
# It can be re-used by other stacks in other Docker Compose files
- traefik.http.middlewares.https-redirect.redirectscheme.scheme=https
- traefik.http.middlewares.https-redirect.redirectscheme.permanent=true
# traefik-http set up only to use the middleware to redirect to https
# Uses the environment variable DOMAIN
- traefik.http.routers.traefik-public-http.rule=Host(`${DOMAIN?Variable not set}`)
- traefik.http.routers.traefik-public-http.entrypoints=http
- traefik.http.routers.traefik-public-http.middlewares=https-redirect
# traefik-https the actual router using HTTPS
# Uses the environment variable DOMAIN
- traefik.http.routers.traefik-public-https.rule=Host(`${DOMAIN?Variable not set}`)
- traefik.http.routers.traefik-public-https.entrypoints=https
- traefik.http.routers.traefik-public-https.tls=true
# Use the special Traefik service api@internal with the web UI/Dashboard
- traefik.http.routers.traefik-public-https.service=api@internal
# Use the "le" (Let's Encrypt) resolver created below
- traefik.http.routers.traefik-public-https.tls.certresolver=http
# Enable HTTP Basic auth, using the middleware created above
#- traefik.http.routers.traefik-public-https.middlewares=traefik-auth
# Define the port inside of the Docker service to use
- traefik.http.services.traefik-public.loadbalancer.server.port=8080
volumes:
# Add Docker as a mounted volume, so that Traefik can read the labels of other services
- /var/run/docker.sock:/var/run/docker.sock:ro
# Mount the volume to store the certificates
- traefik-public-certificates:/certificates
networks:
# Use the public network created to be shared between Traefik and
# any other service that needs to be publicly available with HTTPS
- traefik-public
networks:
gun:
traefik-public:
external: true
volumes:
gun02:
traefik-public-certificates:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment