2010年12月6日月曜日

LVS+KeepAlive+VRRP+DSR (Load balancer)

I installed LVS. I’ve written the installation document below. Please give me advice/suggestions on it. I would appreciate any kind of advice/suggestion!

== Basic Setting of LVS
[client] 192.168.10.24
           |
           |
           |   192.168.10.11  (eth1)
         [LVS] 192.168.10.100 (仮想IP)
           |   192.168.20.108 (eth0)
           |
           |
           |
-----------------------------------
    |                       |
    |                       |
    |192.168.20.110(eth0)   | 192.168.20.111(eth1)
  [web1]                   [web2]
yum -y install ipvsadm
ipvsadm -v

ipvsadm -v
ipvsadm v1.24 2003/06/07 (compiled with popt and IPVS v1.2.0)

LV1に仮想IPを加える。
ip addr add 192.168.10.100 label eth1:100 dev eth1

いったん設定をリセット
ipvsadm -C

VIPを追加する。 lcオプションはリアルサーバーにバランスする。
ipvsadm -A -t 192.168.10.100:80 -s lc

削除したい時は以下
ipvsadm -D -t 192.168.10.100:80

NATモードで不可分散サーバーを設定
ipvsadm -a -t 192.168.10.100:80 -r 192.168.20.110 -m
ipvsadm -a -t 192.168.10.100:80 -r 192.168.20.111 -m

削除したい時は以下
ipvsadm -d -t 192.168.10.100:80 -r 192.168.20.110

-g --gatewaying
Direct Server Return (DSR)
-i --ipip
IPIPのカプセリング
-m --masquerading
NAT

確認
ipvsadm -Ln

パケット転送を有効にする
echo '1' > /proc/sys/net/ipv4/ip_forward

再起動後も有効になるように設定
vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
sysctl -p

clientサーバーの GatewayをLVSの内側192.168.20.108に設定

クライアント192.168.10.24から以下curlでアクセスして負荷分散の確認
curl http://192.168.10.100

サービスの削除は以下のコマンド
ipvsadm -d -t 192.168.10.100:80 -r 192.168.20.110

ウエイトを0にするには以下のコマンド
ipvsadm -e -t 192.168.100:80 -r 192.168.20.111 -m -w 0

== Keepalived
[client] 192.168.10.24
           |
           |
           |   192.168.10.11  (eth1)         |
         [LVS] 192.168.10.100 (仮想IP)
           |   192.168.20.108 (eth0)
           |
           |
           |
----------------------------------------------------------
    |                       |                        |
    |                       |                        |
    |192.168.20.110(eth0)   | 192.168.20.111(eth1)   |192.168.20.113
  [web1]                   [web2]                  [sorry]
インストール  keepalived
wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz

tar zxvf keepalived-1.1.15.tar.gz
cd keepalived-1.1.15
./configure

以下NOになっている。
Keepalived configuration
------------------------
Keepalived version : 1.1.15
Compiler : gcc
Compiler flags : -g -O2
Extra Lib : -lpopt -lssl -lcrypto
Use IPVS Framework : No
IPVS sync daemon support : No
Use VRRP Framework : Yes
Use LinkWatch : No
Use Debug flags : No

以下インストールしてConfigure
yum install kernel-devel

wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.18.8.tar.gz
tar zxvf linux-2.6.18.8.tar.gz
cp net/core/* /usr/src/kernels/2.6.18-8.el5-i686/net/core/

再度configure
./configure --with-kernel-dir=/usr/src/kernels/2.6.18-8.el5-i686

Keepalived configuration
------------------------
Keepalived version : 1.1.15
Compiler : gcc
Compiler flags : -g -O2
Extra Lib : -lpopt -lssl -lcrypto
Use IPVS Framework : Yes
IPVS sync daemon support : Yes
Use VRRP Framework : Yes
Use LinkWatch : Yes
Use Debug flags : No

make
make install

cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/.
ln -s /usr/local/sbin/keepalived /usr/sbin/.
mkdir /etc/keepalived/
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/.

[root@localhost sysconfig]# cat /etc/keepalived/keepalived.conf
global_defs {
notification_email {
sakai@aplogics.com
}
notification_email_from sakai@aplogics.com
smtp_server localhost
smtp_connect_timeout 30
lvs_id LVS_DEVEL
}

vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.100
}
}

virtual_server 192.168.10.100 80 {
delay_loop 3
lb_algo lc
lb_kind NAT
nat_mask 255.255.255.0
protocol TCP

virtualhost test.com
sorry_server 192.168.20.113 80

real_server 192.168.20.110 80 {
weight 1
inhibit_on_failure
HTTP_GET {
url {
path /index.html
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}

real_server 192.168.20.111 80 {
weight 1
inhibit_on_failure
HTTP_GET {
url {
path /index.html
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}

cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/.
cat /etc/sysconfig/keepalived
KEEPALIVED_OPTIONS="-C"

ipvsadm -Cで IPVSをクリア
/etc/rc.d/init.d/keepalived start

== VRRP ==
[client] 192.168.10.24
                               |
-------------------------------------------------------------------
           |  192.168.10.100 (VIP for web1,2 and VRRP) |
           |  192.168.10.101 (VIP for web3 and VRRP)   |
           |                                           |
           |192.168.10.108  (eth1)                     |192.168.10.109(eth1)
     [LVS1]                                     [LVS2]
       |192.168.20.108 (eth0)                      |192.168.20.109(eth0)
           |                                           |
           |        192.168.20.200 (VIP for VRRP)      |
           |                                           |
           |                                           |
----------------------------------------------------------------
    |                       |                        |
    |                       |                        |
    |192.168.20.110(eth0)   | 192.168.20.111(eth1)   |192.168.20.112
  [web1]                   [web2]                  [web3]
clientのGWを以下設定
route add -host 192.168.10.100 gw 192.168.10.10

[root@localhost ~]# cat /etc/sysconfig/keepalived
KEEPALIVED_OPTIONS="-D -C -P"

- Master側 LVS

[root@server1 keepalived]# cat /etc/keepalived/keepalived.conf
global_defs {
notification_email {
sakai@aplogics.com
}
notification_email_from sakai@aplogics.com
smtp_server localhost
smtp_connect_timeout 30
lvs_id LVS_DEVEL
}

vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 101                 <---- Priorityを をあげる
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.100/24 dev eth1 label eth1:100
192.168.10.101/24 dev eth1 label eth1:101
192.168.20.200/24 dev eth0
}
}

virtual_server 192.168.10.100 80 {
delay_loop 3
lb_algo lc
lb_kind NAT
nat_mask 255.255.255.0
protocol TCP

virtualhost test.com
#sorry_server 192.168.20.109 80

real_server 192.168.20.110 80 {
weight 1
inhibit_on_failure
HTTP_GET {
url {
path /index.html
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}

real_server 192.168.20.111 80 {
weight 1
inhibit_on_failure
HTTP_GET {
url {
path /index.html
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}

virtual_server 192.168.10.101 80 {
delay_loop 3
lb_algo lc
lb_kind NAT
nat_mask 255.255.255.0
protocol TCP

virtualhost test.com
#sorry_server 192.168.20.109 80

real_server 192.168.20.112 80 {
weight 1
inhibit_on_failure
HTTP_GET {
url {
path /index.html
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}

--BACKUP側 LVS

[root@server2 keepalived]# cat /etc/keepalived/keepalived.conf
global_defs {
notification_email {
sakai@aplogics.com
}
notification_email_from sakai@aplogics.com
smtp_server localhost
smtp_connect_timeout 30
lvs_id LVS_DEVEL
}

vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100 <---- Priorityを を下げる
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.100/24 dev eth1 label eth1:100
192.168.10.101/24 dev eth1 label eth1:101
192.168.20.200/24 dev eth0
}
}

virtual_server 192.168.10.100 80 {
delay_loop 3
lb_algo lc
lb_kind NAT
nat_mask 255.255.255.0
protocol TCP

virtualhost test.com
#sorry_server 192.168.20.109 80

real_server 192.168.20.110 80 {
weight 1
inhibit_on_failure
HTTP_GET {
url {
path /index.html
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}

real_server 192.168.20.111 80 {
weight 1
inhibit_on_failure
HTTP_GET {
url {
path /index.html
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}

virtual_server 192.168.10.101 80 {
delay_loop 3
lb_algo lc
lb_kind NAT
nat_mask 255.255.255.0
protocol TCP

virtualhost test.com
#sorry_server 192.168.20.109 80

real_server 192.168.20.112 80 {
weight 1
inhibit_on_failure
HTTP_GET {
url {
path /index.html
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}

/etc/rc.d/init.d/keepalived start

以下でIPを確認
ip addr show eth1

内側のサーバーからNATで外にアクセスしたい場合はMasqueradeの設定を以下のように/etc/rc.d/init.d/iptablesに設定する。

*nat
:PREROUTING ACCEPT [60:3408]
:POSTROUTING ACCEPT [0:0]
:OUTPUT ACCEPT [3:243]
-A POSTROUTING -o eth0 -j MASQUERADE
COMMIT

*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [301:35162]
-A INPUT -i lo -j ACCEPT
-A INPUT -p icmp -m icmp --icmp-type any -j ACCEPT
-A INPUT -p esp -j ACCEPT
-A INPUT -p ah -j ACCEPT
-A INPUT -d 224.0.0.251 -p udp -m udp --dport 5353 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p udp -m udp --dport 631 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 631 -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT

以下設定変更時のセッション状況

以下でweightを0にしてもセッションの継続は可、次回からのセッションが接続しなくなる
ipvsadm -e -t 10.1.1.10:80 -r 192.168.2.236:80 -m -w 0

設定をかえたら以下で設定変更。
/etc/rc.d/init.d/keepalived reload
セッションがつながっている以外のサーバーの追加や削除をしてもセッションの継続は可

ただし以下のようにVIPやサービスを削除するとセッションが切れる。
ipvsadm -D -t 10.1.1.10:80 -s lc
ipvsadm -d -t 10.1.1.10:80 -r 192.168.2.236:80

== DSR
[client] 192.168.10.24
                               |
 ---------------------------------------------------------------------------------
           |       192.168.10.200 (VIP for VRRP)       |                         |
           |                                           |                         |
           |                                           |                         |
           |192.168.10.108  (eth1)                     |192.168.10.109(eth1)     |
         [LVS1]                                     [LVS2]                       |
       |192.168.20.108 (eth0)                      |192.168.20.109(eth0)     |
           |                                           |                         |
           |        192.168.20.200 (VIP for VRRP)      |                         |
           |                                           |                         |
           |                                           |                         |
 ----------------------------------------------------------------                |
    |                       |                                                    |
    |                       |                                                    |
    |192.168.20.110(eth0)   |192.168.20.111(eth0)                                |
  [web1]                   [web2]                                                |
  |192.168.10.110(eth1)   |192.168.10.111(eth1)                                |
    |                       |                                                    |
    |                       |                                                    |
 ---------------------------------------------------------------------------------
クライアントGWを以下に変更(同セグメントのブロードキャストにLVSがARPを返さないため)
route add -host 192.168.10.100 gw 192.168.10.200

- LVS

cat /etc/keepalived/keepalived.conf
virtual_ipaddress {
#192.168.10.100/24 dev eth1 label eth1:100
#192.168.10.101/24 dev eth1 label eth1:101
192.168.10.200/24 dev eth1
192.168.20.200/24 dev eth0
}

lb_algo rr
lb_kind DR

LVSに届いた192.168.10.100宛てのパケットに対してmark値を1にセットするようにnetfilterを設定。
iptables -t mangle -A PREROUTING -d 192.168.10.100 -j MARK --set-mark 1
iptables -t mangle -n -Lで確認可能
以下で削除
iptables -t mangle -D PREROUTING -d 192.168.10.100 -j MARK --set-mark 1

mark値が1のパケットは100番の経路テーブルを適用させるようにします。
ip rule add prio 100 fwmark 1 table 100
ip rule で確認
以下で削除可能
ip rule del prio 100 fwmark 1 table 100
再起動後には消える。

全てのパケットをローカルデバイスにルーティングするような設定を 100番の経路テーブルに追加します。
ip route add local 0/0 dev lo table 100
ip route list table 100 で確認
以下で削除可能
ip route del local 0/0 dev lo table 100
再起動後には消える。

rp_filterを無効にする。これが有効のままだと、カーネルのソースアドレス認証(Source Address Verification)
という機能が働いてしまうため、リアルサーバからの応答パケットをフィルタする。

echo 0 > /proc/sys/net/ipv4/conf/eth0/rp_filter

vi /etc/sysctl.conf
net.ipv4.conf.default.rp_filter = 0
sysctl -p

/etc/rc.d/init.d/keepalived start

- WEB Server

送信先アドレスが自分自身のIPアドレスに変換されてローカルデバイスに渡される
iptables -t nat -A PREROUTING -d 192.168.10.100 -j REDIRECT
iptables -L -t nat で確認
以下で削除可能

iptables -t nat -D PREROUTING -d 192.168.10.100 -j REDIRECT

GWをクライアントに向ける。
cat /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=node1
GATEWAY=192.168.10.24

tcpdump -n -i eth1 port 80 and host 192.168.10.24

以下で上記のiptableの設定を保存
/etc/rc.d/init.d/iptables save

==もしサーバーのルーター越しのロードバランスではなく、VIPと同じインタフェース側のサブネットをしようする場合

echo "1" >/proc/sys/net/ipv4/ip_forward

ipvsadm -C
ipvsadm -A -t 216.69.70.80:21 -s wlc
ipvsadm -a -t 216.69.70.80:21 -r 216.69.70.81 -m
ipvsadm -a -t 216.69.70.80:21 -r 216.69.70.82 -m
ipvsadm -Ln

ロードバランスされる側のGWをロードバランサのVIPに向ける

もしくは上記で説明したDSRを用いればOK.



ラベル:


コメント:

コメントを投稿

登録 コメントの投稿 [Atom]





<< ホーム

This page is powered by Blogger. Isn't yours?

登録 投稿 [Atom]