Benutzer-Werkzeuge

Webseiten-Werkzeuge


projekte:docker:traefik:start

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen Revision Vorhergehende Überarbeitung
Nächste Überarbeitung
Vorhergehende Überarbeitung
projekte:docker:traefik:start [2022/11/15 17:24]
admin [Vorbereitung]
projekte:docker:traefik:start [2022/12/07 17:09] (aktuell)
admin [Eigenes Zertifikat]
Zeile 1: Zeile 1:
 ====== Installation Traefik Loadbalancer/Proxy Container ====== ====== Installation Traefik Loadbalancer/Proxy Container ======
 +
 +Traefik ist mit der wichtigste Teil in der ganzen Installation da darueber die Browser-Zugriffe auf die Container verteilt werden und auch ein Zertifikat fuer https zur Verfuegung gestellt wird.
  
 ===== Vorbereitung ===== ===== Vorbereitung =====
Zeile 5: Zeile 7:
 Traefik muss auf einem Manager Node installiert werden. Traefik muss auf einem Manager Node installiert werden.
  
-  apt install apache2-utils (Für htpasswd)+  apt install apache2-utils (Für htpasswd um den Passwort Hash fuer Broser Benutzer erstellen zu koennen)
  
   export EMAIL=meinname@beispiel.de   export EMAIL=meinname@beispiel.de
   export DOMAIN=traefik.net17.porz   export DOMAIN=traefik.net17.porz
 +
 +Hiermit erzeugen wir jetzt fuer den Benutzer admin mit Passwort "secure_password" einen Hashwert. Das wird dann nachher beim Browserzugriff abgefragt.
  
   htpasswd -nb admin secure_password   htpasswd -nb admin secure_password
Zeile 17: Zeile 21:
   docker network ls (Test ob das neue Netzwerk angelegt wurde)   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.+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 einem dieser beiden Nodes verfuegbar ist.
  
 Dazu mit Dazu mit
Zeile 27: Zeile 31:
   jfctstt12sri9q0fr4snnxaa3     dockerwrk04   Ready     Active                          20.10.21   jfctstt12sri9q0fr4snnxaa3     dockerwrk04   Ready     Active                          20.10.21
 die IDs der beiden Manager rauskriegen und dann mit den Befehlen 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=true h1ueklwju2myqotsbpkqhuknl 
-  docker node update --label-add traefik-node n4u23ksil3wqdp4ee4iz5n43j +  docker node update --label-add traefik-node=true n4u23ksil3wqdp4ee4iz5n43j 
-die Labels vergeben.+die Labels vergeben. Die Labels sind quasi nur Notizen die zu einem Node gehoeren und nachher in Konfigurationen fuer Dienste benannt werden koennen. So kann z.B. ein bestimmter Container nur auf einem bestimmten PI gestartet werden falls gewuenscht.
  
 Jetzt bereiten wir noch einen Benutzer und andere Angaben fuer traefik vor: Jetzt bereiten wir noch einen Benutzer und andere Angaben fuer traefik vor:
Zeile 47: Zeile 51:
 ===== Konfiguration ===== ===== Konfiguration =====
  
-Jetzt braucht man noch eine compose Datei für traefik um es schlussendlich in den Swarm aufzunehmen:+Für traefik benutze ich auf meinem NFS drei Verzeichnisse: 
 +  mkdir -p /docker/data/volumes/traefik/certs 
 +  mkdir /docker/data/volumes/traefik/conf 
 +  mkdir /docker/data/volumes/traefik/data 
 + 
 +Im Verzeichnis certs liegen meine Zertifikate die ich selbst für meine interne Domain erstellt habe (siehe unten). 
 + 
 + 
 +Es gibt jetzt drei Dateien die wir konfigurieren muessen.  
 +  - /docker/data/compose/traefik/mn-traefik.yml (Konfiguration des traefik Service in Docker) 
 +  - /docker/data/volumes/traefik/conf/dynamic_conf.yml (Dynamische traefik Konfiguration, kann zur Laufzeit angepasst werden) 
 +  - /docker/data/volumes/traefik/data/traefik.yml (Statische traefik Konfiguration, wird nur beim Start von traefik geladen) 
 + 
 +==== compose ==== 
 + 
 +Jetzt braucht man noch eine compose Datei für traefik um es schlussendlich in den Swarm aufzunehmen. Eine Vorlage kann man aus dem Internet laden mit dem Befehl:
  
   curl -L dockerswarm.rocks/traefik.yml -o traefik.yml   curl -L dockerswarm.rocks/traefik.yml -o traefik.yml
