HAProxy-Beispiel
HAProxy ist ein kostenloses Lastenausgleichstool, das in Linux ausgeführt wird. Es ist sehr leistungsstark und unterstützt standardmäßig Überwachungsfunktionen.
Weitere Informationen zur Konfiguration finden Sie unter https://cbonte.github.io/haproxy-dconv/2.0/configuration.html.
Globale Einstellungen
Globale Einstellungen werden auf Frontend- und Backend-Server angewendet und können im Allgemeinen ignoriert werden.
Zum Beispiel:
#--------------------------------------------------------------------- #
Global settings
#---------------------------------------------------------------------
global
# setup logging and force level to debug. Logs require rsyslog be setup.
chroot /var/lib/haproxy
log /dev/log local0 debug
# maximum number of connections allowed
maxconn 10000
# turn on stats unix socket
stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
stats timeout 30s
# user and group haproxy will run as
user haproxy
group haproxy
Standardeinstellungen
Diese Einstellungen gelten für Standardkonfigurationen der Backend-Server.
log
Verwenden Sie die in den globalen Einstellungen konfigurierte Protokollierung.
mode
Bestimmt, wie Anforderungen geparst werden. In diesem Beispiel verwenden wir die HTTP-Verarbeitung.
option
Zum Beispiel:
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
default-server init-addr last,libc,none
log global
mode http
option httplog
option redispatch
option forwardfor
timeout connect 10s
timeout client 60s
timeout server 60s
timeout tunnel 1h
FrontEnd-Einstellungen
Dies ist das Frontend, das alle Anwendungen durchlaufen. Es enthält die Routing-Regeln, die bestimmen, welcher Backend-Server für die Verarbeitung der eingehenden Anforderung verwendet wird.
bind
Gibt den Port an, an dem eingehende Anforderungen empfangen werden.
capture
Schließt das Sitzungs-ID-Cookie in die HAProxy-HTTP-Anforderungsprotokollierung ein.
acl
Erstellt eine Variable basierend auf der Bedingung, die geprüft wird. Beispielsweise prüft -i -m beg <string> den URI, der mit <string> beginnt.
use_backend
Wird basierend auf dem Wert "true" oder "false" von Variablen zu einer bestimmten Gruppe von Backend-Servern weitergeleitet.
default
Das verwendete Backend, wenn es nicht mit den anderen Regeln übereinstimmt.
Zum Beispiel:
#---------------------------------------------------------------------
# FrontEnd Configuration
#---------------------------------------------------------------------
frontend ft_web
bind *:8080 name web
# log the session cookie if passed
capture cookie JSESSIONID= len 32
## path based routing to connection server
acl path_cxserver path -i -m beg /Thingworx/WS
acl path_tunnelserver path -i -m beg /Thingworx/WSTunnelServer
acl path_tunnelclient path -i -m beg /Thingworx/WSTunnelClient
use_backend cxserver if path_cxserver or path_tunnelserver or path_tunnelclient

# default traffic to platform
default_backend platform
Beispiel für ThingWorx Flow
#---------------------------------------------------------------------
# FrontEnd Configuration
#---------------------------------------------------------------------
frontend ft_web
bind *:8443 ssl crt /certs/haproxy.pem name sslweb
# log the session cookie if passed
capture cookie JSESSIONID= len 32
##path based routing to ThingWorx Flow
acl p_flow1 path -i -m beg /Thingworx/Composer/apps/flow
acl p_flow2 path -i -m beg /Thingworx/Flow
acl p_flow3 path -i -m beg /Thingworx/Triggers
acl p_flow4 path -i -m beg /Thingworx/Lookups
acl p_flow5 path -i -m beg /Thingworx/Oauths
acl p_flow6 path -i -m beg /Thingworx/Subsystems/EventProcessingSubsystem/Subscriptions
acl p_flow7 path -i -m beg /enterprise/v1/fetchconfig
use_backend flow if p_flow1 or p_flow2 or p_flow3 or p_flow4 or p_flow5 or p_flow6 or p_flow7
Einstellungen für die Backend-Plattform
Das Backend ist eine Gruppe von Servern, die vom Frontend übergebene Anforderungen und alle spezifischen Konfigurationen verarbeiten kann.
balance
Der Lastenausgleichsalgorithmus. Der Standardwert ist "round robin", wobei die verfügbaren Server auf der Suche nach der nächsten verfügbaren Verbindung durchlaufen werden.
cookie
Fügt ein vom Lastenausgleich verwendetes Cookie ein, um zukünftige Anforderungen an denselben Server weiterzuleiten.
Zum Beispiel:
#---------------------------------------------------------------------
# BackEnd Platform Configuration
#---------------------------------------------------------------------
backend platform
balance roundrobin
# sticky sessions
cookie SERVER insert indirect nocache
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
# health check
option httpchk GET /Thingworx/health
# configure platform instances
server platform1 10.0.10.10:8080 check inter 1000 fastinter 1000 cookie twx1
server platform2 10.0.10.11:8080 check inter 1000 fastinter 1000 cookie twx2
Beispiel für ThingWorx Flow
#---------------------------------------------------------------------
# BackEnd Flow Configuration
#---------------------------------------------------------------------
backend flow
balance source

