Traefik无法与工作节点[docker swarm]

时间:2019-06-20 08:25:45

标签: docker docker-compose docker-swarm traefik docker-network

我的问题如下:

我创建了一个由两个简单节点组成的小型集群:一个管理器和一个工作器。 在管理器节点上,我为Swarmpit仪表板安装了一个包含Traefik服务的堆栈和一个包含服务的堆栈。 在工作节点上,我安装了带有wordpress服务的堆栈。 我为正确的traefik配置安装了一个名为“ web”的“覆盖”网络。

结果是Traefik仪表板和Swarmpit仪表板均正常工作。 traefik无法“识别”我放在工作节点上的所有内容,因此它不起作用。如果我尝试访问该服务的网页,则会返回错误404

如果将wordpress服务从工作程序节点移动到管理程序节点,它将开始正常工作。 此外,我还可以通过“网络”网络从traefik容器内部ping到wordpress容器,反之亦然

可能是什么问题?

下面是一些配置和日志:

Traefik docker-conpose.yml 配置:

version: "3.7"
networks:
  web:
    external: true
services:
  traefik:
    image: traefik:alpine
    command:
      - --api
      - --docker
      - --logLevel=DEBUG
    networks:
      - web
    labels:
      - traefik.frontend.rule=Host:traefik.mydomain.cloud
      - traefik.docker.network=web
      - traefik.backend=traefik.mydomain.cloud
      - traefik.enable=true
      - traefik.port=8080
    ports:
      - 80:80
      - 443:443
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - data:/etc/traefik
    deploy:
      mode: global
      placement:
        constraints:
          - node.role == manager
volumes:
  data:
    driver_opts:
      type: nfs
      o: addr=x.x.x.x,nolock,soft,rw
      device: :/var/nfs/traefik


Wordpress测试 docker-conpose.yml 配置:

version: "3.7"
networks:
  web:
    external: true
  internal:
    external: false
services:
  app:
    image: wordpress:5.2.1-php7.1-apache
    depends_on:
      - mysql
    networks:
      - web
      - internal
    labels:
      - traefik.enable=true
      - traefik.frontend.rule=Host:www.mywordpress_test.com
      - traefik.backend=www.mywordpress_test.com
      - traefik.docker.network=web
      - traefik.port=80
    environment:
      WORDPRESS_DB_HOST: mysql:3306
      WORDPRESS_DB_USER: xxxxx
      WORDPRESS_DB_PASSWORD: xxxxx
      WORDPRESS_DB_NAME: xxxxx
    volumes:
      - www:/var/www/html
    deploy:
      placement:
        constraints:
          - node.role == worker
  mysql:
    image: mysql:5.7
    networks:
      - web
      - internal
    environment:
      MYSQL_ROOT_PASSWORD: xxxxx
      MYSQL_DATABASE: xxxxx
      MYSQL_USER: xxxxx
      MYSQL_PASSWORD: xxxxx
    volumes:
      - db_data:/var/lib/mysql/
      - db_init:/docker-entrypoint-initdb.d/
    deploy:
      placement:
        constraints:
          - node.role == worker
volumes:
  db_init:
    driver_opts:
      type: nfs
      o: addr=x.x.x.x,nolock,soft,rw
      device: :/var/nfs/wordpress_test/db/initdb.d
  db_data:
    driver_opts:
      type: nfs
      o: addr=x.x.x.x,nolock,soft,rw
      device: :/var/nfs/wordpress_test/db/data
  www:
    driver_opts:
      type: nfs
      o: addr=x.x.x.x,nolock,soft,rw
      device: :/var/nfs/wordpress_test/www


经理节点上:

    sudo docker node ls

    ID          HOSTNAME    STATUS  AVAILABILITY    MANAGER STATUS  ENGINE VERSION
    xxxxxx *    sw-man-01   Ready   Active          Leader          18.09.6
    xxxxxx      sw-wk-01    Ready   Active                          18.09.6

经理节点上:

    sudo docker network ls

    NETWORK ID          NAME                        DRIVER              SCOPE
    xxxxxx              bridge                      bridge              local
    xxxxxx              docker_gwbridge             bridge              local
    xxxxxx              swarmpit_net                overlay             swarm
    xxxxxx              wordpress_test_internal     overlay             swarm
    xxxxxx              host                        host                local
    xxxxxx              ingress                     overlay             swarm
    xxxxxx              none                        null                local
    xxxxxx              web                         overlay             swarm

经理节点上:

    sudo docker network inspect web

    [
    {
        "Name": "web",
        "Id": "vdvnirom26xxxxxx",
        "Created": "2019-06-17T17:59:52.865557645+02:00",
        "Scope": "swarm",
        "Driver": "overlay",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "10.0.0.0/24",
                    "Gateway": "10.0.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "xxxxxx": {
                "Name": "swarmpit_app.1.pefyqnzqyhk4lrnkexgs7k4wu",
                "EndpointID": "xxxxxx",
                "MacAddress": "02:42:0a:00:00:6f",
                "IPv4Address": "10.0.0.111/24",
                "IPv6Address": ""
            },
            "xxxxxx": {
                "Name": "traefik_traefik.pttpmxhv0fsq02zcew5i2qylw.qfrllyuio7yau74ablv0amqut",
                "EndpointID": "xxxxxx",
                "MacAddress": "02:42:0a:00:00:1e",
                "IPv4Address": "10.0.0.30/24",
                "IPv6Address": ""
            },
            "lb-web": {
                "Name": "web-endpoint",
                "EndpointID": "xxxxxx",
                "MacAddress": "02:42:0a:00:00:04",
                "IPv4Address": "10.0.0.4/24",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.driver.overlay.vxlanid_list": "4097"
        },
        "Labels": {},
        "Peers": [
            {
                "Name": "xxxxxx",
                "IP": "192.168.2.120"
            },
            {
                "Name": "xxxxxx",
                "IP": "192.168.2.100"
            }
        ]
    }
]

