首页 > 代码库 > lvs主主互备
lvs主主互备
项目名称:lvs双主负载均衡
项目描述:lvs双主负载web为CDN客户提供访问
项目技术:lvs双主负载Nginx
lvs+keepalived
192.168.110.130
vip1:192.168.110.140
lvs+keepalived
192.168.110.131
vip2:192.168.110.141
Nginx1
192.168.110.132
vip1:192.168.110.140
vip2:192.168.110.141
Nginx2
192.168.110.133
vip1:192.168.110.140
vip2:192.168.110.141
lvs上安装keepalived和ipvsadm
#!/bin/bash
#判断执行结果
judge() {
if [ $? -eq 0 ];then
echo "successfully...."
else
echo "filed...."
exit
fi
}
#http://www.linuxvirtualserver.org/software/kernel-2.6
#http://www.keepalived.org/software
echo "环境安装"
yum -y install gcc openssl openssl-devel popt popt-devel net-snmp net-snmp-devel libnl* &>> /dev/null
judge
echo "安装ipvsadm"
cd /usr/local/src
yum -y install ipvsadm &>> /dev/null
judge
echo "下载并安装keepalived"
wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz &>> /dev/null
tar -zxf keepalived-1.2.7.tar.gz
cd keepalived-1.2.7
echo "执行configure"
./configure --prefix=/usr/local/keepalived --enable-snmp --sysconfdir=/etc &>> configure-`date +%F`.log
judge
echo "执行make"
make &>> make-`date +%F`.log
judge
echo "执行make install"
make install &>> make_install-`date +%F`.log
judge
rm -rf configure-`date +%F`.log make-`date +%F`.log make_install-`date +%F`.log
cp /usr/local/keepalived/sbin/keepalived /sbin/
cp /usr/local/keepalived/bin/genhash /bin/
webserver上配置VIP
#!/bin/bash
#description:start realserver
#script_name:realserver_config
VIP1="192.168.110.140" #虚拟IP
VIP2="192.168.110.141" #虚拟IP
source /etc/init.d/functions
case "$1" in
start)
echo "start LVS of realserver."
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 $VIP1 broadcast $VIP1 netmask 255.255.255.255 up
/sbin/ifconfig lo:1 $VIP2 broadcast $VIP2 netmask 255.255.255.255 up
;;
stop)
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
/sbin/ifconfig lo:0 down
/sbin/ifconfig lo:1 down
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
lvs1配置keepalived
global_defs {
notification_email {
guotao@163.com
}
notification_email_from guotao@163.com
smtp_server guotao@163.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.110.140
}
}
virtual_server 192.168.110.140 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
# nat_mask 255.255.255.0
# persistence_timeout 50
protocol TCP
real_server 192.168.110.132 80 {#real server上80端口
weight 5
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.110.133 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.110.141
}
}
virtual_server 192.168.110.141 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
# nat_mask 255.255.255.0
# persistence_timeout 50
protocol TCP
real_server 192.168.110.132 80 {#real server上80端口
weight 5
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.110.133 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
lvs2配置keepalived
global_defs {
notification_email {
guotao@163.com
}
notification_email_from guotao@163.com
smtp_server guotao@163.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.110.140
}
}
virtual_server 192.168.110.140 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
# nat_mask 255.255.255.0
# persistence_timeout 50
protocol TCP
real_server 192.168.110.132 80 {#real server上80端口
weight 5
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.110.133 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.110.141
}
}
virtual_server 192.168.110.141 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
# nat_mask 255.255.255.0
# persistence_timeout 50
protocol TCP
real_server 192.168.110.132 80 {#real server上80端口
weight 5
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.110.133 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
lvs主主互备