Ana içeriğe geç

Giriş Seçeneği 3: L4 TCP Load Balancer (HAProxy, Envoy, Keepalived VIP, Bulut LB)

Saniyeler içinde otomatik sağlık kesintisi ve kesintisiz yüksek erişilebilirlik için hermEX backend pod'larının (veya durumsuz edge düğümlerinin) önüne bir Layer-4 TCP Passthrough Load Balancer yerleştirin. Tüm DNS kayıtları (mail.example.com, autodiscover.example.com, mta-sts.example.com, MX) tek bir yüksek erişilebilir VIP adresine (203.0.113.50) bakar; load balancer gelen ham TCP bağlantılarını sağlıklı düğümlere dağıtır.


1. Kritik Kural: Yalnızca L4 TCP Passthrough (PROXY Protocol Kapalı)​

İnternet / Kurumsal İstemciler
SMTP(25/465/587) IMAP(993) POP3(995) HTTPS(443)
|
VIP 203.0.113.50
|
+---------------------+---------------------+
| L4 TCP Passthrough Load Balancer |
| HAProxy / Envoy / Keepalived / Bulut NLB |
| (TLS sonlandırma YOK, PROXY proto YOK) |
+---------------------+---------------------+
/ \
v v
Pod 0 / Edge 1 Pod 1 / Edge 2
10.0.0.10 10.0.0.11
Her zaman ham TCP passthrough kullanın (PROXY protocol KAPALI)
  • 443, 993, 995 ve 465 portlarında HAProxy veya Envoy üzerinde TLS sonlandırmayın: TLS-ALPN-01 Let's Encrypt ACME, alan adı bazlı SNI sertifika seçimi ve STARTTLS akışlarının çalışması için TLS bağlantısını doğrudan hermex-gateway, hermex-imap, hermex-pop3, hermex-mta ve hermex-sieve sonlandırır.
  • send-proxy veya PROXY protocol açmayın: hermEX TCP dinleyicileri doğrudan ham istemci TCP akışı bekler ve HAProxy PROXY v1/v2 ön ekini ayrıştırmaz.
  • Yalnızca L7 HTTPS sonlandırmasını özel olarak başka bir proxy üzerinde yapmak zorundaysanız X-Forwarded-For başlığını koruyun; ancak Exchange RPC/HTTP, MAPI/HTTP, TLS-ALPN-01 ve ikili posta protokolleri için ham L4 TCP passthrough kesinlikle önerilen yöntemdir.

2. Port Yönlendirme Matrisi (Tüm 10 Genel Port)​

Genel TCP PortuProtokol / DaemonBoşta Oturum TimeoutSağlık Yoklaması
25SMTP (hermex-mta)5mTCP connect
465SMTPS implicit TLS (hermex-mta)5m465 TCP connect
587SMTP Submission STARTTLS (hermex-mta)5m587 TCP connect
110POP3 (hermex-pop3)10m110 TCP connect
995POP3S implicit TLS (hermex-pop3)10m995 TCP connect
143IMAP (hermex-imap)35m (RFC 2177 IDLE)143 TCP connect
993IMAPS implicit TLS (hermex-imap)35m (RFC 2177 IDLE)993 TCP connect
4190ManageSieve (hermex-sieve)10m4190 TCP connect
80Gateway HTTP / ACME HTTP-01 (hermex-gateway)2m80 TCP connect
443Gateway HTTPS / Webmail / Exchange / JMAP / DAV / ALPN-0110m443 TCP connect

3. Seçenek A: Eksiksiz HAProxy L4 Yapılandırması​

İki load balancer sunucusuna (lb-1 203.0.113.51 / 10.0.0.6 ve lb-2 203.0.113.52 / 10.0.0.7) HAProxy kurun:

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

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

Yerel olmayan VIP bağlamasını açın:

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

Tüm 10 genel portu kapsayan /etc/haproxy/haproxy.cfg dosyasını yazın (lb-2 üzerinde stats bind IP'sini 10.0.0.7:8404 yapın):

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

# İsteğe bağlı özel HAProxy durum ekranı
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 yönlendirme ve 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 (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) ve IMAPS (993) - 35 dk IDLE 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) ve 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

