王志广的个人博客

        王志广的个人博客 >> IT >> Linux高可用负载均衡

Linux高可用负载均衡

admin发表于2015-08-27  1,219次浏览 标签: 

本文主要介绍TCP七层结构中第4层(传输层)和第7层(应用层)的负载均衡,以及如何实现检测web服务器的状态

1.第4层负载均衡如何实现
第四层所使用到的工具有lvs+keepalive
LVS是Linux虚拟服务器,也是一个负载均衡软件
三种网络/工作模式:NAT、DR、TUN
十个调度算法: rr、wrr、lc、wlc、lblc、lblcr、dh、sh、sed、 nq
规划
虚拟IP 192.168.1.100
web服务器192.168.1.1
web服务器192.168.1.2
web服务器192.168.1.3
LVS配置主服务器192.168.2.1
LVS配置从服务器192.168.2.2
wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm包

在web服务器上配置,需要执行脚本
#!/bin/bash
#description Config LVS to realserver lo and apply noarp
#Written by ce
VIP=192.168.1.100
. /etc/init.d/functions
case “$1” in
start)
ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/sbin/route add -host $SNS_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

LVS服务器上配置,同时启用脚本
#!/bin/bash
# description: start LVS of DirectorServer
#Written by : ce
VIP=192.168.1.100
RIP1=192.168.1.1
RIP2=192.168.1.2
RIP3=192.168.1.3
. /etc/rc.d/init.d/functions
logger $0 called with $1
case “$1” in
start)
# set squid vip
/sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev eth0:0
echo “LVSServer Start OK”
;;
stop)
ifconfig eth0:0 down
route del $VIP
echo “LVSServer stoped”
;;
*)
echo “Usage: $0 {start|stop}”
exit 1
esac
在LVS服务器上安装LVS和keepalive软件
配置keepalive.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
# VIP1
vrrp_instance VI_1 {
state MASTER #在LVS从服务器上将MASTER改为BACKUP
interface eth0
virtual_router_id 51
priority 100 #在LVS从服务上将100改为99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100#虚拟ip可以写多个
}
}
virtual_server 192.168.0.100 80 {
delay_loop 6 #(每隔10秒查询realserver状态)
lb_algo wrr #(lvs 算法)
lb_kind DR #(Direct Route)
persistence_timeout 60 #(同一IP的连接60秒内被分配到同一台realserver)
protocol TCP #(用TCP协议检查realserver状态)
real_server 192.168.1.1 80 { #配置WEB服务器
weight 3 #(权重)
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.2 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.3 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}

查看LVS运行是否正常
/etc/ipvsadm 查看
浏览器访问192.168.1.100
然后关闭LVS主服务器看看LVS从服务器是否正常

2.第7层负载均衡如何实现
第七层所使用的工具有nginx+keepalive
yum -y install gcc pcre-devel zlib-devel openssl-devel
yum -y install popt-devel
wget http://nginx.org/download/nginx包
wget http://www.keepalived.org/software/keepalived包
tar zxvf keepalived包
cd keepalived
./configure
make
make install
nginx步骤和keepalive差不多
启动服务 在192.168.2.1和192.168.2.2中
/usr/local/nginx/sbin/nginx
/etc/init.d/keepalived start
配置Nignx的nginx.conf
添加 upstream
upstream abc {
ip_hash;
server 192.168.1.2:80;
server 192.168.1.3:80;
server 192.168.1.4:80;
}
添加反向代理
location / {
proxy_pass http://abc;
}

配置keepalived的keepalived.conf
在主服务器 192.168.2.1
!Configuration File for keepalived
global_defs {
router_id NGINX_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100
}}

从服务器 192.168.2.2
! Configuration File for keepalived
global_defs {
router_id NGINX_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100
}}
然后服务器上启动keepalived
可以直接浏览器访问192.168.1.100
查看ip addr中是否有 192.168.1.100这个虚拟IP
然后停掉主服务器上keepadlived,看看从服务器能够接管虚拟IP

让keepalived监控NginX的状态如果192.168.2.1的nginx当掉了就关闭192.168.2.1的keepalived
使用脚本检测nginx状态,如果第一次失败等3秒,如果还是失败就关闭keepalived,实现把nginx负载均衡责任交给备用服务器
#!/bin/bash
# check nginx server status
NGINX=/usr/local/nginx/sbin/nginx
PORT=80
nmap localhost -p $PORT | grep “$PORT/tcp open”
#echo $?
if [ $? -ne 0 ];then
$NGINX -s stop
$NGINX
sleep 3
nmap localhost -p $PORT | grep “$PORT/tcp open”
[ $? -ne 0 ] && /etc/init.d/keepalived stop
fi
可以把脚本写到keepalive中
比如在主服务器的keepalive.conf中添加
vrrp_script chk_http_port {
script “/opt/chk_nginx.sh”\\这里就是上面脚本的地址
interval 1
weight -2
}
track_script {
chk_http_port
}

然后就实现了高可用的负载均衡,当192.168.2.1服务器当掉,能够快速让192.168.2.2替换192.168.2.1的任务

你可以发表评论引用到你的网站或博客,或通过RSS 2.0订阅这个日志的所有评论。
上一篇:
下一篇:
已有1条评论
汤圆
2015 年 9 月 15 日

支持!

我来说两句

  Ctrl+Enter