Linux下部署LVS(DR)+keepalived+Nginx负载均衡

架构部署

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.10
1、安装ipvsadm、keepalived(Master/Slave) 
yum -y install keepalived ipvsadm 
2、修改keepalived.conf文件 
LVS_master
cd /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端配置 
写一下lvs脚本 

#!/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 "$1" in

start)

# Start LVS-DR real server on 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: $0 {start|status|stop}”

exit 1

;;

esac

4、测试

Master

[root@text1 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

[root@omserver 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

上一篇:c#调用oracle存储过程返回数据集


下一篇:Tomcat服务器端口的配置