Zeile 54: Zeile 73:
  
 <code> <code>
-version: '3.3' +version: "3"
 services: services:
- 
   traefik:   traefik:
-    # Use the latest v2.2.x Traefik image available +    image: traefik:latest
-    image: traefik:v2.2+
     ports:     ports:
-      # Listen on port 80, default for HTTP, necessary to redirect to HTTPS 
       - 80:80       - 80:80
-      # Listen on port 443, default for HTTPS+      - 82:82
       - 443:443       - 443:443
 +      - 8080:8080
 +    command: --docker --docker.domain=net17.porz --providers.file.directory=/mnconf/ --providers.file.watch=true --entrypoints.http.address=:80 --entrypoints.https.address=:443 --accesslog --log --api
     deploy:     deploy:
       placement:       placement:
         constraints:         constraints:
-          # Make the traefik service run only on the node with this label +          - node.labels.traefik-node == true
-          # as the node with it has the volume for the certificates +
-          - node.labels.traefik_proxy.traefik_proxy-certificates == true+
       labels:       labels:
-        # Enable Traefik for this service, to make it available in the public network 
         - traefik.enable=true         - traefik.enable=true
-        # Use the traefik_proxy network (declared below) 
         - traefik.docker.network=traefik_proxy         - traefik.docker.network=traefik_proxy
-        # Use the custom label "traefik.constraint-label=traefik_proxy" +        - traefik.http.middlewares.traefik-auth.basicauth.users=admin:$$2y$$05$$IPSaQ2uuefW/TuvwT773UOuShpxNYcuTNdwEXqUBYaju3IbeqqPbK 
-        # This public Traefik will only use services with this label +        - traefik.http.routers.traefik.entrypoints=http
-        # 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.scheme=https
         - traefik.http.middlewares.https-redirect.redirectscheme.permanent=true         - 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(`traefik.net17.porz`)
-        - 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.entrypoints=http
         - traefik.http.routers.traefik_proxy-http.middlewares=https-redirect         - 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(`traefik.net17.porz`)
-        - 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.entrypoints=https
         - traefik.http.routers.traefik_proxy-https.tls=true         - traefik.http.routers.traefik_proxy-https.tls=true
 +
         # Use the special Traefik service api@internal with the web UI/Dashboard         # Use the special Traefik service api@internal with the web UI/Dashboard
         - traefik.http.routers.traefik_proxy-https.service=api@internal         - 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         # Define the port inside of the Docker service to use
         - traefik.http.services.traefik_proxy.loadbalancer.server.port=8080         - traefik.http.services.traefik_proxy.loadbalancer.server.port=8080
 +    networks:
 +      - traefik_proxy
 +
     volumes:     volumes:
-      # Add Docker as a mounted volume, so that Traefik can read the labels of other services+      - /etc/localtime:/etc/localtime:ro
       - /var/run/docker.sock:/var/run/docker.sock:ro       - /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/traefik.yml:/traefik.yml:ro
       - /docker/data/volumes/traefik/data/acme.json:/acme.json       - /docker/data/volumes/traefik/data/acme.json:/acme.json
 +      - /docker/data/volumes/traefik/logs/:/var/log/traefik/
       - /docker/data/volumes/traefik/conf/:/mnconf/       - /docker/data/volumes/traefik/conf/:/mnconf/
       - /docker/data/volumes/traefik/certs/:/certs/       - /docker/data/volumes/traefik/certs/:/certs/
       - /docker/data/volumes/traefik/conf/dynamic_conf.yml:/mnconf/dynamic_conf.yml:ro       - /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: networks:
-  # Use the previously created public network "traefik_proxy", shared with other 
-  # services that need to be publicly available via this Traefik 
   traefik_proxy:   traefik_proxy:
     external: true     external: true
 </code> </code>
  
