はじめに
- lvs と keepalived を使った負荷分散システムについて復習してみる
- 過去に何度か構築したけど忘れていたのでメモ
- さらに
Debian環境では初めてなのでメモ
参考
以下がとても参考になった...というか、おそらく、過去に構築した時にも参考にしたはず。
比較的に新しい記事。用語やアーキテクチャの復習になった。
iptables については以下を参考にした。
環境、構成
以下のような構成をを想定している。

lvs
real server
- web01(Debian 6.0.7 / nginx)
lvs の構築
apt-get update をしておく。
keepalived のインストール
apt-get install keepalived
keepalived.conf の設定
設定のポイントは下記の通り。
virtual_router_idpriorityauthenticationでtype PASS時はパスワードをMasterとSlaveで合わせるvirtual_ipaddresslb_algoとlb_kind
等、書きだしたらキリが無い。
Master
こちらをそのまま利用させて頂いた。(IP アドレスは別途設定)
# Global Configuration
global_defs {
notification_email {
notification@domain.org
}
notification_email_from keepalived@domain.org
smtp_server localhost
smtp_connect_timeout 30
router_id LVS_MASTER # string identifying the machine
}
# describe virtual service ip
vrrp_instance VI_1 {
# initial state
state MASTER
interface eth0
# arbitary unique number 0..255
# used to differentiate multiple instances of vrrpd
virtual_router_id 1
# for electing MASTER, highest priority wins.
# to be MASTER, make 50 more than other machines.
priority 100
authentication {
auth_type PASS
auth_pass hogehoge
}
virtual_ipaddress {
xxx.xxx.xxx.100/24
}
# Invoked to master transition
notify_master "/etc/keepalived/bypass_ipvs.sh del xxx.xxx.xxx.100"
# Invoked to slave transition
notify_backup "/etc/keepalived/bypass_ipvs.sh add xxx.xxx.xxx.100"
# Invoked to fault transition
notify_fault "/etc/keepalived/bypass_ipvs.sh add xxx.xxx.xxx.100"
}
# describe virtual web server
virtual_server xxx.xxx.xxx.100 80 {
delay_loop 30
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server xxx.xxx.xxx.79 80 {
HTTP_GET {
url {
path /index.html
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 2
}
}
}
Slave
こちらをそのまま利用させて頂いた。(IP アドレスは別途設定)
# Global Configuration
global_defs {
notification_email {
notification@domain.org
}
notification_email_from keepalived@mailinblack.org
smtp_server localhost
smtp_connect_timeout 30
router_id LVS_MASTER # string identifying the machine
}
# describe virtual service ip
vrrp_instance VI_1 {
# initial state
state BACKUP
interface eth0
# arbitary unique number 0..255
# used to differentiate multiple instances of vrrpd
virtual_router_id 1
# for electing MASTER, highest priority wins.
# to be MASTER, make 50 more than other machines.
priority 50
authentication {
auth_type PASS
auth_pass hogehoge
}
virtual_ipaddress {
xxx.xxx.xxx.100/24
}
# Invoked to master transition
notify_master "/etc/keepalived/bypass_ipvs.sh del xxx.xxx.xxx.100"
# Invoked to slave transition
notify_backup "/etc/keepalived/bypass_ipvs.sh add xxx.xxx.xxx.100"
# Invoked to fault transition
notify_fault "/etc/keepalived/bypass_ipvs.sh add xxx.xxx.xxx.100"
}
# describe virtual web server
virtual_server xxx.xxx.xxx.100 80 {
delay_loop 30
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server xxx.xxx.xxx.79 80 {
HTTP_GET {
url {
path /index.html
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 2
}
}
}
Master と Slave の切り替えシェルスクリプトの設置
こちらを利用して Master と Slave の切り替えシェルスクリプトを Master サーバー と Slave サーバー に設置する。
cd /etc/keepalived/ wget http://gcharriere.com/blog/wp-content/uploads/2009/10/bypass_ipvs.sh chmod 755 /etc/keepalived/bypass_ipvs.sh
sysctl.conf の設定
sysctl.conf の以下をコメントアウトを外して IP フォワードを有効にする。
net.ipv4.ip_forward = 1
コメントアウトを外した後、sysctrl -p で設定を反映させる。
keepalived の再起動
/etc/init.d/keepalived restart
Slave → Master の順番で keepalived の再起動を行う。
実サーバーの構築
やらないといけないこと。
- Web サーバーの設定
iptablesの設定(仮想 IP を受け付けられるようにする)
nginx のインストール
apt-get install nginx
nginx の設定
mkdir /var/www echo "hello" > /var/www/index.html
iptables の設定
実サーバー側で仮想 IP 宛のアクセスも受けられるように iptables の設定を行う。
iptables -A PREROUTING -t nat -d xxx.xxx.xxx.100 -p tcp -j REDIRECT
設定後、以下のように確認する。
iptables -t nat --list
正しく設定されていれば以下のように表示される。
Chain PREROUTING (policy ACCEPT) target prot opt source destination REDIRECT tcp -- anywhere xxx.xxx.xxx.100 Chain POSTROUTING (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
iptables の設定が再起動時にも反映されるように準備する
若干、話が逸れてしまうが、iptables の設定が再起動後に反映されるように準備をする。
iptables-persistent のインストール
iptables-persistent は /etc/iptables/rules の設定を読み込んで iptables を自動で設定するツール(だと思う)。
sudo apt-get install iptables-persistent
既存の iptables の設定を書き出し
iptables-save を使って既存の iptables の設定を書き出す。
sudo iptables-save > /etc/iptables/rules
以下のような出力が得られる。
# Generated by iptables-save v1.4.8 on Fri Oct 4 05:42:53 2013 *filter :INPUT ACCEPT [19856:1771669] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [7376:467232] COMMIT # Completed on Fri Oct 4 05:42:53 2013 # Generated by iptables-save v1.4.8 on Fri Oct 4 05:42:53 2013 *nat :PREROUTING ACCEPT [32:2834] :POSTROUTING ACCEPT [3:195] :OUTPUT ACCEPT [3:195] -A PREROUTING -d xxx.xxx.xxx.100/32 -p tcp -j REDIRECT COMMIT # Completed on Fri Oct 4 05:42:53 2013
次回の再起動から iptables の設定が自動で読み込まれることになる。尚、iptables の NAT テーブルの削除は iptables -F -t nat として削除する。
テスト
正常系
ブラウザでアクセスすると以下のように表示される。

ip addr の確認
master 側は以下の通り。
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:77:49:dd brd ff:ff:ff:ff:ff:ff
inet xxx.xxx.xxx.82/18 brd 172.16.127.255 scope global eth0
inet xxx.xxx.xxx.100/24 scope global eth0
inet6 fe80::a00:27ff:fe77:49dd/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
slave 側は以下の通り。
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:78:50:de brd ff:ff:ff:ff:ff:ff
inet xxx.xxx.xxx.82/18 brd 172.16.127.255 scope global eth0
inet6 fe80::a00:28ff:fe67:50dd/64 scope link
valid_lft forever preferred_lft forever
ipvsadm は以下の通り。
IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 172.16.80.100:80 rr persistent 50 -> 172.16.80.79:80 Route 1 0 0
master が停止
master サーバーの keepalived を停止させて仮想 IP にアクセスする。

特に何も変わらずアクセス可能。その際、仮想 IP は slave サーバーで運用されている。
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:78:50:de brd ff:ff:ff:ff:ff:ff
inet xxx.xxx.xxx.82/18 brd 172.16.127.255 scope global eth0
inet xxx.xxx.xxx.100/24 scope global eth0
inet6 fe80::a00:28ff:fe67:50dd/64 scope link
valid_lft forever preferred_lft forever
inet xxx.xxx.xxx.100/24 scope global eth0 が仮想サーバーの IP アドレスエントリ。
負荷分散
- 実サーバーをもう一台構築してから試す
real server の一台が停止
- 実サーバーをもう一台構築してから試す
まとめ
- はよ real server をもう一台立てなければ
- 定期的に復習しないとなー