option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }

# configure flow instances
server nginx {nginx_ip}:443 check ssl verify none
Einstellungen für den Backend-Connection Server
Beispiel:
#---------------------------------------------------------------------
# BackEnd Connection Server Configuration
#---------------------------------------------------------------------
backend cxserver
balance source
#hash-type consistent
option httpchk GET /
# configure connection server instances
server cxserver1 10.0.10.20:8080 check port 9009
server cxserver2 10.0.10.21:8080 check port 9009
Überwachung
HAProxy verfügt über eine Überwachungsbenutzeroberfläche, die zum Anzeigen des Datenverkehrsflusses verwendet werden kann. Sie können den eingehenden Port und optionale Anmeldeinformationen konfigurieren.
#---------------------------------------------------------------------
#HAProxy Monitoring Config
#---------------------------------------------------------------------
listen stats
bind *:1936
mode http
option forwardfor
option httpclose
stats enable
stats uri /
stats refresh 5s
stats show-legends
stats realm Haproxy\ Statistics
Vollständiges Beispiel
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# setup logging and force level to debug. Logs require rsyslog be setup.
chroot /var/lib/haproxy
log /dev/log local0 debug
# maximum number of connections allowed
maxconn 10000
# turn on stats unix socket
stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
stats timeout 30s
# user and group haproxy will run as
user haproxy
group haproxy
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
default-server init-addr last,libc,none
log global
mode http
option httplog
option redispatch
option forwardfor
timeout connect 10s
timeout client 60s
timeout server 60s
timeout tunnel 1h
#---------------------------------------------------------------------
# FrontEnd Configuration
#---------------------------------------------------------------------
frontend ft_web
bind *:8080 name web
# log the session cookie if passed
capture cookie JSESSIONID= len 32
## path based routing to connection server
acl path_cxserver path -i -m beg /Thingworx/WS
acl path_tunnelserver path -i -m beg /Thingworx/WSTunnelServer
acl path_tunnelclient path -i -m beg /Thingworx/WSTunnelClient
use_backend cxserver if path_cxserver or path_tunnelserver or path_tunnelclient
# default traffic to platform
default_backend platform
#---------------------------------------------------------------------
# BackEnd Platform Configuration
#---------------------------------------------------------------------
backend platform
balance roundrobin
# sticky sessions
cookie SERVER insert indirect nocache
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
# health check
option httpchk GET /Thingworx/health
# configure platform instances
server platform1 10.0.10.10:8080 check inter 1000 fastinter 1000" cookie twx1
server platform2 10.0.10.11:8080 check inter 1000 fastinter 1000" cookie twx2
#---------------------------------------------------------------------
# BackEnd Connection Server Configuration
#---------------------------------------------------------------------
backend cxserver
balance source
#hash-type consistent
option httpchk GET /
# configure connection server instances
server cxserver1 10.0.10.20:8080 check port 9009
server cxserver2 10.0.10.21:8080 check port 9009
#---------------------------------------------------------------------
#HAProxy Monitoring Config
#---------------------------------------------------------------------
listen stats
bind *:1936
mode http
option forwardfor
option httpclose
stats enable
stats uri /
stats refresh 5s
stats show-legends
stats realm Haproxy\ Statistics
War dies hilfreich?