-Traefik hat zwei Arten der Konfiguration, statische und dynamische. Manche Befehle und Konfigurationen gibt es nur las statische, manche nur als dynamische Version. Die dynamische findet bei mir über eine Datei dynamic_conf.yml statt und die statische über eine Datei die auch trafik.yml heisst aber woanders liegt.+Traefik hat zwei Arten der Konfiguration, statische und dynamische. Manche Befehle und Konfigurationen gibt es nur als statische, manche nur als dynamische Version. Die dynamische findet bei mir über eine Datei dynamic_conf.yml statt und die statische über eine Datei die trafik.yml heisst aber woanders liegt.
  
-Für traefik benutze ich auf meinem NFS drei Verzeichnisse: +==== dynamic_conf.yml ====
-  mkdir -p /docker/data/volumes/traefik/certs +
-  mkdir /docker/data/volumes/traefik/conf +
-  mkdir /docker/data/volumes/traefik/data+
  
-Im Verezeichnis certs liegen meine Zertifikate die ich selbst für meine interne Domain erstellt habe (siehe unten) unter conf liegt die dynmaische dynamic_conf.yml +Unter conf liegt die dynamische dynamic_conf.yml 
-traefik.yml+<code> 
 +root@dockerman01:/docker/data/compose# cat ../volumes/traefik/conf/dynamic_conf.yml  
 +tls: 
 +  certificates: 
 +    - certFile: /certs/net17.porz-crt.pem 
 +      keyFile: /certs/net17.porz-key.pem 
 +      stores: 
 +        - default 
 +  stores: 
 +    default: 
 +      defaultCertificate: 
 +        certFile: /certs/net17.porz-crt.pem 
 +        keyFile: /certs/net17.porz-key.pem 
 +    Store0: 
 +      defaultCertificate: 
 +        certFile: /certs/net17.porz-crt.pem 
 +        keyFile: /certs/net17.porz-key.pem 
 +  options: 
 +    default: 
 +      minVersion: VersionTLS12 
 +      cipherSuites: 
 +        - TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 
 +        - TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 
 +        - TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305 
 +        - TLS_AES_128_GCM_SHA256 
 +        - TLS_AES_256_GCM_SHA384 
 +        - TLS_CHACHA20_POLY1305_SHA256 
 +      curvePreferences: 
 +        - CurveP521 
 +        - CurveP384 
 +      sniStrict: true 
 +http: 
 +  middlewares: 
 +    secHeaders: 
 +      headers: 
 +        browserXssFilter: true 
 +        contentTypeNosniff: true 
 +        frameDeny: true 
 +        sslRedirect: true 
 +        #HSTS Configuration 
 +        stsIncludeSubdomains: true 
 +        stsPreload: true 
 +        stsSeconds: 31536000 
 +        customFrameOptionsValue: "SAMEORIGIN" 
 +</code> 
 + 
 +==== traefik.yml ==== 
 + 
 +Unter data liegt die statische traefik.yml:
 <code> <code>
 api: api:
Zeile 167: Zeile 185:
   insecure: true   insecure: true
 entryPoints: entryPoints:
 +  dns:
 +    address: ":53"
   http:   http:
     address: ":80"     address: ":80"
Zeile 175: Zeile 195:
     endpoint: "unix:///var/run/docker.sock"     endpoint: "unix:///var/run/docker.sock"
     exposedByDefault: false     exposedByDefault: false
 +    watch: true
 +    swarmMode: true
   file:   file:
-    filename: "./dynamic_conf.yml+    directory: "/mnconf/
-certificatesResolvers+    watchtrue 
-  http+log
-    acme+  levelDEBUG 
-      emailemail@example.com   #### hier eure E-Mail angeben #### +  filePath"/var/log/traefik/traefik.log" 
-      storageacme.json +accessLog
-      httpChallenge: +  filePath"/var/log/traefik/access.log"
-        entryPoint: http+
 </code> </code>
 +
  
 ===== Start ===== ===== Start =====
