CentOSに「Linux Virtual Server」と「Keepalived」をインストールする
1.依存パッケージのインストール
#yum -y install wget libnl* popt* gcc.x86_64 gcc-c++.x86_64 gcc-objc++.x86_64 kernel-devel.x86_64 make popt-static.x86_64
#yum -y install openssl-devel
2.BackupサーバとMasterサーバにLVSをインストール
#wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
#uname -r
#ln -s /usr/src/kernels/2.6.18-164.el5-i686/ /usr/src/linux
#tar zxvf ipvsadm-1.24.tar.gz
#cd ipvsadm-1.24
#make && make install
3.BackupサーバとMasterサーバにkeepalivedをインストール
#wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz
#tar zxvf keepalived-1.1.19.tar.gz
#cd keepalived-1.1.19
#./configure –prefix=/usr/local/keepalived
#make
#make install
#cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
#cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
#cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
#mkdir /etc/keepalived
4.keepalivedの設定
#vi /etc/sysconfig/keepalived
KEEPALIVED_OPTIONS="-D -f /usr/local/keepalived/etc/keepalived/keepalived.conf"
vi /etc/init.d/keepalived
#更新
. /etc/sysconfig/keepalived
PATH="$PATH:/usr/sbin"
export PATH
vi /usr/local/keepalived/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
fstartnews24@gmail.com
}
notification_email_from fstartnews24@gmail.com
smtp_server localhost
smtp_connect_timeout 30
router_id NodeA
}
vrrp_instance VI_1 {
state MASTER
interface ens160
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.17.3.143 //VIP
}
}
virtual_server 172.17.3.143 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 1
protocol TCP
real_server 172.17.3.144 8151 {
weight 1
TCP_CHECK {
connect_timeout 5
nb_get_retry 3
delay_before_retry 3
connect_port 8151
}
}
real_server 172.17.3.201 8151 {
weight 2
TCP_CHECK {
connect_timeout 5
nb_get_retry 3
delay_before_retry 3
connect_port 8151
}
}
}
VIP:172.17.3.143
WEB:172.17.3.144、172.17.3.201
5.keepalived 起動
/etc/init.d/keepalived start
6.クライアント
#!/bin/bash
# description: Config realserver lo and apply noarp
SNS_VIP=172.17.3.143
/etc/rc.d/init.d/functions
case “$1" in
start)
ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
/sbin/route add -host $SNS_VIP dev lo:0
echo “1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo “2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo “1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo “2" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p >/dev/null 2>&1
echo “RealServer Start OK"
;;
stop)
ifconfig lo:0 down
route del $SNS_VIP >/dev/null 2>&1
echo “0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo “0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo “0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo “0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo “RealServer Stoped"
;;
*)
echo “Usage: $0 {start|stop}"
exit 1
esac
exit 0
7.検証
ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP docker223:80 rr persistent 1
-> 172.17.3.144:8151 Route 1 0 0
ブラウザアクセス
http://172.17.3.143
http://172.17.3.144:8151