Schema deployment
Lvs/keepalived (Master): 192.168.21.3 lvs/keepalived (Slave): 192.168.21.6 nginx1:192.168.21.4 nginx2:192.168.21.5 VIP : 192.168.21.101, installing Ipvsadm, keepalived (Master/slave)yum-y Install keepalived ipvsadm2. Modify the keepalived.conf fileLvs_masterCd/etc/keepalived vi/deepalived
! Configuration File for Keepalived
Global_defs {
router_id master_201
}
Vrrp_instance Vi_1 {
State MASTER
Interface eth0
VIRTUAL_ROUTER_ID 100
Priority 151
Advert_int 1
Authentication {
Auth_type PASS
Auth_pass 123456
}
virtual_ipaddress {
192.168.21.10
}
}
Virtual_server 192.168.21.10 80 {
Delay_loop 6
Lb_algo WRR
Lb_kind DR
# Persistence_timeout 50
Protocol TCP
Real_server 192.168.21.4 80 {
Weight 1
Tcp_check {
Connect_timeout 3
Nb_get_retry 3
Delay_before_retry 3
Connect_port 80
}
}
Real_server 192.168.21.5 80 {
Weight 1
Tcp_check {
Connect_timeout 3
Nb_get_retry 3
Delay_before_retry 3
Connect_port 80
}
}
}
Lvs_slave
! Configuration File for Keepalived
Global_defs {
router_id slave_211
}
Vrrp_instance Vi_1 {
State MASTER
Interface eth0
VIRTUAL_ROUTER_ID 100
Priority 150
Advert_int 1
Authentication {
Auth_type PASS
Auth_pass 123456
}
virtual_ipaddress {
192.168.21.10
}
}
Virtual_server 192.168.21.10 80 {
Delay_loop 6
Lb_algo WRR
Lb_kind DR
# Persistence_timeout 50
Protocol TCP
Real_server 192.168.21.4 80 {
Weight 1
Tcp_check {
Connect_timeout 3
Nb_get_retry 3
Delay_before_retry 3
Connect_port 80
}
}
Real_server 192.168.21.5 80 {
Weight 1
Tcp_check {
Connect_timeout 3
Nb_get_retry 3
Delay_before_retry 3
Connect_port 80
}
}
}
3, Nginx side Configuration write the LVs script
#!/bin/bash
#
# Script to start LVS DR real server.
# Description:lvs DR Real Server
#
. /etc/rc.d/init.d/functions
vip=192.168.21.10 #这里根据需要改成自己的VIP地址
Host= '/bin/hostname '
Case "$" in
Start
# Start LVS-DR Real Server on the this machine.
/sbin/ifconfig Lo Down
/sbin/ifconfig lo Up
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
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add-host $VIP Dev lo:0
;;
Stop
# Stop LVS-DR Real Server loopback device (s).
/sbin/ifconfig lo:0 Down
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
;;
Status
# Status of Lvs-dr Real server.
Islothere= '/sbin/ifconfig lo:0 | grep $VIP '
Isrothere= ' Netstat-rn | grep "lo:0" | grep $VIP '
if [! "$islothere"-O! "Isrothere"];then
# either the route or the lo:0 device
# not found.
echo "LVS-DR Real server Stopped."
Else
echo "LVS-DR Real server Running."
Fi
;;
*)
# Invalid entry.
echo "$0:usage: $ {start|status|stop}"
Exit 1
;;
Esac
4. Testing
Master
[Email protected] keepalived]# 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.21.10:80 WRR
-192.168.21.4:80 Route 1 0 0
-192.168.21.5:80 Route 1 0 0
Slave
[Email protected] keepalived]# 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.21.10:80 WRR
-192.168.21.4:80 Route 1 0 0
-192.168.21.5:80 Route 1 0 0
Deploy LVs (DR) +keepalived+nginx load balancer under Linux