HAProxyでロードバランシング
サーバの負荷分散ができるHAProxyを使ってみました。
サーバ構成
役割
|
IP
|
備考
|
---|---|---|
webサーバ | 192.168.1.121 | index.htmlを配置 |
webサーバ | 192.168.1.122 | index.htmlを配置 |
HAProxy | 192.168.1.123 | |
webサーバ | 192.168.1.124 | index.txt |
構築
HAProxy(192.168.1.123)
インストール
$ sudo yum -y install haproxy
設定ファイル
$ cat /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2chroot /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/statsdefaults
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 3000frontend main *:80
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js .htmluse_backend static if url_static
default_backend appbackend static
balance roundrobin
server static 192.168.1.121:80 check
server static 192.168.1.122:80 checkbackend app
balance roundrobin
server app1 192.168.1.124:80 check
.htmlファイルは192.168.1.121と192.168.1.122に、
そのほかのファイルは192.168.1.124に接続されるよう設定しました。
起動
$ sudo /etc/init.d/haproxy start
webサーバ(192.168.1.121、192.168.1.122、192.168.1.124)
インストール
設定ファイル
192.168.1.121サーバ
$ cat /var/www/html/index.html
<html>
hoge1
</html>
192.168.1.122サーバ
$ cat /var/www/html/index.html
<html>
hoge2
</html>
192.168.1.123サーバ
$ cat /var/www/html/index.txt
hoge4.txt
起動
sudo /etc/init.d/httpd start
実行
index.htmlにアクセスする。
$ curl 192.168.1.123/index.html
<html>
hoge1
</html>
$ curl 192.168.1.123/index.html
<html>
hoge2
</html>
$ curl 192.168.1.123/index.html
<html>
hoge1
</html>
$ curl 192.168.1.123/index.html
<html>
hoge2
</html>
192.168.1.121と192.168.1.122交互にアクセスしています
index.txtにアクセスする。
$ curl 192.168.1.123/index.txt
hoge4.txt$ curl 192.168.1.123/index.txt
hoge4.txt
192.168.1.124のみアクセスしています
無事負荷分散できました!
参考URL
オデの日記@WEB系: L7ロードバランサHaproxyを使う(その2 設定から起動)