Zeile 198: Zeile 220:
   watch docker container ls   watch docker container ls
 benutzen. Das Ganze kann man dann mit CTRL+C bzw. STRG+C abbrechen. benutzen. Das Ganze kann man dann mit CTRL+C bzw. STRG+C abbrechen.
 +
 +Danach kann auf das traefik Dashboard per Browser zugegriffen werden: http://172.17.200.24:8080
 +
 +{{:projekte:docker:traefik:traefik-dashboard.png|}}
 +
 +Nicht wundern das da schon Port 53 aufgeführt wird. Den hatte ich schon testweise als Entrypoint konfiguriert da ich den nachher für DNS bzw. Pihole brauche.
 +
 +===== Eigenes Zertifikat =====
 +
 +Um unsere Dienste auch verschluesselt zur Verfuegung zu stellen brauchen wir ein Zertifikat. Man kann sich einerseits eines kostenfreies von zum Beispiel Let's Encrypt holen, ein kommerzielles kaufen oder, wie ich es gemacht habe, ein selbsterselltes benutzen.
 +
 +Das Wahl fiel auf ein selbsterstelltes Zertfikat da ich ein generisches Zertfikat moechte und dieses nur kommerziell fuer richtig Geld verfuegbar ist. Ich haette natuerlich auch mehrere Let's Encrypt Zertifikate verwenden koennen aber das war mir zu viel Aufwand und Arbeit die zu erstellen und zu verwalten.
 +
 +Normalerweise gibt es eine 1:1 Beziehung zwischen Servername www.beispiel.de und dem Namen der im Zertifikat hinterlegt ist. Wenn das nicht uebrein stimmt gibt es eine Warnung vom Browser, die Verbindung ist zwar nach Bestaetigung trotzdem verschluesselt aber es verunsichert Nutzer.
 +
 +Da ich diverse Dienste benutze wie apache Webbrowser, traefik, iobroker, pihole usw. wuerde ich auch mehrere Einzelzertifikate benoetigen, daher die Idee ein generisches *.beispiel.domain zu verwenden. Diese gilt fuer alle Namen wie apache.beispiel.domain, traefik.beispiel.domain usw.
 +
 +Das Zertfikat und alles was dazu benoetigt wird erstellen wir durch den openssl Befehl auf der Linuk Konsole. Als erstes muessen wir uns eine kurze Konfigurationsdatei erstellen:
 +<code>
 +  mkdir ssl-cert
 +  cd ssl-cert
 +</code>
 +Hier legen wir jetzt eine Datei openssl.ss.cnf an mit folgendem Inhalt:
 +<code>
 +basicConstraints=CA:FALSE
 +subjectAltName=DNS:*.beispiel.domain
 +extendedKeyUsage=serverAuth
 +</code>
 +Dann erstellen wir noch ein Skript gen_wildcard.sh mit folgendem Inhalt:
 +<code>
 +echo Erstelle CA Key
 +openssl genrsa -des3 -out CAPrivate.key 4096
 +echo Erstelle CA Cert
 +openssl req -x509 -new -nodes -key CAPrivate.key -sha256 -days 365 -out CAPrivate.pem
 +echo Erstelle Domain private Key
 +openssl genrsa -out beispiel.domain-key.pem 4096
 +echo CSR fuer beispiel.domain erzeugen
 +openssl req -new -key beispiel.domain-key.pem -extensions v3_ca -out beispiel.domain-csr.csr
 +echo beispiel.domain Certificate erzeugen
 +openssl x509 -req -in beispiel.domain-csr.csr -CA CAPrivate.pem -CAkey CAPrivate.key -CAcreateserial -extfile openssl.ss.cnf -out beispiel.domain-crt.pem -days 3650 -sha256
 +</code>
 +Auf die Einzelheiten der Zertfikatserzeugung gehe ich mal nicht ein da das ein ganzer Artikel waere und man dazu im Internet eigentlich genug Informationen findet.
 +
 +Die erzeugten Dateien mit dem Zertfikat und dem Private Key muessen nun unter /docker/data/volumes/trafik/certs kopiert werden. Wenn ihr andere Namen fuer die dateien benutzt muessen die in der dynamic_conf.yml angepasst werden.
  
  
projekte/docker/traefik/start.1668529467.txt.gz · Zuletzt geändert: 2022/11/15 17:24 von admin