負荷分散について考えてみる②: keepalivedの設定
keepalivedのインストールが無事に完了しましたら今度は。設定を行ってみましょう。
- keepalived.confの編集 特に説明する部分もないのですが、こんな感じで書きました。以下のconfigでのポイントは、 lb_kindをDR(DSR)にしている部分になります。他の設定は、NATになりますが、上記図の通り、ネットワークセグメントが同一のため、NATですと(僕の知識レベルでは)IPアドレスを変換できない形なってしまいます。ちょっと理由になっていないかも。DR(DSR)の設定では、NATのように、IPアドレスを変換しないで、ダイレクトに、リアルサーバ(192.168.0.61,192.168.0.62)がリスエスト元にレスポンスを返すため、KeepAlivedのサーバに負荷をかけません。
[root@iruka ~]# vi /etc/keepalived/keepalived.conf
vrrp_instance V_W {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
nopreemp
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.200
}
}
virtual_server group HTTP100 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 0
protocol TCP
sorry_server 192.168.0.200 80
real_server 192.168.0.61 80 {
weight 1
inhibit_on_failure
HTTP_GET {
url {
path /test.html
status_code 200
}
connect_port 80
connect_timeout 5
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.0.62 80 {
weight 1
inhibit_on_failure
HTTP_GET {
url {
path /test.html
status_code 200
}
connect_port 80
connect_timeout 5
nb_get_retry 3
delay_before_retry 3
}
}
- keepalivedの起動(iruka) 一応、現在のipvsadmのバージョンを確認する
[root@iruka ~]# ipvsadm -v ipvsadm v1.24 2003/06/07 (compiled with getopt_long and IPVSv1.2.0)
念のため、設定をクリアしておく
[root@iruka ~]# ipvsadm -C
ちゃんとクリアされているか、確認する(なんもでていないからクリアされていますね)
[root@iruka ~]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn keepalivedサービスを再起動する [root@iruka ~]# service keepalived restart keepalived を停止中: [ OK ] keepalived を起動中: [ OK ]
もいいちど確認
[root@iruka ~]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.0.200:80 rr -> 192.168.0.62:80 Route 1 0 0 -> 192.168.0.61:80 Route 1 0 0
念のためVIPが正常に認識されている確認する。
[root@iruka ~]# ping 192.168.0.200 PING 192.168.0.200 (192.168.0.200) 56(84) bytes of data. 64 bytes from 192.168.0.200: icmp_seq=1 ttl=64 time=0.075 ms 64 bytes from 192.168.0.200: icmp_seq=2 ttl=64 time=0.036 ms 64 bytes from 192.168.0.200: icmp_seq=3 ttl=64 time=0.037 ms
- 各リアルサーバの設定(shiune,tenten両方で実施します) keepalived.confにて、"DR"を選択した場合、ほんとうにちょびっとですがリアルサーバの設定を変更しないといけません。 いじる部分は、iptableのipパケットの設定の部分です念のため、iptablesをクリアしておく(色々いじっちゃかったからが理由であって、他の方たちは大丈夫かと思います)
[root@shizune ~]# iptables -t nat -F [root@shizune ~]# /sbin/service iptables save ファイアウォールのルールを /etc/sysconfig/iptables に保存中[ OK ]
本当にクリアされたか確認する
[root@shizune ~]# view /etc/sysconfig/iptables # Generated by iptables-save v1.3.5 on Wed Jun 16 01:36:25 2010 *filter :INPUT ACCEPT [65767:5136735] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [45047:11141968] COMMIT # Completed on Wed Jun 16 01:36:25 2010 # Generated by iptables-save v1.3.5 on Wed Jun 16 01:36:25 2010 *nat :P REROUTING ACCEPT [13793:1327753] :P OSTROUTING ACCEPT [1743:109910] :OUTPUT ACCEPT [1743:109910] COMMIT # Completed on Wed Jun 16 01:36:25 2010
以下のコマンドを実行 iptablesの-j REDIRECTは、localhost宛てとして取り込む。VIP(192.168.0.200)宛のパケットを自分宛のように扱うことができます。
[root@shizune ~]# iptables -t nat -A PREROUTING -p tcp -d 192.168.0.200 -j REDIRECT [root@shizune ~]# /sbin/service iptables save ファイアウォールのルールを /etc/sysconfig/iptables に保存中[ OK ] iptablesの内容を確認(今度は、設定が反映されているか確認する) [root@shizune ~]# view /etc/sysconfig/iptables # Generated by iptables-save v1.3.5 on Wed Jun 16 01:53:53 2010 *filter :INPUT ACCEPT [66740:5219738] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [45687:11229723] COMMIT # Completed on Wed Jun 16 01:53:53 2010 # Generated by iptables-save v1.3.5 on Wed Jun 16 01:53:53 2010 *nat :P REROUTING ACCEPT [13981:1348768] :P OSTROUTING ACCEPT [1755:110678] :OUTPUT ACCEPT [1755:110678] -A PREROUTING -d 192.168.0.200 -p tcp -j REDIRECT COMMIT # Completed on Wed Jun 16 01:53:53 2010
- 接続確認(別のクライアントから検証) [shell]
[root@neji ~]# curl 'http://192.168.0.200/test.html' <html> <body> tenten webserv </body> </html> [root@neji ~]# curl 'http://192.168.0.200/test.html' <html> <body>
shizune webserv
</body> </html> [/shell]