未加星标

高可用集群技术之Keepalived实现LVS高可用并负载均衡Web服务

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二04 | 时间 20160901 | 作者 红领巾 ] 0人收藏点击收藏
Keepalived概述:
Keepalived的作用是检测服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作,当服务器工作正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器.
keepalived实现lvs高可用并负载均衡web服务:
准备工作:
2台keepalived节点为: node1.samlee.com node2.samlee.com
2台realserver服务为: node3.samlee.com node4.samlee.com
1、配置RSserver过程如下(node3.samlee.com\node4.samlee.com):
(1)部署web服务(使用nginx配置)
# yum -y install nginx-1.10.0-1.el6.ngx.x86_64.rpm
(2)禁用防火墙及主机名修改
# service iptables stop
# chkconfig iptables off
(3)配置路由转发及转发模块配置
# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
# echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
# echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore

--添加路由条目
# ifconfig lo:0 172.16.100.99 netmask 255.255.255.255 broadcast 172.16.100.99 up
# route add -host 172.16.100.99 dev lo:0
2、配置keepalived服务器
(1)安装ipvsadm负载均衡管理器

# yum -y install ipvsadm

(2)安装keepalived
# yum -y install keepalived
# chkconfig keepalived on
(3)配置keepalived默认初始状态如下:(主节点:master状态)
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL #物理路由id,一般指定为本机的hostname
}
vrrp_instance VI_1 { #vrrp实例配置段
state MASTER #指定keepalived服务运行状态
interface eth0 #指定keepalived的心跳口
virtual_router_id 51 #指定虚拟路由id(1~255),同一vrrp实例的主备keepalived必须配置为一样
priority 101 ##指定起始优先级,优先级高的会成为master
advert_int 1 #vrrp通告的发送间隔
authentication { #配置通过密码认证
auth_type PASS
auth_pass keepalivedpass
}
virtual_ipaddress {
172.16.100.99 dev eth0 label eth0:0 #定义VIP
}
}
(4)配置keepalived默认初始状态如下:(备节点:backuo状态)
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}

vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass keepalivedpass
}
virtual_ipaddress {
172.16.100.99 dev eth0 label eth0:0
}
}
(5)调用周期性检测down文件是否存在来控制keepalived服务的主备切换
在keepalived.conf添加如下内容:
全局配置的段下添加如下:
vrrp_script chk_downfile { #服务状态检测脚本配置段
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0" #通过周期性检测down文件是否存在来控制keepalived服务的主备切换
interval 1 #指定检测间隔为1秒
weight -2 #指定检测失败时,优先级减2;检测的成功或失败是由script后面指定的命令或脚本执行返回的状态码决定的,0表示成功,非0表示失败
}

在vrrp_instance VI_1 ##vrrp实例配置段添加如下:
track_script {
chk_downfile #通过指定上面定义监控脚本来监控服务状态,以完成主备切换
}
重启keepalived服务创建down文件测试:
# service keepalived restart
# touch /etc/keepalived/down

---在/etc/keepalived/下建立down文件可实现主备模式切换
(6)调用外部脚本检测web(nginx)健康状态
建立/etc/keepalived/chkngx.sh脚本
#/bin/bash
#/etc/keepalived/chkngx.sh
#
if pkill -0 nginx>/dev/null; then
[ `curl -sL -w %{http_code} http://localhost -o /dev/null` -eq 200 ] && exit 0
else
service nginx start
fi
exit $?
授予脚本执行权限:
# chmod +x /etc/keepalived/chkngx.sh
配置keepalived.conf调用外部脚本
全局配置的段下添加如下:
vrrp_script chkngx {
script "/etc/keepalived/chkngx.sh" #指定用于检测nginx服务的执行脚本路径
interval 1 #监测间隔
weight -2 #失败时,优先级减2
fall 3 # 指定nginx检测脚本连续执行失败次数为3,才进行Failover
rise 3 # 指定nginx检测脚本连续执行成功次数为3,才进行Failback
}

在vrrp_instance VI_1 ##vrrp实例配置段添加如下:
track_script {
chkngx #通过指定上面定义监控脚本来监控服务状态,监控nginx监控状态
}
(7)使用通知脚本实现如何在vrrp事务发生时,发送警告邮件给指定的管理员? 比如:主从节点故障时
建立脚本监控脚本如下:
# vim /etc/keepalived/notify.sh
#!/bin/bash
#

declare -a vips=(172.16.100.98 172.16.100.99)
[email protected]'
Usage() {
echo "Usage:`basename $0`{master|backup|fault}"
}

