Keepalived + LVS + CentOS4 でロードバランサー(NAT)

keepalived は、1.1.15からinclude が使えるようになったので、設定が簡単になったのでお勧め。ありがとうid:stanaka さん。

設定は以下のとおり。

種別 ホスト名 IPアドレス デフォルトゲートウェイ 備考
クライアント cl0 10.0.0.173 通常通り
ロードバランサー lb0 eth0:10.0.31.11/16 なし ロードバランサーのdefault g/w をすべて削除。
eth0:0 10.0.7.100(VIP) なし
eth1:192.168.31.11/16 なし
リアルサーバー web100 eth0:192.168.31.100/16 192.168.31.11 default g/w をロードバランサーの内側のIP(192.168.31.11)に変更。
リアルサーバー web101 eth0:192.168.31.101/16 192.168.31.11 default g/w をロードバランサーの内側のIP(192.168.31.11)に変更。
# yum -y install ipvsadm iproute curl
# rpm -ivh keepalived-1.1.15-5.i386.rpm

keepalived-1.1.15-5.i386.rpm は、ここからダウンロード。

/etc/sysconfig/network-scripts/ifcfg-eth0:0 というファイルを以下の内容で作成し、ネットワークを再起動。

DEVICE=eth0:0
BROADCAST=10.0.255.255
IPADDR=10.0.7.100
NETMASK=255.255.0.0
NETWORK=10.0.0.0
ONBOOT=yes
TYPE=Ethernet

/etc/keepalived/keepalived.conf を以下のように作成。(includeは、 keepalived-1.1.15 以降しか使えないので注意。)

! Configuration File for keepalived

global_defs {
   notification_email {
     yoshifumi1975@example.org
   }
   notification_email_from yoshifumi1975@example.org
   smtp_server 10.0.7.250
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

include web.conf
  • includeされる /etc/keepalived/web.conf の内容。
virtual_server_group HTTP100 {
  10.0.7.100  80
}

virtual_server group HTTP100 {
  delay_loop   3
  lvs_sched    rr
  lvs_method   NAT
  protocol     TCP
  virtualhost  example.org
  sorry_server 192.168.31.102 80

  real_server  192.168.31.100 80 {
    weight 1                      
    inhibit_on_failure            
    HTTP_GET {                    
      url {                       
        path /
        status_code 200           
      }                           
      connect_timeout 3           
    }                             
  }                               
  real_server  192.168.31.101 80 {
    weight 1                      
    inhibit_on_failure            
    HTTP_GET {                    
      url {                       
        path /         
        status_code 200           
      }                           
      connect_timeout 3           
    }                             
  }                               
}
# service keepalived restart
  • 設定の確認
# ipvsadm -Ln
IP Virtual Server version 1.2.0 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.0.7.100:80 rr
  -> 192.168.31.100:80            Masq    1      0          0
  -> 192.168.31.101:80            Masq    1      0          0

/etc/sysctl.confを以下のように編集。

(前略)
net.ipv4.ip_forward = 1
(中略)
net.ipv4.conf.default.rp_filter = 0
(後略)

設定をリロードする。(またはサーバー再起動しちゃえ。)

# sysctl -p
  • テスト

クライアントから以下を実行して動作を確認。

# curl 'http://10.0.7.100/'
<html>
<body>
192.168.31.101
</body>
</html>

# curl 'http://10.0.7.100/'
<html>
<body>
192.168.31.100
</body>
</html>

# curl 'http://10.0.7.100/'
<html>
<body>
192.168.31.101
</body>
</html>

# curl 'http://10.0.7.100/'
<html>
<body>
192.168.31.100
</body>
</html>

このページを参考にしました。
DSAS開発者の部屋:こんなに簡単! Linuxでロードバランサ (1)