Traefik Loadbalancing multiple ports on the same container instance

Hi all,
I have a docker container, where I host 10 tcp services, that are hosted on port 1000-1009. Now I want to create a load balancer using traefik, but I can't get it to work, routing the same load balancer to these 10 "servers" that are all running on the same instance.

The reason, why I want to run 10 services in a single container is, that a single instance of the server requires about 950 MB memory, and running 10 services in this instance only increases the memory usage to about 1 GB. If I would scale them up using Docker I would require 10 GB of Memory allocated, mostly for the environment. Also each of these services can only execute a single request at a time.

When I create my service with these labels:

  • "traefik.enable=true"
  • "traefik.tcp.routers.cas.rule=HostSNI(*)"
  • "traefik.tcp.routers.cas.entrypoints=cas"

it will only pick up the first exposed port of the container instance, like stated in the doc.

When I create multiple services

  • "traefik.tcp.routers.cas.rule=HostSNI(*)"
  • "traefik.tcp.routers.cas.entrypoints=cas"
  • "traefik.tcp.routers.cas.service=cas"
  • "traefik.tcp.services.cas.loadbalancer.server.port=1000"
  • "traefik.tcp.routers.cas2.rule=HostSNI(*)"
  • "traefik.tcp.routers.cas2.entrypoints=cas"
  • "traefik.tcp.routers.cas2.service=cas2"
  • "traefik.tcp.services.cas2.loadbalancer.server.port=1001"

It will just create multiple loadbalancers

How can I create a loadbalancer with multiple ports, like this?

  • "traefik.tcp.routers.cas.rule=HostSNI(*)"
  • "traefik.tcp.routers.cas.entrypoints=cas"
  • "traefik.tcp.routers.cas.service=cas"
  • "traefik.tcp.services.cas.loadbalancer.server.port=1000-1009"

Also later I want to scale the docker instance aswell, so e.g. 10 replicas mean, that I have a loadbalancer loadbalancing to 100 service instances in 10 docker instances.

Best regards
Mosquito

Based on the documentation, it appears the docker provider doesn't support configuring services in the way you describe (but I could be wrong, @ldez can verify for me on this).

However, it is possible to configure Traefik through the File provider, https://docs.traefik.io/routing/services/#servers-load-balancer

tcp:
  services:
    my-service:
      loadBalancer:
        servers:
        - address: "<container-ip>:1000"
        - address: "<container-ip>:1001"
        - address: "<container-ip>:1002"
...

Hello,

@Mosquito you cannot use a range syntax.

In this case, you can use the file provider and the WRR https://docs.traefik.io/v2.2/routing/services/#weighted-round-robin-service to group your service: you have to define your services with labels and reference it in the WRR configuration.

Hi notsureifkevin, Idez,

thank you for the fast replies.
So as far as I understood, I will require these labels

  • "traefik.tcp.routers.cas.rule=HostSNI( * )"
  • "traefik.tcp.routers.cas.entrypoints=cas"
  • "traefik.tcp.routers.cas.service=cas"
  • "traefik.tcp.services.cas.loadbalancer.server.port=1000"
  • "traefik.tcp.routers.cas2.rule=HostSNI( * )"
  • "traefik.tcp.routers.cas2.entrypoints=cas"
  • "traefik.tcp.routers.cas2.service=cas2"
  • "traefik.tcp.services.cas2.loadbalancer.server.port=1001"
  • ... cas3 (port 1002)
  • ... cas4 (port 1003)
  • ... cas5-10

and then just create a weighted round robin over all services with the same weight.

[tcp.services]
[tcp.services.app]
[[tcp.services.app.weighted.services]]
name = "cas1"
weight = 1
[[tcp.services.app.weighted.services]]
name = "cas2"
weight = 1
[[tcp.services.app.weighted.services]]
name = "cas3"
weight = 1
... cas 4-10

Okay I will try that. Thank you!