lvs+keepalived(DR) 博客分类: lvs+keepalived(DR)
DR工作模式参考:http://os.51cto.com/art/201105/264303.htm
LVS详细参考: http://www.it165.net/admin/html/201401/2248.html
一、搭建环境
master:192.168.181.129 (director)
backup:192.168.181.133
realserver:192.168.181.131
realserver:192.168.181.132
vip: 192.168.181.134
系统均为CentOS 6.6
二、安装:
yum install -y ipvsadm
yum install -y keepalived
Director 上 vim /usr/local/sbin/lvs_dr.sh //增加
#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
ipv=/sbin/ipvsadm
vip=192.168.181.134
rs1=192.168.181.131
rs2=192.168.181.132
ifconfig eth0:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip dev eth0:0
$ipv -C
$ipv -A -t $vip:80 -s rr
$ipv -a -t $vip:80 -r $rs1:80 -g -w 1
$ipv -a -t $vip:80 -r $rs2:80 -g -w 1
两台realserver上:vim /usr/local/sbin/lvs_dr_rs.sh //增加
#! /bin/bash
vip=192.168.181.134
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
#避免arp广播问题
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignoreecho "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
编写配置文件:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
#邮件部分不需要的话可注释
global_defs {
notification_email {
coldnight@linuxzen.com # 发生故障时发送的邮箱
}
notification_email_from linuxzen@linuxzen.com # 使用哪个邮箱发送
smtp_server linuxzen.com # 发件服务器
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER #备用服务器上为 BACKUP
interface eth0 # HA检测端口
virtual_router_id 51 # 主备的virtual_router_id 必须相同
priority 100 # 优先级,备lvs要比主lvs稍小,备用服务器上为90
advert_int 1 # VRRP Multicast 广播周期秒数
authentication { # 定义认证
auth_type PASS # 认证方式为口令认证
auth_pass 1111 # 定义口令
}
virtual_ipaddress { #定义vip
192.168.181.134 # 多个vip可换行添加
}
}
virtual_server 192.168.181.134 80 {
delay_loop 6 # 每隔6秒查看realserver状态
lb_algo wlc # 调度算法为加权最小连接数(lvs算法)
lb_kind DR # lvs工作模式为DR(直接路由)模式
persistence_timeout 50 # 同一IP 的连接50秒内被分配到同一台realserver
protocol TCP # 用TCP监测realserver的状态
real_server 192.168.181.131 80 {
weight 100 #权重
TCP_CHECK {
connect_timeout 10 #(10s无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.181.132 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.181.129 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
以上为主director的配置文件,从director的配置文件只需要修改
state MASTER -> state BACKUP
priority 100 -> priority 90
配置完keepalived后,需要开启端口转发(主从都要做):
echo 1 > /proc/sys/net/ipv4/ip_forward
然后,两个rs上执行 /usr/local/sbin/lvs_dr_rs.sh 脚本
最后,两个director上启动keepalived服务(先主后从):
/etc/init.d/keepalived start
可将master或者backup上的keepalived服务停掉做模拟故障,日志在/var/log/message上显示
上一篇: 盘点程序员新年要做的10个决定