LVS(Linux Vertual Server)に挑戦
CentOS5.2はkernelに既に組み込まれている
# grep IP_VS_VERSION_CODE /usr/src/kernels/`uname -r`-i686/include/net/ip_vs.h kernelソースを指定する部分は適宜修正する必要あり #define IP_VS_VERSION_CODE 0x010201
IPVSはLVSの中心的なソフト
ipvsadmはipvs管理ツール
CentOSのyumでもインストールできるがバージョンアップされているのでソースからインストール
# wget http://www.linux-vs.org/software/kernel-2.6/ipvsadm-1.24.tar.gz # tar zxvf ipvsadm-1.24.tar.gz # cd ipvsadm-1.24 # ln -s /usr/src/kernels/`uname -r`-`uname -m` /usr/src/linux ←おまじない(笑 # make # make installipvsadmのMakefileは/usr/src/linuxにkernelソースがあることが前提になっているのでおまじないが必要
ipvsadmだけで負荷分散は可能
# ip addr add 122.213.181.239 label eth0:239 dev eth0 # ip addr show eth0 # ipvsadm -C # ipvsadm -A -t <VIP>:<ポート> -s <バランスアルゴリズム> # ipvsadm -a -t <VIP>:<ポート> -r <リアルサーバーIPアドレス> <転送方法> ←必要台数分 # ipvsadm -Lnで確認
DSR時にはノードサーバーに以下の設定
iptables -t nat -A PREROUTING -d 仮想IPアドレス -j REDIRECTノード側では仮想IPアドレスを受け付けるようにする必要あり
これでサーバーの負荷分散が出来る…
アンインストールはない!
インストールされたファイルを削除する!
# ipvsadm --help
ipvsadm v1.24 2005/12/10 (compiled with popt and IPVS v1.2.1)
Usage:
ipvsadm -A|E -t|u|f service-address [-s scheduler] [-p [timeout]] [-M netmask]
ipvsadm -D -t|u|f service-address
ipvsadm -C
ipvsadm -R
ipvsadm -S [-n]
ipvsadm -a|e -t|u|f service-address -r server-address [options]
ipvsadm -d -t|u|f service-address -r server-address
ipvsadm -L|l [options]
ipvsadm -Z [-t|u|f service-address]
ipvsadm --set tcp tcpfin udp
ipvsadm --start-daemon state [--mcast-interface interface] [--syncid sid]
ipvsadm --stop-daemon state
ipvsadm -h
Commands:
Either long or short options are allowed.
--add-service -A add virtual service with options
--edit-service -E edit virtual service with options
--delete-service -D delete virtual service
--clear -C clear the whole table
--restore -R restore rules from stdin
--save -S save rules to stdout
--add-server -a add real server with options
--edit-server -e edit real server with options
--delete-server -d delete real server
--list -L|-l list the table
--zero -Z zero counters in a service or all services
--set tcp tcpfin udp set connection timeout values
--start-daemon start connection sync daemon
--stop-daemon stop connection sync daemon
--help -h display this help message
Options:
--tcp-service -t service-address service-address is host[:port]
--udp-service -u service-address service-address is host[:port]
--fwmark-service -f fwmark fwmark is an integer greater than zero
--scheduler -s scheduler one of rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq,
the default scheduler is wlc.
--persistent -p [timeout] persistent service
--netmask -M netmask persistent granularity mask
--real-server -r server-address server-address is host (and port)
--gatewaying -g gatewaying (direct routing) (default)
--ipip -i ipip encapsulation (tunneling)
--masquerading -m masquerading (NAT)
--weight -w weight capacity of real server
--u-threshold -x uthreshold upper threshold of connections
--l-threshold -y lthreshold lower threshold of connections
--mcast-interface interface multicast interface for connection sync
--syncid sid syncid for connection sync (default=255)
--connection -c output of current IPVS connections
--timeout output of timeout (tcp tcpfin udp)
--daemon output of daemon information
--stats output of statistics information
--rate output of rate information
--exact expand numbers (display exact values)
--thresholds output of thresholds information
--persistent-conn output of persistent connection info
--sort sorting output of service/server entries
--numeric -n numeric output of addresses and ports
shedulerとはipvs分散方法のこと
| 分類 | 意味 |
|---|---|
| rr(round robin) | 順番に分散 |
| wrr(weighted round robin) | 重み付けの割合に従って順番に分散 |
| lc(least connected) | 接続数が一番少ないリアルサーバーに接続させる |
| wlc(weighted least connection) | (デフォルト)重み付けの割合に従って接続数が一番少ないサーバーに接続させる |
| lblc(a persistent memory algorythm) | 透過型cache proxyにのみ適用可能 |
| sh(sorce hash) | ソースIPアドレスを元に分散先を決める、同じソースIPからの接続は同じリアルサーバーに接続される |
| dh(destination hash) | 宛先IPアドレスを元に分散先を決める |
| w(weight) | 重み付けの割合のみで分散 |
# 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 # make # make install /usr/local/etc/の下にkeepalived用の各種ファイルが生成される initスクリプトもある
インストールされたファイルを削除してくれます
これでテスト的に出来るかな
# make uninstall
| lvs_sync_daemon_interface インターフェース名 | アクティブなコネクションをフェールオーバー時に引き継ぐ |
| vrrp_sync_group | VRRPインターフェースをグループ化してインスタンスを同期してくれる |
vrrpセクションを同一にしておく必要がある
サンプルはこんな感じ
# cd /usr/local/etc/rc.d/init.d/
# cp -p keepalived vrrpd
# vi vrrpd
ここで編集(下記参照)
# cp -p vrrp ipvsd
# sed -i "s/vrrp/ipvs/g" ipvsd
# vi ipvsd
daemon行の--ipvsを--checkに修正
pidfile指定を/var/run/keepalived_checkers.pidに修正
# ln -s /usr/local/etc/rc.d/init.d/vrrpd /etc/rc.d/init.d/vrrpd
# ln -s /usr/local/etc/rc.d/init.d/ipvsd /etc/rc.d/init.d/ipvsd
# chkconfig --add vrrpd
# chkconfig --add ipvsd
# chkconfig vrrpd on
# chkconfig ipvsd on
(↓initスクリプトはこれ)
#!/bin/sh
#
# Startup script for the Keepalived daemon
#
# processname: keepalived
# pidfile: /var/run/keepalived_vrrp.pid
# config: /usr/local/etc/keepalived/vrrpd.conf
# chkconfig: - 21 79
# description: Start and stop Keepalived
# Source function library
. /etc/rc.d/init.d/functions
# Source configuration file (we set KEEPALIVED_OPTIONS there)
. /usr/local/etc/sysconfig/keepalived
RETVAL=0
prog="keepalived_vrrpd"
CONFIG="/usr/local/etc/keepalived/vrrpd.conf"
pidfile="/var/run/keepalived_vrrp.pid"
start() {
echo -n $"Starting $prog: "
daemon /usr/local/sbin/keepalived ${KEEPALIVED_OPTIONS} --vrrp -f ${CONFIG}
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
}
stop() {
echo -n $"Stopping $prog: "
##killproc keepalived
killproc -p ${pidfile}
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
}
reload() {
echo -n $"Reloading $prog: "
##killproc keepalived -1
killproc -p ${pidfile} -1
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
condrestart)
if [ -f /var/lock/subsys/$prog ]; then
stop
start
fi
;;
status)
status -p ${pidfile} ${prog}
;;
*)
echo "Usage: $0 {start|stop|reload|restart|condrestart|status}"
exit 1
esac
exit $RETVAL
でも、reload()はなんか使えない..
LVS用SNMPの話ここ
元のソースがあったページは既に存在しないそうだ…
# wget http://www.loadbalancer.org/download/SNMP/net-snmp-lvs-module-0.0.4.tar.gz # tar zxvf net-snmp-lvs-module-0.0.4.tar.gz # cd net-snmp-lvs-module-0.0.4 # cp /usr/local/src/ipvsadm-1.24/libipvs/* libipvs/ ipvsadmのライブラリが必要だそうだ。既にあるのは上書きしない! # make /usr/src/linuxにkernelソースが存在していること (ipvsadmのインストールでシンボリック作成済みのはず) # cp *-MIB.txt /usr/share/snmp/mibs/ # cp lvs.so /usr/lib/netsnmplvs.so # vi /etc/snmp/snmpd.conf dlmod lvs /usr/lib/libnetsnmplvs.so ←追加 # service snmpd restart
試してみる
# snmptranslate -m LVS-MIB -On -IR lvsServiceEntry # snmpwalk -c public -v 2c -m LVS-MIB localhost 1.3.6.1.4.1.8225.4711