是否可以使用Traefik 2.0 TCP功能平衡MQTT流量?

时间:2019-04-27 12:57:12

标签: tcp load-balancing mqtt traefik

我目前正在使用Haproxy来负载均衡N个dockerized mqtt经纪人的MQTT流量

Traefik 2.0已经发布并且可以代理TCP通信,我想知道是否可以像使用Haproxy一样使用Traefik作为负载均衡器来将MQTT通信负载均衡到N个dockerized mqtt代理。

我使用traefik官方文档尝试了一些基本配置,但无法向上游传输流量。

有人尝试过吗?

1 个答案:

答案 0 :(得分:2)

是的,昨天我做了。我将 VerneMQ 用于MQTT代理,并将 Traefik 用作负载平衡器。以下配置文件适用于 Docker Swarm

这是我使用的 docker-compose 配置:

version: "3"

services:
    traefik:
        image: traefik:v2.1
        ports:
            - "80:80"
            - "1883:1883"
        volumes:
            - /var/run/docker.sock:/var/run/docker.sock
            - /dev/random:/dev/random
            - ./traefik.toml:/traefik.toml
        deploy:
            placement:
                constraints: [node.role == manager]
            labels:
                - traefik.enable=true
                - traefik.docker.network=net

                - traefik.http.routers.dashboard.rule=Host(`traefik.home.gtheofilis.com`)
                - traefik.http.routers.dashboard.entrypoints=http
                - traefik.http.routers.dashboard.service=api@internal
                - traefik.http.services.dashboard.loadbalancer.server.port=9999

    vmq0:
        image: vernemq/vernemq
        environment:
            DOCKER_VERNEMQ_SWARM: 1
            DOCKER_VERNEMQ_ACCEPT_EULA: "yes"
            DOCKER_VERNEMQ_ALLOW_ANONYMOUS: "on"
        deploy:
            placement:
                constraints: [node.role == manager]
            labels:
                - traefik.enable=false

    vmq:
        image: vernemq/vernemq
        depends_on:
            - vmq0
        environment:
            DOCKER_VERNEMQ_SWARM: 1
            DOCKER_VERNEMQ_DISCOVERY_NODE: vmq0
            DOCKER_VERNEMQ_ACCEPT_EULA: "yes"
            DOCKER_VERNEMQ_ALLOW_ANONYMOUS: "on"
        deploy:
            replicas: 3
            labels:
                - traefik.enable=true
                - traefik.docker.network=net
                - traefik.tcp.routers.mqtt.rule=HostSNI(`*`)
                - traefik.tcp.routers.mqtt.entrypoints=mqtt
                - traefik.tcp.services.mqtt.loadbalancer.server.port=1883

                - traefik.http.routers.mqtt_dashboard.rule=Host(`vernemq.home.gtheofilis.com`)
                - traefik.http.routers.mqtt_dashboard.service=mqtt_dashboard
                - traefik.http.routers.mqtt_dashboard.middlewares=auth
                - traefik.http.routers.mqtt_dashboard.entrypoints=http
                - traefik.http.services.mqtt_dashboard.loadbalancer.server.port=8888

networks:
    default:
        external:
            name: net

这是我的 traefik.toml 配置文件:

[api]
    dashboard = true

[entryPoints]
  [entryPoints.http]
    address = ":80"
  [entryPoints.mqtt]
    address = ":1883"

[providers]
    [providers.docker]
        endpoint = "unix:///var/run/docker.sock"
        swarmMode = true