简体   繁体   English

如何让traefik从docker内部重定向到特定的非docker端口

[英]How to get traefik to redirect to specific non-docker port from inside docker

First of all I'm sorry if I'm not using the right terms to ask this question, but I'm not up to the terminology in place.首先,如果我没有使用正确的术语来问这个问题,我很抱歉,但我不适合现有的术语。

I have traefik running in a docker container and serving some services with the PathPrefix option, for instance, www.myserver.com/wordpress redirects to a docker container running wordpress.我在 docker 容器中运行 traefik 并使用 PathPrefix 选项提供一些服务,例如,www.myserver.com/wordpress 重定向到运行 wordpress 的 docker 容器。

But how do I get it to redirect to outside a docker container?但是如何让它重定向到 docker 容器之外呢? Specifically, how do I get www.myserver.com to redirect to port 8080 in my machine to serve a service I have running there in the host OS (not in a docker container)?具体来说,我如何让 www.myserver.com 重定向到我机器中的端口 8080 以提供我在主机操作系统中运行的服务(而不是在 docker 容器中)?

This is my traefik.toml :这是我的traefik.toml

logLevel = "DEBUG"
defaultEntryPoints = ["http", "https"]

[entryPoints]
    [entryPoints.http]
        address = ":80"
        compress = false
        [entryPoints.http.redirect]
            entryPoint = "https"
    [entryPoints.https]
        address = ":443"
        [entryPoints.https.tls]

[acme]
email = "mymail@mail.com"
storage = "acme.json"
entryPoint = "https"
onHostRule = true
#onDemand = true
[[acme.domains]]
    main = "www.myserver.com"

[web]
address = ":8888"

[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "www.myserver.com"
watch = true
exposedbydefault = false

And my docker-compose.yml for the traefik container:docker-compose.yml用于 traefik 容器:

version: "2"

services:
  traefik:
    image: traefik
    network_mode: "host"
    ports:
      - "80:80"
      - "443:443"
      - "8888:8888"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ${SERVER_DIR}/AppData/traefik:/etc/traefik/
      - ${PWD}/acme.json:/acme.json
      - ${PWD}/traefik.toml:/etc/traefik/traefik.toml
      - ${PWD}/servers.toml:/etc/traefik/servers.toml
    restart: never

With the new Traefik (v.2) you need to use a combination of labels and an external file, you can find below my working example.使用新的 Traefik (v.2),您需要使用标签和外部文件的组合,您可以在我的工作示例下方找到。

In your docker compose you need to add the comands to define the external file and enable the provider在您的 docker compose 中,您需要添加命令来定义外部文件并启用提供程序

  - "--providers.file=true"
  - "--providers.file.filename=/etc/traefik/rules.toml"

Into your file (rules.toml) the routing to foward to your external service (be aware of the syntax, use the char to define the host ( ` ) )在您的文件 (rules.toml) 中,将路由转发到您的外部服务(注意语法,使用字符来定义主机(`))

example :例子:

Docker-compose: Docker-撰写:

  traefik:
    image: "traefik:v2.0.0"
    container_name: "traefik"
    restart: always
    command:
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
      - "--entrypoints.websecure.address=:443"
      - "--certificatesresolvers.myhttpchallenge.acme.httpchallenge=true"
      - "--certificatesresolvers.myhttpchallenge.acme.httpchallenge.entrypoint=web"
      - "--certificatesresolvers.myhttpchallenge.acme.email=xx@xx.com"
      - "--providers.file=true"
      - "--providers.file.filename=/etc/traefik/rules.toml"
      - "--providers.docker=true"
      - "--providers.file.watch=true"
    ports:
      - "80:80"
      - "8080:8080"
      - "443:443"
    networks:
      - proxy
    environment:
      - CF_API_EMAIL="xx"
      - CF_API_KEY="xx"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
      - "./traefik/rules.toml:/etc/traefik/rules.toml"

Rules.toml规则.toml

  [http.routers]
   # Define a connection between requests and services
     [http.routers.nasweb]
        rule = "Host(`nas.xxxx.com`)"
        entrypoints = ["websecure"]
        service = "nas"
     [http.routers.nasweb.tls]
        certResolver = "myhttpchallenge"


 [http.services]
        # Define how to reach an existing service on our infrastructure
        [http.services.nas.loadBalancer]
           [[http.services.nas.loadBalancer.servers]]
             url = "http://192.168.0.165:80"

I've fiddled around and found the answer.我已经摆弄并找到了答案。

In traefik.toml add:traefik.toml添加:

################################################################
# File configuration backend
################################################################
# Enable file configuration backend
# Optional
[file]
        filename = "servers.toml"

# Enable watch file changes
        watch = true

In docker-compose.yml change the volumes: to:docker-compose.ymlvolumes:更改为:

volumes:
  - /var/run/docker.sock:/var/run/docker.sock
  - ${SERVER_DIR}/AppData/traefik:/etc/traefik/
  - ${PWD}/acme.json:/acme.json
  - ${PWD}/traefik.toml:/etc/traefik/traefik.toml
  - ${PWD}/servers.toml:/servers.toml

Add file servers.toml :添加文件servers.toml

loglevel = "ERROR"

[backends]
[backends.nasweb]
        [backends.nasweb.servers.nasweb]
            url = "http://192.168.1.11:8080"

[frontends]
        [frontends.domain]
                backend = "nasweb"
        [frontends.domain.routes.domain]
                rule = "Host:www.myserver.com"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM