Skip to main content

Ingress Option 3: L4 TCP Load Balancer (HAProxy, Envoy, Keepalived VIP, Cloud LB)

For production high availability with fast sub-second health eviction, place a Layer-4 TCP Passthrough Load Balancer in front of your hermEX backend pods (or stateless edge nodes). DNS hostnames (mail.example.com, autodiscover.example.com, mta-sts.example.com, MX) point to one highly available VIP (203.0.113.50), and the load balancer forwards raw TCP connections across healthy backend nodes.


1. Critical Rule: L4 TCP Passthrough Only (Disable PROXY Protocol)​

Internet / Corporate Clients
SMTP(25/465/587) IMAP(993) POP3(995) HTTPS(443)
|
VIP 203.0.113.50
|
+---------------------+---------------------+
| L4 TCP Passthrough Load Balancer |
| HAProxy / Envoy / Keepalived / Cloud NLB |
| (NO SSL termination, NO PROXY protocol) |
+---------------------+---------------------+
/ \
v v
Pod 0 / Edge 1 Pod 1 / Edge 2
10.0.0.10 10.0.0.11
Always use raw TCP passthrough (PROXY protocol OFF)
  • Do NOT terminate TLS at HAProxy/Envoy on port 443, 993, 995, or 465: hermEX terminates TLS natively inside hermex-gateway, hermex-imap, hermex-pop3, hermex-mta, and hermex-sieve so TLS-ALPN-01 Let's Encrypt ACME, per-domain SNI certificates, and STARTTLS banners work correctly.
  • Do NOT enable send-proxy / PROXY protocol: hermEX listeners expect raw client TCP streams and do not parse HAProxy PROXY v1/v2 preambles.
  • Exception for HTTP if using external L7 SSL offload: Only if you explicitly terminate HTTPS upstream, configure X-Forwarded-For cleanly; however, raw L4 TCP passthrough is strongly recommended because Exchange RPC/HTTP, MAPI/HTTP, TLS-ALPN-01, and binary mail protocols all depend on direct TLS sessions.

2. Port Forwarding Matrix (All 10 Public Ports)​

Public TCP PortProtocol / DaemonIdle Tunnel TimeoutHealth Check Probe
25SMTP (hermex-mta)5mTCP connect (or 220 SMTP check)
465SMTPS implicit TLS (hermex-mta)5mTCP connect on 465
587SMTP Submission STARTTLS (hermex-mta)5mTCP connect on 587
110POP3 (hermex-pop3)10mTCP connect on 110
995POP3S implicit TLS (hermex-pop3)10mTCP connect on 995
143IMAP (hermex-imap)35m (RFC 2177 IDLE)TCP connect on 143
993IMAPS implicit TLS (hermex-imap)35m (RFC 2177 IDLE)TCP connect on 993
4190ManageSieve (hermex-sieve)10mTCP connect on 4190
80Gateway HTTP / ACME HTTP-01 (hermex-gateway)2mTCP connect on 80
443Gateway HTTPS / Webmail / Exchange / JMAP / DAV / ALPN-0110mTCP connect on 443 (or HTTP /api/v1/branding)

3. Option A: Complete HAProxy L4 Configuration​

Install HAProxy on both load balancer nodes (lb-1 at 203.0.113.51 / 10.0.0.6 and lb-2 at 203.0.113.52 / 10.0.0.7):

# RHEL / AlmaLinux / Rocky 9
sudo dnf -y install haproxy

# Debian 12 / Ubuntu
sudo apt-get install -y haproxy

Allow non-local VIP binding on both LB nodes:

echo 'net.ipv4.ip_nonlocal_bind = 1' | sudo tee /etc/sysctl.d/90-vip-bind.conf
sudo sysctl -p /etc/sysctl.d/90-vip-bind.conf

Write the complete /etc/haproxy/haproxy.cfg covering all 10 public mail and Exchange ports:

global
log /dev/log local0
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
user haproxy
group haproxy
daemon
maxconn 20000

defaults
log global
mode tcp
option tcplog
option dontlognull
retries 3
timeout connect 5s
timeout client 10m
timeout server 10m
timeout tunnel 35m
timeout check 3s

# Optional private HAProxy stats dashboard
listen stats
bind 10.0.0.6:8404
mode http
stats enable
stats uri /stats
stats auth admin:REPLACE_STATS_PASSWORD

# --- HTTPS Gateway (Webmail, Admin, EWS, EAS, MAPI/HTTP, RPC/HTTP, DAV, JMAP, ACME TLS-ALPN-01) ---
frontend fe_https
bind 203.0.113.50:443
default_backend be_https

backend be_https
balance leastconn
default-server inter 3s fall 3 rise 2
server pod-0 10.0.0.10:443 check
server pod-1 10.0.0.11:443 check

# --- HTTP Gateway (HTTPS redirect & ACME HTTP-01) ---
frontend fe_http
bind 203.0.113.50:80
default_backend be_http

backend be_http
balance roundrobin
default-server inter 3s fall 3 rise 2
server pod-0 10.0.0.10:80 check
server pod-1 10.0.0.11:80 check

# --- SMTP Inbound (25), SMTPS (465), Submission (587) ---
frontend fe_smtp
bind 203.0.113.50:25
default_backend be_smtp

backend be_smtp
balance roundrobin
default-server inter 3s fall 3 rise 2
server pod-0 10.0.0.10:25 check
server pod-1 10.0.0.11:25 check

frontend fe_smtps
bind 203.0.113.50:465
default_backend be_smtps

backend be_smtps
balance roundrobin
default-server inter 3s fall 3 rise 2
server pod-0 10.0.0.10:465 check
server pod-1 10.0.0.11:465 check

frontend fe_submission
bind 203.0.113.50:587
default_backend be_submission

backend be_submission
balance leastconn
default-server inter 3s fall 3 rise 2
server pod-0 10.0.0.10:587 check
server pod-1 10.0.0.11:587 check

# --- IMAP (143) & IMAPS (993) with 35m IDLE tunnel timeout ---
frontend fe_imap
bind 203.0.113.50:143
timeout client 35m
default_backend be_imap

backend be_imap
balance leastconn
timeout server 35m
default-server inter 3s fall 3 rise 2
server pod-0 10.0.0.10:143 check
server pod-1 10.0.0.11:143 check

frontend fe_imaps
bind 203.0.113.50:993
timeout client 35m
default_backend be_imaps

backend be_imaps
balance leastconn
timeout server 35m
default-server inter 3s fall 3 rise 2
server pod-0 10.0.0.10:993 check
server pod-1 10.0.0.11:993 check

# --- POP3 (110) & POP3S (995) ---
frontend fe_pop3
bind 203.0.113.50:110
default_backend be_pop3

backend be_pop3
balance leastconn
default-server inter 3s fall 3 rise 2
server pod-0 10.0.0.10:110 check
server pod-1 10.0.0.11:110 check

frontend fe_pop3s
bind 203.0.113.50:995
default_backend be_pop3s

backend be_pop3s
balance leastconn
default-server inter 3s fall 3 rise 2
server pod-0 10.0.0.10:995 check
server pod-1 10.0.0.11:995 check

# --- ManageSieve (4190) ---
frontend fe_sieve
bind 203.0.113.50:4190
default_backend be_sieve

backend be_sieve
balance leastconn
default-server inter 3s fall 3 rise 2
server pod-0 10.0.0.10:4190 check
server pod-1 10.0.0.11:4190 check

Validate and start HAProxy (change stats bind IP on lb-2 to 10.0.0.7:8404):

sudo haproxy -c -f /etc/haproxy/haproxy.cfg
sudo setsebool -P haproxy_connect_any 1 2>/dev/null || true
sudo systemctl enable --now haproxy

4. Option B: Complete Envoy Proxy L4 Configuration​

If your platform standardizes on Envoy Proxy (/etc/envoy/envoy.yaml), install Envoy from the official package repository (func-e or distro repo) and configure raw tcp_proxy listeners across all 10 ports with active TCP health checks (idle_timeout: 2100s for 35-minute IMAP IDLE sessions):

admin:
address:
socket_address:
address: 127.0.0.1
port_value: 9901

static_resources:
listeners:
- name: listener_https
address:
socket_address: { address: 203.0.113.50, port_value: 443 }
filter_chains:
- filters:
- name: envoy.filters.network.tcp_proxy
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy
stat_prefix: hermes_https
cluster: cluster_443
idle_timeout: 600s
- name: listener_http
address:
socket_address: { address: 203.0.113.50, port_value: 80 }
filter_chains:
- filters:
- name: envoy.filters.network.tcp_proxy
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy
stat_prefix: hermes_http
cluster: cluster_80
idle_timeout: 120s
- name: listener_smtp
address:
socket_address: { address: 203.0.113.50, port_value: 25 }
filter_chains:
- filters:
- name: envoy.filters.network.tcp_proxy
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy
stat_prefix: hermes_smtp
cluster: cluster_25
idle_timeout: 300s
- name: listener_smtps
address:
socket_address: { address: 203.0.113.50, port_value: 465 }
filter_chains:
- filters:
- name: envoy.filters.network.tcp_proxy
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy
stat_prefix: hermes_smtps
cluster: cluster_465
idle_timeout: 300s
- name: listener_submission
address:
socket_address: { address: 203.0.113.50, port_value: 587 }
filter_chains:
- filters:
- name: envoy.filters.network.tcp_proxy
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy
stat_prefix: hermes_submission
cluster: cluster_587
idle_timeout: 300s
- name: listener_imap
address:
socket_address: { address: 203.0.113.50, port_value: 143 }
filter_chains:
- filters:
- name: envoy.filters.network.tcp_proxy
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy
stat_prefix: hermes_imap
cluster: cluster_143
idle_timeout: 2100s
- name: listener_imaps
address:
socket_address: { address: 203.0.113.50, port_value: 993 }
filter_chains:
- filters:
- name: envoy.filters.network.tcp_proxy
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy
stat_prefix: hermes_imaps
cluster: cluster_993
idle_timeout: 2100s
- name: listener_pop3
address:
socket_address: { address: 203.0.113.50, port_value: 110 }
filter_chains:
- filters:
- name: envoy.filters.network.tcp_proxy
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy
stat_prefix: hermes_pop3
cluster: cluster_110
idle_timeout: 600s
- name: listener_pop3s
address:
socket_address: { address: 203.0.113.50, port_value: 995 }
filter_chains:
- filters:
- name: envoy.filters.network.tcp_proxy
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy
stat_prefix: hermes_pop3s
cluster: cluster_995
idle_timeout: 600s
- name: listener_sieve
address:
socket_address: { address: 203.0.113.50, port_value: 4190 }
filter_chains:
- filters:
- name: envoy.filters.network.tcp_proxy
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy
stat_prefix: hermes_sieve
cluster: cluster_4190
idle_timeout: 600s

clusters:
- name: cluster_443
type: STATIC
lb_policy: LEAST_REQUEST
connect_timeout: 3s
health_checks:
- timeout: 2s
interval: 3s
unhealthy_threshold: 3
healthy_threshold: 2
tcp_health_check: {}
load_assignment:
cluster_name: cluster_443
endpoints:
- lb_endpoints:
- endpoint: { address: { socket_address: { address: 10.0.0.10, port_value: 443 } } }
- endpoint: { address: { socket_address: { address: 10.0.0.11, port_value: 443 } } }
# Repeat identical cluster blocks for ports 80, 25, 465, 587, 143, 993, 110, 995, and 4190

Validate Envoy syntax before starting the service:

sudo envoy --mode validate -c /etc/envoy/envoy.yaml
sudo systemctl enable --now envoy

5. Option C: Keepalived VRRP VIP Between lb-1 and lb-2​

Run Keepalived across lb-1 (203.0.113.51) and lb-2 (203.0.113.52) so the public VIP 203.0.113.50 fails over automatically if an LB host drops offline.

  1. Create /usr/local/bin/check_lb.sh on both LB hosts (replace haproxy with envoy if using Option B):
    #!/bin/bash
    set -euo pipefail
    pgrep -x haproxy >/dev/null
    sudo chmod +x /usr/local/bin/check_lb.sh
  2. Open VRRP (protocol 112) and public mail ports on both LB nodes:
    # firewalld
    for p in 25/tcp 80/tcp 110/tcp 143/tcp 443/tcp 465/tcp 587/tcp 993/tcp 995/tcp 4190/tcp; do
    sudo firewall-cmd --permanent --add-port="$p"
    done
    sudo firewall-cmd --permanent --add-rich-rule='rule protocol value="vrrp" accept'
    sudo firewall-cmd --reload
  3. Write /etc/keepalived/keepalived.conf on lb-1 (MASTER, priority 150; set BACKUP, priority 100, swapped unicast_src_ip/unicast_peer on lb-2):
    vrrp_script chk_lb {
    script "/usr/local/bin/check_lb.sh"
    interval 2
    fall 2
    rise 2
    }

    vrrp_instance VI_HERMEX_PUBLIC {
    state MASTER
    interface eth0
    virtual_router_id 71
    priority 150
    advert_int 1
    unicast_src_ip 203.0.113.51
    unicast_peer {
    203.0.113.52
    }
    authentication {
    auth_type PASS
    auth_pass REPLACE_PUBLIC_VRRP_SECRET
    }
    virtual_ipaddress {
    203.0.113.50/24 dev eth0
    }
    track_script {
    chk_lb
    }
    }
    sudo systemctl enable --now keepalived

6. Option D: Cloud L4 Load Balancer Reference Matrix​

If you use a managed cloud L4 load balancer instead of HAProxy/Envoy, configure raw TCP pass-through listeners for every port below with PROXY protocol disabled:

Cloud ServiceListener ModeTarget / Health CheckClient IP & Outbound Notes
Hetzner Cloud Load BalancerTCP (Proxy Protocol off)Target Type: Private Network Server (10.0.0.10, 10.0.0.11); Health Check: TCP on each port (interval 5s, timeout 3s, retries 3)Public outbound mail exits via pod IPs; set PTR + SPF on each pod's public IPv4.
AWS Network Load Balancer (NLB)TCP Listener (PROXY protocol v2 off)Target Group type: IP (10.0.0.10, 10.0.0.11); Health Check: TCP port checkNLB preserves client IP at L4; for outbound mail use Elastic IPs with matching AWS rDNS PTR + SPF, or route via static NAT gateways listed in SPF.
DigitalOcean Load BalancerTCP Pass-Through (Enable Proxy Protocol off)Health Check: TCP port 443 / 993 / 25Ensure firewall allows LB health-check source subnet to reach pod ports 25, 80, 110, 143, 443, 465, 587, 993, 995, 4190.

7. Failover Verification​

  1. Point mail.example.com, autodiscover.example.com, autoconfig.example.com, mta-sts.example.com, and MX to the VIP (203.0.113.50).
  2. Test external connections through the VIP while Pod 1 is stopped briefly:
    # On Pod 1
    sudo systemctl stop hermex-gateway.service hermex-imap.service
  3. Confirm HAProxy/Envoy marks pod-1 DOWN within ~6 seconds and that new HTTPS (443), IMAP (993), POP3 (995), and SMTP (25, 587) sessions through 203.0.113.50 succeed on Pod 0 without connection stalls:
    curl -sf https://mail.example.com/api/v1/branding
    openssl s_client -connect mail.example.com:993 -servername mail.example.com -quiet </dev/null
  4. Bring Pod 1 services back up (sudo systemctl start hermex-gateway.service hermex-imap.service) and confirm health status returns to UP.