New to Voyager? Please start here.

Configure Load Balancing Algorithm

You can configure a backend to use a specific load balancing algorithm using backend.loadBalanceOn. You can specify any HAProxy supported options along with arguments.

Ingress Example:

apiVersion: voyager.appscode.com/v1
kind: Ingress
metadata:
  name: test-ingress
  namespace: demo
spec:
  defaultBackend:
    service:
      name: test-server
      port:
        number: 80
    loadBalanceOn: roundrobin # configure for default backend
  rules:
  - http:
      port: 8989
      paths:
      - backend:
          service:
            name: test-server
            port:
              number: 8989
          loadBalanceOn: static-rr # configure for http backend
  - tcp:
      port: 4545
      backend:
        service:
          name: test-server
          port:
            number: 4545
        loadBalanceOn: leastconn # configure for tcp backend

Generated haproxy.cfg:

# HAProxy configuration generated by https://github.com/appscode/voyager
# DO NOT EDIT!
global
  daemon
  stats socket /var/run/haproxy.sock level admin expose-fd listeners
  server-state-file global
  server-state-base /var/state/haproxy/
  # log using a syslog socket
  log /dev/log local0 info
  tune.ssl.default-dh-param 2048
  ssl-default-bind-ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!3DES:!MD5:!PSK
  lua-load /etc/auth-request.lua
  hard-stop-after 30s
defaults
  log global
  # https://cbonte.github.io/haproxy-dconv/1.7/configuration.html#4.2-option%20abortonclose
  # https://github.com/voyagermesh/voyager/pull/403
  option dontlognull
  option http-server-close
  # Timeout values
  timeout client 50s
  timeout client-fin 50s
  timeout connect 5s
  timeout server 50s
  timeout tunnel 50s
  # Configure error files
  # default traffic mode is http
  # mode is overwritten in case of tcp services
  mode http
frontend http-0_0_0_0-8989
  bind *:8989  
  mode http
  option httplog
  option forwardfor
  acl is_proxy_https hdr(X-Forwarded-Proto) https
  acl is_proxy_https ssl_fc
  http-request set-var(req.scheme) str(https) if is_proxy_https
  http-request set-var(req.scheme) str(http) if ! is_proxy_https
  use_backend test-server.demo:8989 
  default_backend default-backend
backend test-server.demo:8989
  balance static-rr
  server pod-test-server-47lck 172.17.0.5:8989        
frontend tcp-0_0_0_0-4545
  bind *:4545     
  mode tcp
  default_backend test-server.demo:4545
backend test-server.demo:4545
  mode tcp
  balance leastconn
  server pod-test-server-47lck 172.17.0.5:4545     
backend default-backend
  balance roundrobin
  server pod-test-server-47lck 172.17.0.5:8080

Instead of configuring backends you can also specify load balancing algorithm in defaults sections. For that, you need to add balance <algorithm> in defaults.cfg using custom template. See here for detailed example on custom template.