LinuCエヴァンジェリストの鯨井貴博@opensourcetechです。
前回の記事では、Keepalived & HAProxyを使ってLoad Balancerを構築しましたが、
今回はHAProxyのACLを使った振り分けサーバの制御をしてみます。
なお、本家サイトは、以下となります。
https://www.haproxy.com/blog/introduction-to-haproxy-acls/
https://www.haproxy.com/documentation/hapee/latest/onepage/#7
まず、ACLの構文は以下の通り。
先頭のaclは固定。
is_staticとあるのが、acl名。
path以降が、条件となります。
上記の場合、パスに「/static/」で始まる(-m beg)ものが含まれるという条件となります。
ACL名ですが、大小英語・数字・-(ダッシュ)・_(アンダースコア)・.(ドット)・:(コロン)が使用可能で、大文字小文字の区別が行われます。
criterionの部分は、0or1・整数・IPアドレス・文字列・データブロックが使用できます。
フラグは、-i(大小文字区別しない)・-m(マッチング方法指定)・-f(ファイルからパターン読込)・-n(DNS解決を未使用)などがあります。
文字列の完全一致(str)・部分一致(sub)・前方一致(beg)・後方一致(end)なども利用できます。
その他、書ききれないくらいあるので、そろそろ利用例の紹介にいきます。
実際に使用した例の意味は、以下となります。
acl firefox hdr_sub(User-Agent) -i Firefox
→こちらは、HTTPヘッダーのUser-AgentがFirefoxだった場合というACL
acl host hdr_sub(Host) -i 192.168.1.220
→こちらは、HTTPヘッダーのHostが192.168.1.220だった場合というACL
use_backend app2 if host
→ACL hostに適合した場合、バックエンドapp2にアクセスを割り振る
use_backend app if firefox
→ACL firefoxに適合した場合、バックエンドappにアクセスを割り振る
default_backend app
→それ以外の際には、デフォルトバックエンドappにアクセスを割り振る
なお、use_backendについては、上から適合するか見ていくので、記述する順番には注意が必要です。
[root@localhost ~]# cat /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
#frontend main *:5000
# acl url_static path_beg -i /static /images /javascript /stylesheets
# acl url_static path_end -i .jpg .gif .png .css .js
#
# use_backend static if url_static
# default_backend app
frontend main *:80
option forwardfor
acl firefox hdr_sub(User-Agent) -i Firefox ===ここから===
acl host hdr_sub(Host) -i 192.168.1.220
# acl site2 src 192.168.1.131
# use_backend app2 if site2
use_backend app2 if host
use_backend app if firefox
default_backend app ===ここまで===
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
balance roundrobin
server static 127.0.0.1:4331 check
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
#backend app
# balance roundrobin
# server app1 127.0.0.1:5001 check
# server app2 127.0.0.1:5002 check
# server app3 127.0.0.1:5003 check
# server app4 127.0.0.1:5004 check
backend app
# balance source
balance roundrobin
server sv1 192.168.1.254:10001 check weight 1
server sv2 192.168.1.254:10002 check weight 2
backend app2
# balance source
balance roundrobin
server sv3 192.168.1.254:20001 check weight 1
server sv4 192.168.1.254:20002 check weight 2