实验环境
192.168.80.62 lvs1
192.168.80.63 lvs2
192.168.80.64 web1
192.168.80.65 web2
在lvs1 安装 lvs+keepalived
下载lvs命令工具
wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
下载keepalived
wget http://www.keepalived.org/software/keepalived-1.2.19.tar.gz
解决依赖
yum install install kernel-devel kernel -y 安装内核
yum -y install gcc gcc-c++ pcre-devel zlib-devel openssl-devel -y
uname -r 查看内核banben
ln -s /usr/src/kernels/2.6.32-573.18.1.el6.x86_64 /usr/src/linux 内核软连接
安装lvs命令工具
tar -xf ipvsadm-1.24.tar.gz
cd ipvsadm-1.24
make && make install
安装keepalived
tar -xf keepalived-1.2.19.tar.gz
cd keepalived-1.2.19
./configure --with-kernel-dir=/usr/src/kernels/2.6.32-573.3.1.el6.x86
make && make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir -p /etc/keepalived
cp /usr/local/sbin/keepalived /usr/sbin/
编辑lvs1配置文件 配置文件默认不存在的
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
907765003@qq.com #报错发邮件地址
}
notification_email_from 907765003@qq.com #报错发邮件地址
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
# VIP1
vrrp_instance VI_1 {
state MASTER
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 51 #id
priority 100 #优先级
advert_int 5
# nopreempt #不抢占 可以不加
authentication {
auth_type PASS
auth_pass 1111 #验证密码
}
virtual_ipaddress {
192.168.80.99 #vip
}
}
virtual_server 192.168.80.99 80 { #vip 监听80端口
delay_loop 6
lb_algo wrr
lb_kind DR #dr 轮训模式
# persistence_timeout 60
protocol TCP
real_server 192.168.80.64 80 { #web1
weight 100 #权重
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.80.65 80 { #web2
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
lvs2 安装和lvs1 一样
编辑lvs2 配置文件
! Configuration Filefor keepalived
global_defs {
notification_email {
907765003@qq.com
}
notification_email_from 907765003@qq.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
# VIP1
vrrp_instance VI_1 {
state BACKUP
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 51
priority 90
advert_int 5
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.80.99
}
}
#REAL_SERVER_1
virtual_server 192.168.80.99 80 {
delay_loop 6
lb_algo wlc
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.80.64 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
#REAL_SERVER_2
real_server 192.168.80.65 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
/etc/init.d/keepalived start 启动服务
chkconfig keepalived on
[root@han2 han]# ip a 查看VIP
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:67:cd:68 brd ff:ff:ff:ff:ff:ff
inet 192.168.80.62/24 brd 192.168.80.255 scope global eth0
inet 192.168.80.99/32 scope global eth0
inet6 fe80::20c:29ff:fe67:cd68/64 scope link
valid_lft forever preferred_lft forever
查看日志
MASTER STATE
Apr 4 21:33:16 han2 Keepalived_vrrp[21134]: VRRP_Instance(VI_1) Received lower prio advert, forcing new election
Apr 4 21:33:21 han2 Keepalived_vrrp[21134]: VRRP_Instance(VI_1) Entering MASTER STATE
Apr 4 21:33:21 han2 Keepalived_vrrp[21134]: VRRP_Instance(VI_1) setting protocol VIPs.
Apr 4 21:33:21 han2 Keepalived_vrrp[21134]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.80.99
Apr 4 21:33:21 han2 Keepalived_healthcheckers[21133]: Netlink reflector reports IP 192.168.80.99 added
Apr 4 21:33:26 han2 Keepalived_vrrp[21134]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.80.99
然后要在realservser 执行脚本
vim realserver.sh
#!/bin/sh
#LVS Client Server
VIP=192.168.80.99
case $1 in
start)
ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/sbin/route add -host $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"
exit 0
;;
stop)
ifconfig lo:0 down
route del $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 OK"
exit 1
;;
*)
echo "Usage: $0 {start|stop}"
;;
esac
./reralserver start 执行脚本