Auto register container to (global) loadbalancer

Hello,

I built a revproxy with caddy webserver and docker-gen which works great!
Now I need a TCP Loadbalancer, but the container should register during start automatically to the LB by container label / exposed port.

Is there an example / guide how this could be done?

Regards

Hi! I remember that I wanted something similar a while ago and someone was kind enough to share their efforts

I have not tried it but the approach looks good.

Best regards,

Alejandro

There’s a similar project in rancher catalog called traefik, I believe it’s on github and open sourced, you will probably find your answers there. It does exactly what you are trying to achieve but with its own LB service layer, but all based on rancher events somehow.

I’m currently using a tcp load balancer to caddy (to proxy my https2 traffic), caddy then forwards traffic to an internal load balancer that then does what its supposed to do.

This is what my caddyfile looks like:

:443 {
  tls /var/hugo/keys/prod.crt /var/hugo/keys/prod.key
  proxy / http://ssl-inbound-intel:80 {
    policy least_conn
    proxy_header Host {host}
    proxy_header X-Real-IP {remote}
    proxy_header X-Forwarded-Proto {scheme}
  }
}

I should also mention that its considerably faster than regular https haproxy that comes with rancher – at least it feels like it. That might just be the http2 talking though…

I created a caddy image which could be used as webserver or reverse proxy (stack with a prepared docker-gen image, caddy image is just tagged with “revproxy” because of needed reload…). Works fine for me…

Here is my docker-compose from the generated stack. It should work out of the box as global service (multi host).

caddy:
  ports:
  - 443:443/tcp
  - 80:80/tcp
  labels:
    io.rancher.scheduler.global: 'true'
    io.rancher.sidekicks: docker-gen
  command:
  - -email=<your-certadmin@yourdomain.com>
  tty: true
  image: itdengler/caddy:revproxy
  volumes:
  - /home/httpd/conf
  - caddy-revproxy-certs:/home/httpd/.caddy
  - caddy-revproxy-logs:/home/httpd/logs
  stdin_open: true
docker-gen:
  labels:
    io.rancher.scheduler.global: 'true'
  tty: true
  image: itdengler/caddy-revproxy-gen
  volumes:
  - /var/run/docker.sock:/var/run/docker.sock:ro
  - /usr/bin/docker:/usr/bin/docker:ro
  volumes_from:
  - caddy
  stdin_open: true

Add backend webservers with the following env vars.

Public domain
-e VIRTUAL_HOST=example.com

Port different to 80? Set it… (my caddy webserver image use 80 as default)
-e VIRTUAL_port=2015

Push additional caddy options to the reverse proxy (delimiter == “;”)?
-e CADDY_OPTS="tls off;basicauth / username password"

Also Loadbalancing works fine. Scale up your backend stack and all the internal ip addresses added to caddy proxy (round robin).