Doğrulayıp başlatın:

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. Seçenek B: Eksiksiz Envoy Proxy L4 Yapılandırması​

Altyapınızda Envoy Proxy kullanıyorsanız /etc/envoy/envoy.yaml içinde tüm 10 port için ham tcp_proxy dinleyicileri ve aktif TCP sağlık kontrolleri tanımlayın (idle_timeout: 2100s 35 dakikalık IMAP IDLE oturumlarını korur):

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 } } }
# Aynı cluster bloğunu 80, 25, 465, 587, 143, 993, 110, 995 ve 4190 portları için tekrarlayın

Yapılandırmayı doğrulayıp başlatın:

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

5. Seçenek C: lb-1 ve lb-2 Arasında Keepalived VRRP VIP​

LB sunucularından biri kapanırsa genel VIP 203.0.113.50 adresini otomatik devretmek için lb-1 (203.0.113.51) ve lb-2 (203.0.113.52) üzerinde Keepalived çalıştırın.

  1. İki LB sunucusunda /usr/local/bin/check_lb.sh betiğini oluşturun (envoy kullanıyorsanız haproxy yerine envoy yazın):
    #!/bin/bash
    set -euo pipefail
    pgrep -x haproxy >/dev/null
    sudo chmod +x /usr/local/bin/check_lb.sh
  2. Güvenlik duvarında 10 genel TCP portunu ve VRRP (protocol 112) trafiğini açın:
    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. lb-1 üzerinde /etc/keepalived/keepalived.conf (MASTER, priority 150; lb-2 üzerinde BACKUP, priority 100 ve IP'leri yer değiştirin):
    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. Seçenek D: Bulut L4 Load Balancer Başvuru Tablosu​

HAProxy veya Envoy yerine yönetilen bulut L4 load balancer kullanıyorsanız her port için PROXY protocol kapalı ham TCP pass-through tanımlayın:

Bulut HizmetiDinleyici ModuHedef ve Sağlık Kontrolüİstemci IP ve Giden Posta Notları
Hetzner Cloud Load BalancerTCP (Proxy Protocol kapalı)Hedef: Private Network Server (10.0.0.10, 10.0.0.11); Sağlık kontrolü: her port için TCP (interval 5s, timeout 3s, retries 3)Giden SMTP pod genel IP'lerinden çıkar; her pod genel IPv4 adresine PTR + SPF ekleyin.
AWS Network Load Balancer (NLB)TCP Listener (PROXY protocol v2 kapalı)Target Group türü: IP (10.0.0.10, 10.0.0.11); Sağlık kontrolü: TCP port yoklamasıNLB L4 düzeyinde istemci IP adresini korur; giden posta için AWS rDNS PTR + SPF tanımlı Elastic IP veya sabit NAT gateway kullanın.
DigitalOcean Load BalancerTCP Pass-Through (Enable Proxy Protocol kapalı)Sağlık kontrolü: TCP 443 / 993 / 25Güvenlik duvarında LB sağlık yoklama alt ağının tüm portlara (25..4190) erişmesine izin verin.

7. Failover Doğrulaması​

  1. mail.example.com, autodiscover.example.com, autoconfig.example.com, mta-sts.example.com ve MX kayıtlarını VIP (203.0.113.50) adresine yönlendirin.
  2. Pod 1 servislerini geçici durdurarak VIP üzerinden bağlantıyı test edin:
    # Pod 1 üzerinde
    sudo systemctl stop hermex-gateway.service hermex-imap.service
  3. HAProxy veya Envoy'un yaklaşık 6 saniye içinde pod-1 hedefini DOWN işaretlediğini ve 203.0.113.50 üzerinden gelen HTTPS (443), IMAP (993), POP3 (995) ve SMTP (25, 587) isteklerinin kesintisiz Pod 0 üzerinden çalıştığını doğrulayın:
    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. Pod 1 servislerini yeniden başlatın (sudo systemctl start hermex-gateway.service hermex-imap.service) ve sağlığın UP durumuna döndüğünü görün.