Notify() {
if [ "$2" = "VI_1" ]; then
vip=${vips[0]}
echo $vip
elif [ "$2" = "VI_2" ]; then
vip=${vips[1]}
echo $vip
fi
suject="`hostname`'s state chaged to $1"
mailbody="`date +%F--%H:%M:%S`: `hostname`'s state chage to $1,vip floating..."
echo $mailbody | mail -s "$subject" $contact
echo $mailbody >> /tmp/keepalived.log
}

case $1 in
master)
Notify master
exit 0
;;
backup)
Notify backup
exit 0
;;
fault)
Notify fault
exit 0
;;
*)
Usage
exit 1
;;
esac
授予脚本执行权限:
# chmod +x /etc/keepalived/notify.sh
配置keepalived.conf调用外部脚本
在vrrp_instance VI_1 ##vrrp实例配置段最后添加如下:
notify_master "/etc/keepalived/notify.sh master" #状态变为master时,触发的通知脚本
notify_backup "/etc/keepalived/notify.sh backup" #状态变为backup时,触发的通知脚本
notify_fault "/etc/keepalived/notify.sh fault" #状态变为fault时,触发的通知脚本
notify "/etc/keepalived/notify.sh" #当发生所有的状态改变时,会先触发对应的状态通知脚本后,再触发该脚本
(8)使用脚本根据down文件检测实现主从状态转换
# vim /etc/keepalived/convert_state.sh
#!/bin/bash
#

usage() {
echo "./`basename $0` {master|backup}"
exit 1
}

if [ $# -eq 0 ]; then
usage
fi

if [[ "$1" = "master" ]]; then
[ -f /etc/keepalived/down ] && rm -f /etc/keepalived/down
elif [[ "$1" = "backup" ]]; then
[ ! -f /etc/keepalived/down ] && touch /etc/keepalived/down
else
usage
fi
sleep 1

# chmod +x /etc/keepalived/convert_state.sh
执行脚本convert_state进行备节点转换:
# /etc/keepalived/convert_state.sh backup
# ls /etc/keepalived/ | grep down
down
执行脚本convert_state进行主节点转换:
# /etc/keepalived/convert_state.sh master
(9)LVS+keepalived配置ipvs规则实现负载均衡高可用
配置如下:
virtual_server 172.16.100.99 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.0.0
# persistence_timeout 50
protocol TCP
sorry_server 127.0.0.1 80 #如果所有节点都down机,将转向本地web服务

real_server 172.16.100.8 80 {
weight 1
HTTP_GET { #web健康状态检测
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 172.16.100.9 80 {
weight 1
HTTP_GET { #web健康状态检测
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
最后查询ipvs规则,如下所示:
# ipvsadm -L -n --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes
-> RemoteAddress:Port
TCP 172.16.100.99:80 3 22 0 4657 0
-> 172.16.100.8:80 0 0 0 0 0
-> 172.16.100.9:80 0 0 0 0 0

以上为keepalived+lvs+web服务实现所有内容。

keepalived双主模型配置案例如下:
master1为:
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass keepalivedpass
}
virtual_ipaddress {
172.16.100.99 dev eth0 label eth0:0
}
track_script {
chk_downfile
}
track_script {
chkngx
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
notify "/etc/keepalived/notify.sh"
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass keepalivedpass
}
virtual_ipaddress {
172.16.100.98 dev eth0 label eth0:1
}
track_script {
chk_downfile
}
track_script {
chkngx
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
notify "/etc/keepalived/notify.sh"
}
master2为:
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass keepalivedpass
}
virtual_ipaddress {
172.16.100.99 dev eth0 label eth0:0
}
track_script {
chk_downfile
chkngx
}
track_script {
chkngx
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
notify "/etc/keepalived/notify.sh"
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 52
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass keepalivedpass
}
virtual_ipaddress {
172.16.100.98 dev eth0 label eth0:1
}
track_script {
chk_downfile
}
track_script {
chkngx
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
notify "/etc/keepalived/notify.sh"
}

学习LVS+Keepalived必须阅读的三个文档。

1、 《Keepalived权威指南》下载见 http://www.linuxidc.com/Linux/2012-05/60951.htm

2、 《LVS手册》http://www.codesec.net/Linux/2016-03/129233.htm

3、 《Red_Hat_Enterprise_Linux-5-Virtual_Server_Administration-zh-CN》 http://www.codesec.net/Linux/2016-03/129234.htm

本文地址:http://www.codesec.net/Linux/2016-09/135111.htm


高可用集群技术之Keepalived实现LVS高可用并负载均衡Web服务

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

分页:12
转载请注明
本文标题:高可用集群技术之Keepalived实现LVS高可用并负载均衡Web服务
本站链接:http://www.codesec.net/view/481884.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(21)