由于测试环境有限,在虚拟机中测试
一、环境:
CentOs 6.4 LVS Keepalived Nginx
主服务器(ServerA):192.168.1.251
从服务器(ServerB):192.168.1.252
VIP:192.168.1.254
二、步骤
1、主服务器ServerA安装LVS keepalived
[root@ServerA /]# yum -y install ipvsadm
[root@ServerA /]# yum -y install keepalived
[root@ServerA /]# cd /etc/init.d/
[root@ServerA init.d]# vi ipvs
#!/bin/bash
# description: Config realserver lo and apply noarp
VIP=192.168.1.254
. /etc/rc.d/init.d/functions
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"
;;
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"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
[root@ServerA init.d]#chmod a+x ipvs
[root@ServerA /]# cd /etc/keepalived/
[root@ServerA keepalived]# vi keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_Master
}
vrrp_instance VI_1 {
state MASTER
interface eth2
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.254
}
}
virtual_server 192.168.1.254 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 10
protocol TCP
real_server 192.168.1.251 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 30
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.1.252 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 30
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@ServerA /]#/etc/init.d/ipvs start
[root@ServerA /]#service keepalived start
[root@ServerA /]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 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
inet 192.168.1.254/32 brd 192.168.1.254 scope global lo:0
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:50:56:2d:7d:d7 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.251/24 brd 192.168.1.255 scope global eth2
inet 192.168.1.254/32 scope global eth2
inet6 fe80::250:56ff:fe2d:7dd7/64 scope link
valid_lft forever preferred_lft forever
[root@ServerA /]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.1.254:80 wrr persistent 10
-> 192.168.1.251:80 Local 1 0 0
-> 192.168.1.252:80 Route 1 0 0
2、从服务器ServerB安装LVS keepalived
[root@ServerB /]# yum -y install ipvsadm
[root@ServerB /]# yum -y install keepalived
[root@ServerB /]# cd /etc/init.d/
[root@ServerB init.d]# vi ipvs
#!/bin/bash
# description: Config realserver lo and apply noarp
VIP=192.168.1.254
. /etc/rc.d/init.d/functions
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"
;;
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"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
[root@ServerB init.d]#chmod a+x ipvs
[root@ServerB /]# cd /etc/keepalived/
[root@ServerB keepalived]# vi keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_Backup
}
vrrp_instance VI_1 {
state BACKUP
interface eth3
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.254
}
}
virtual_server 192.168.1.254 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 10
protocol TCP
real_server 192.168.1.251 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 30
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.1.252 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 30
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@ServerB /]#/etc/init.d/ipvs start
[root@ServerB /]#service keepalived start
[root@ServerB /]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 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
inet 192.168.1.254/32 brd 192.168.1.254 scope global lo:0
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:50:56:26:eb:9b brd ff:ff:ff:ff:ff:ff
inet 192.168.1.252/24 brd 192.168.1.255 scope global eth3
inet6 fe80::250:56ff:fe26:eb9b/64 scope link
valid_lft forever preferred_lft forever