Traefik 2.0 за док-роем не работает - PullRequest
0 голосов
/ 16 октября 2019

Итак, я пытаюсь использовать Traefik для балансировки нагрузки моих веб-приложений через Docker Swarm.

Однако я уже перепробовал множество конфигураций, но как-то не работает. Я уже прочитал документацию и прочитал несколько статей в интернете. К сожалению, многие статьи ссылаются на traefik 1.x вместо traefik 2.0.

Вот мой docker-stack.yml для traefik

version: '3.7'
services:
  traefik:
    image: traefik:2.0
    deploy:
      mode: global
      placement:
        constraints:
        - node.role == manager
      restart_policy:
        condition: on-failure
      labels:
      - traefik.docker.network=load_balancer
    configs:
    - source: traefik
      target: /etc/traefik/traefik.yml
    ports:
    - 80:80
    volumes:
    - /var/run/docker.sock:/var/run/docker.sock
    networks:
    - load_balancer
configs:
  traefik:
    file: ./traefik.yml
networks:
  load_balancer:
    external: true
    name: load_balancer

whoami.yml (для тестирования)

version: '3.7'
services:
  whoami:
    image: containous/whoami
    deploy:
      labels:
      - traefik.enable=true
      - traefik.docker.network=load_balancer
      - traefik.http.routers.whoami.rule=Host(`whoami.docker.localhost`)
    networks:
    - load_balancer
networks:
  load_balancer:
    external: true
    name: load_balancer

Мой traefik.yml

log:
  level: DEBUG

api:
  insecure: true

providers:
  docker:
    exposedByDefault: false
    swarmMode: true
    watch: true

Докерская сеть ls

hxjw9rytw3od        load_balancer       overlay             swarm

curl -H Хост: whoami.docker.localhost http://127.0.0.1

404 page not found

Ответы [ 3 ]

0 голосов
/ 26 октября 2019

Обнаружение службы Traefik с помощью Docker требует меток контейнеров вместо меток изображений (отметьте быстрый запуск traefik ).

Следующий минимальный рабочий пример работает с роем докеров:

version: '3.7'

services:
  traefik:
    image: traefik:2.0
    command: --providers.docker
    ports:
      - 80:80
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

  whoami:
    image: containous/whoami
    labels:   # defining a container label instead of an image label
      - "traefik.http.routers.whoami.rule=Host(`whoami.docker.localhost`)"
0 голосов
/ 09 ноября 2019

Я могу предложить вам не использовать traefik.yml, а использовать Cli Args для настройки вашего экземпляра.

Вы можете сделать это так:

version: "3.7"

services:
  ingress:
    image: traefik:v2.0
    networks:
      - ingress-net
    ports:
      - "80:80"
      - "443:443"
      # TCP Port if needed for any service you have
      - "60000:60000"
    command:
      ###                          ###
      # Traefik Global Configuration #
      ###                          ###
      # Enable DEBUG logs
      - "--log.level=DEBUG" # DEBUG, INFO, etc...
      - "--ping=true"
      # Enable api access without authentification (only GET route so it only possible to get IPs)
      - "--api.insecure=true" # You can insecure here, because It accessible only in the container if you didn't open the port.
      # Set the provider to Docker
      - "--providers.docker=true"
      # Set the docker network
      - "--providers.docker.network=ingress-net"
      # Set to docker swarm cluster
      - "--providers.docker.swarmMode=true"
      # If False : Do not expose containers to the web by default
      - "--providers.docker.exposedByDefault=false"
      # Default rule to service-name.example.com
      - "--providers.docker.defaultRule=Host(`{{ trimPrefix `/` .Name }}.example.com`)"
      # Default http port
      - "--entrypoints.http.address=:80"
      # Default https port
      - "--entrypoints.https.address=:443"
      # Enable let's encrypt
      - "--certificatesResolvers.certbot=true"
      - "--certificatesResolvers.certbot.acme.httpChallenge=true"
      - "--certificatesResolvers.certbot.acme.httpChallenge.entrypoint=http"
      - "--certificatesResolvers.certbot.acme.email=admin@example.com"
      - "--certificatesResolvers.certbot.acme.storage=/letsencrypt/acme.json"
      # TCP Entrypoint if needed
      - "--entrypoints.tcpendpointname.address=:60000"

    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./acme.json:/letsencrypt/acme.json

    deploy:
      replicas: 1
      labels:
        ###               ###
        # Traefik Dashboard #
        ###               ###
        # Enable this endpoint
        - traefik.enable=true
        ##
        # Http
        #
        # Set the service route
        - traefik.http.routers.ingress_http.rule=Host(`ingress.example.com`)
        # Set the entrypoint (http or https)
        - traefik.http.routers.ingress_http.entrypoints=http
        # Rule to redirect to http to https
        - traefik.http.middlewares.ingress-https-redirect.redirectscheme.scheme=https
        # Enable Https redirection
        - traefik.http.routers.ingress_http.middlewares=ingress-https-redirect@docker
        #
        ##

        ##
        # Https
        #
        - traefik.http.routers.ingress_https.rule=Host(`ingress.example.com`)
        # Set the entrypoint (http or https)
        - traefik.http.routers.ingress_https.entrypoints=https
        # Enable Let's encrypt auto certificat creation
        - traefik.http.routers.ingress_https.tls.certresolver=certbot
        # Enable authentification
        - traefik.http.routers.ingress_https.middlewares=ingress-auth@
        # Uncommant this to enable basic authentification
        # - traefik.http.middlewares.ingress-auth.basicauth.users=admin:$$this$$is$$encrypted$$password
        #
        ##

        ##
        # TCP Endpoint
        #
        # Set the service route
        - "traefik.tcp.routers.tcpendpointname.rule=HostSNI(`*`)"
        # Here you can set the host uri if you use tls only.
        # - "traefik.tcp.routers.tcpendpointname.rule=HostSNI(`tcp.example.com`)"
        # - "traefik.tcp.routers.tcpendpointname.tls=true"
        # Set the entrypoin
        - "traefik.tcp.routers.tcpendpointname.entrypoints=tcpendpointname"
        #
        ##

        ##
        # Service
        #
        # Set the service port
        - traefik.http.services.ingress.loadbalancer.server.port=8080
        #
        ##
      placement:
        constraints:
          - node.role == manager

networks:
  ingress-net:
    external: true

Я надеюсь, что это будетПомочь вам.

Вы можете использовать те же метки для любых других контейнеров, которые работают с той же логикой.

0 голосов
/ 17 октября 2019

Может быть, вам нужно добавить объявление точки входа в вашей конфигурации: https://docs.traefik.io/routing/entrypoints/

...