经理节点上:

    sudo docker container inspect [traefik container id]

    [
    {
        ...,
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "xxxxxxxx",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/xxxxxxxx",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "ingress": {
                    "IPAMConfig": {
                        "IPv4Address": "10.255.0.117"
                    },
                    "Links": null,
                    "Aliases": [
                        "xxxxxxxx"
                    ],
                    "NetworkID": "xxxxxxxx",
                    "EndpointID": "xxxxxxxx",
                    "Gateway": "",
                    "IPAddress": "10.255.0.117",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:0a:ff:00:75",
                    "DriverOpts": null
                },
                "web": {
                    "IPAMConfig": {
                        "IPv4Address": "10.0.0.30"
                    },
                    "Links": null,
                    "Aliases": [
                        "xxxxxxxx"
                    ],
                    "NetworkID": "vdvnirom26xxxxxxxx",
                    "EndpointID": "56a914884xxxxxxxx",
                    "Gateway": "",
                    "IPAddress": "10.0.0.30",
                    "IPPrefixLen": 24,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:0a:00:00:1e",
                    "DriverOpts": null
                }
            }
        }
    }
]

工人节点上:

    sudo docker network inspect web

    [
    {
        "Name": "web",
        "Id": "vdvnirom26xxxxxx",
        "Created": "2019-06-19T18:21:15.839889513+02:00",
        "Scope": "swarm",
        "Driver": "overlay",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "10.0.0.0/24",
                    "Gateway": "10.0.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "xxxxxx": {
                "Name": "wordpress_test_app.1.2mbi7i0u9rn14od5fzw1ycbuk",
                "EndpointID": "xxxxxx",
                "MacAddress": "02:42:0a:00:00:19",
                "IPv4Address": "10.0.0.25/24",
                "IPv6Address": ""
            },
            "xxxxxx": {
                "Name": "wordpress_test_mysql.1.7qktlbc9ksn164gc292zva12w",
                "EndpointID": "xxxxxx",
                "MacAddress": "02:42:0a:00:00:17",
                "IPv4Address": "10.0.0.23/24",
                "IPv6Address": ""
            },
            "lb-web": {
                "Name": "web-endpoint",
                "EndpointID": "xxxxxx",
                "MacAddress": "02:42:0a:00:00:1a",
                "IPv4Address": "10.0.0.26/24",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.driver.overlay.vxlanid_list": "4097"
        },
        "Labels": {},
        "Peers": [
            {
                "Name": "xxxxxx",
                "IP": "192.168.2.120"
            },
            {
                "Name": "xxxxxx",
                "IP": "192.168.2.100"
            }
        ]
    }
]

工人节点上:

    sudo docker container inspect [wordpress_test container id]

    [
    {
        ...,
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "xxxxxxx",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": null,
                "9000/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/xxxxxxx",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "wordpress_test_internal": {
                    "IPAMConfig": {
                        "IPv4Address": "10.0.29.35"
                    },
                    "Links": null,
                    "Aliases": [
                        "xxxxxxx"
                    ],
                    "NetworkID": "xxxxxxx",
                    "EndpointID": "xxxxxxx",
                    "Gateway": "",
                    "IPAddress": "10.0.29.35",
                    "IPPrefixLen": 24,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:0a:00:1d:23",
                    "DriverOpts": null
                },
                "web": {
                    "IPAMConfig": {
                        "IPv4Address": "10.0.0.25"
                    },
                    "Links": null,
                    "Aliases": [
                        "xxxxxxx"
                    ],
                    "NetworkID": "vdvnirom26xxxxxxx",
                    "EndpointID": "cabe06bc3b1c48acxxxxxxx",
                    "Gateway": "",
                    "IPAddress": "10.0.0.25",
                    "IPPrefixLen": 24,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:0a:00:00:19",
                    "DriverOpts": null
                }
            }
        }
    }
]

1 个答案:

答案 0 :(得分:0)

Traefik具有附加的群集模式标志,以支持不同docker节点上的服务。没有此选项,它将仅查看本地运行的容器。使用该选项时,标签需要位于群集服务上,而不是单个容器上。 traefik撰写文件应如下所示:

  traefik:
    image: traefik:alpine
    command:
      - --api
      - --docker
      # enable swarm mode with the next flag
      - --docker.swarmmode
      # I explictly configure it to watch the docker socket
      - --docker.watch
      # I avoid automatically exposing every container
      - --docker.exposedbydefault=false
      - --logLevel=DEBUG
    networks:
      - web
    deploy:
      placement:
        constraints:
        - node.role == manager
      labels:
        - traefik.frontend.rule=Host:traefik.mydomain.cloud
        - traefik.docker.network=web
        - traefik.backend=traefik.mydomain.cloud
        - traefik.enable=true
        - traefik.port=8080

然后您的服务会将标签移到deploy部分下:

services:
  app:
    image: wordpress:5.2.1-php7.1-apache
    depends_on:
      - mysql
    networks:
      - web
      - internal
    deploy:
      labels:
        - traefik.enable=true
        - traefik.frontend.rule=Host:www.mywordpress_test.com
        - traefik.backend=www.mywordpress_test.com
        - traefik.docker.network=web
        - traefik.port=80
      placement:
        constraints:
          - node.role == worker