HAProxy の例
HAProxy は Linux で動作する無料のロードバランサーです。これは非常に強力であり、既成で監視機能をサポートしています。
そのコンフィギュレーションについては、https://cbonte.github.io/haproxy-dconv/2.0/configuration.html を参照してください。
グローバル設定
グローバル設定はフロントエンドとバックエンドサーバー全体に適用され、通常は無視できます。
以下に例を示します。
#--------------------------------------------------------------------- #
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
デフォルト設定
これらの設定はバックエンドサーバーのデフォルトのコンフィギュレーションに使用されます。
log
グローバル設定で設定されているログを使用します。
mode
リクエストの解析方法を指定します。この例では、http 処理を使用します。
option
以下に例を示します。
#---------------------------------------------------------------------
# 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
フロントエンド設定
これはすべてのアプリケーションが通信するフロントエンドです。これには受信リクエストの処理に使用されるバックエンドサーバーを決定するルーティング規則が含まれています。
bind
受信リクエストを受信するポートを指定します。
capture
HAProxy http リクエストのログにセッション ID cookie を含めます。
acl
チェックする条件に基づいて変数を作成します。たとえば、-i -m beg <string><string> で始まる URI をチェックします。
use_backend
変数の値が true か false かに基づいて、特定のグループのバックエンドサーバーにルーティングします。
default
ほかの規則と一致しない場合に使用されるバックエンド。
以下に例を示します。
#---------------------------------------------------------------------
# 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
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
バックエンドプラットフォームの設定
バックエンドは、フロントエンドおよび特定のコンフィギュレーションから渡されたリクエストを処理可能なサーバーのグループです。
balance
負荷分散アルゴリズム。デフォルトはラウンドロビン方式であり、使用可能なサーバーに順番に次の接続を割り当てます。
cookie
cookie を挿入します。ロードバランサーはこれを使用して将来のリクエストを同じサーバーにルーティングします。
以下に例を示します。
#---------------------------------------------------------------------
# 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
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
バックエンド接続サーバーの設定
例:
#---------------------------------------------------------------------
# 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 には、トラフィックフローの表示に使用可能な監視 UI があります。受信ポートとオプションの資格証明を設定できます。
#---------------------------------------------------------------------
#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
完全な例
#---------------------------------------------------------------------
# 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
これは役に立ちましたか?