Benutzer-Werkzeuge

Webseiten-Werkzeuge


projekte:docker:traefik:start

Dies ist eine alte Version des Dokuments!


Installation Traefik Loadbalancer/Proxy Container

Vorbereitung

Traefik muss auf einem Manager Node installiert werden.

apt install apache2-utils (Für htpasswd)
export EMAIL=meinname@beispiel.de
export DOMAIN=traefik.net17.porz
htpasswd -nb admin secure_password

Dann noch ein eigenes Netzwerk für Trafik in Docker anlegen über das nachher der Kontakt der Container zu traefik stattfindet und wodurch traefik auch automatische neue Container erkennen kann und diese zugreifbar macht.

docker network create --driver=overlay traefik_proxy (Der Name des Netzwerkes am Ende des Befehls ist egal, er muss nur nachher in jedem compose-file verwendet werden)
docker network ls (Test ob das neue Netzwerk angelegt wurde)

Dann noch die beiden Manager mit Labels versehen so das traefik nur darauf startet. Der Grund ist die schon oft erwähnte Cluster-IP Adresse (172.17.200.24 und .25)da diese nur auf diesen beiden Nodes verfuegbar ist.

Dazu mit

docker node ls
ID                            HOSTNAME      STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
h1ueklwju2myqotsbpkqhuknl *   dockerman01   Ready     Active         Leader           20.10.21
n4u23ksil3wqdp4ee4iz5n43j     dockerman02   Ready     Active         Reachable        20.10.21
tjghr95cvd5bf8cvz5qcxn2du     dockerwrk02   Ready     Active                          20.10.21
jfctstt12sri9q0fr4snnxaa3     dockerwrk04   Ready     Active                          20.10.21

die IDs der beiden Manager rauskriegen und dann mit den Befehlen

docker node update --label-add traefik-node h1ueklwju2myqotsbpkqhuknl
docker node update --label-add traefik-node n4u23ksil3wqdp4ee4iz5n43j

die Labels vergeben.

Jetzt bereiten wir noch einen Benutzer und andere Angaben fuer traefik vor:

export USERNAME=admin
export DOMAIN=traefik.net17.porz
export EMAIL=name@beispiel.de
export PASSWORD=EIN_PASSWORT

das Passwort wandeln wir dann in einen Hash um mit dem Befehl

export HASHED_PASSWORD=$(openssl passwd -apr1 $PASSWORD)

oder so falls das Klartext-Passwort nicht in einer Umgebungsvariable rumdümpeln soll, es wird dann danach gefragt:

export HASHED_PASSWORD=$(openssl passwd -apr1)

Mit

echo $HASH_PASSWORD

sollte jetzt sowas wie „$apr1$89eqM5Ro$CxaFELthUKV21DpI3UTQO.“ rauskommen.

Jetzt braucht man noch eine compose Datei für traefik um es schlussendlich in den Swarm aufzunehmen:

curl -L dockerswarm.rocks/traefik.yml -o traefik.yml

Damit hat man eine Grundlage mit allen wichtigen Befehlen. Ich habe einige Stellen darin angepasst weil ich die nicht brauche oder meine Umgebung anders ist.

version: '3.3'

services:

  traefik:
    # Use the latest v2.2.x Traefik image available
    image: traefik:v2.2
    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_proxy.traefik_proxy-certificates == true
      labels:
        # Enable Traefik for this service, to make it available in the public network
        - traefik.enable=true
        # Use the traefik_proxy network (declared below)
        - traefik.docker.network=traefik_proxy
        # Use the custom label "traefik.constraint-label=traefik_proxy"
        # 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_proxy
        # 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_proxy-http.rule=Host(`${DOMAIN?Variable not set}`)
        - traefik.http.routers.traefik_proxy-http.entrypoints=http
        - traefik.http.routers.traefik_proxy-http.middlewares=https-redirect
        # traefik-https the actual router using HTTPS
        # Uses the environment variable DOMAIN
        - traefik.http.routers.traefik_proxy-https.rule=Host(`${DOMAIN?Variable not set}`)
        - traefik.http.routers.traefik_proxy-https.entrypoints=https
        - traefik.http.routers.traefik_proxy-https.tls=true
        # Use the special Traefik service api@internal with the web UI/Dashboard
        - traefik.http.routers.traefik_proxy-https.service=api@internal
        # Use the "le" (Let's Encrypt) resolver created below
        - traefik.http.routers.traefik_proxy-https.tls.certresolver=le
        # Enable HTTP Basic auth, using the middleware created above
        - traefik.http.routers.traefik_proxy-https.middlewares=admin-auth
        # Define the port inside of the Docker service to use
        - traefik.http.services.traefik_proxy.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
      #- /docker/data/volumes/traefik/cert:/certificates
      - /docker/data/volumes/traefik/data/traefik.yml:/traefik.yml:ro
      - /docker/data/volumes/traefik/data/acme.json:/acme.json
      - /docker/data/volumes/traefik/conf/:/mnconf/
      - /docker/data/volumes/traefik/certs/:/certs/
      - /docker/data/volumes/traefik/conf/dynamic_conf.yml:/mnconf/dynamic_conf.yml:ro
    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_proxy"
      - --providers.docker.constraints=Label(`traefik.constraint-label`, `traefik_proxy`)
      # 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.le.acme.email=${EMAIL?Variable not set}
      # Store the Let's Encrypt certificates in the mounted volume
      - --certificatesresolvers.le.acme.storage=/certificates/acme.json
      # Use the TLS Challenge for Let's Encrypt
      - --certificatesresolvers.le.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
    networks:
      # Use the public network created to be shared between Traefik and
      # any other service that needs to be publicly available with HTTPS
      - traefik_proxy

networks:
  # Use the previously created public network "traefik_proxy", shared with other
  # services that need to be publicly available via this Traefik
  traefik_proxy:
    external: true

traefik.yml

api:
  dashboard: true
  insecure: true
entryPoints:
  http:
    address: ":80"
  https:
    address: ":443"
providers:
  docker:
    endpoint: "unix:///var/run/docker.sock"
    exposedByDefault: false
  file:
    filename: "./dynamic_conf.yml"
certificatesResolvers:
  http:
    acme:
      email: email@example.com   #### hier eure E-Mail angeben ####
      storage: acme.json
      httpChallenge:
        entryPoint: http
projekte/docker/traefik/start.1668526387.txt.gz · Zuletzt geändert: 2022/11/15 16:33 von admin