Keepalived实现LVS-DR模型的高可用
>>>拓扑结构简介
本次实验所使用的系统发行版本为:centos6.6
(nod1)路由器,为一台linux主机模拟,共有两个接口eth0内网172.16.0.0/16网段,并且该接口为内网RS的网关;eth1为外网192.168.1.0/24网段,为外网客户端的网关
(nod55)为MASTER调度器,DIP为172.16.13.55/16,keepalived工作于抢占模式
(nod44)为BACKUP调度器,DIP为172.16.13.44/16,keepalived工作于抢占模式
(nod22)为RS1,运行httpd服务,负责提供web服务
(nod33)为RS2,运行httpd服务,负责提供web服务
(Client)使用一台linux虚拟机模拟,网关指向nod1的eth1接口
实验目的
利用keepalived,实现LVS-DR模型的双机热备。当nod55故障停机,nod44自动抢占VIP地址,并提供LVS调度功能;并且当nod55修复上线后,其自动抢占VIP,继续作为MASTER服务器
实验准备
在VMware中创建6台,nod22、nod33、nod44、nod55各装配有一块网卡,为桥接模式,nod1的eth0网卡为桥接模式,wth1网卡为自定义网络vmnet10;Client(nod66)网卡也为自定义网络vmnet10
实验配置:以下配置基本为当前生效,重启生效,若想永久生效需自行修改保存对应的配置文件
关于keepalived配置参数介绍,请参见http://imsupeng.blog.51cto.com/10651675/1703673
(nod55)MASTER的配置:提前配置好eth0的IP为172.16.13.55/16,网关无需指定
>>>关闭selinux,清空防火墙规则
[[email protected] ~]# setenforce 0 [[email protected] ~]# iptables –F
>>>由于keepalive的配置前提必须(1、主备节点时间同步;2、主备节点主机名必须是使用hostname或uname –r命令获取的一直,并且能互相解析);因此,需要修改主机名并添加主备节点的hosts文件解析
[[email protected] ~]# sed -i ‘s/\(HOSTNAME=\).*/\1nod55/‘ /etc/sysconfig/network && hostname nod55 [[email protected] ~]# echo -e ‘127.0.0.1nod55\n172.16.13.44 nod44‘ >> /etc/hosts ##172.16.13.44为BACKUP节点的DIP
>>>安装keepalived,并进行配置;
[[email protected] ~]# yum install -y keepalived ##需事先配置好yum源
[[email protected] ~]# vim /etc/keepalived/keepalived.conf ##编辑keepalived配置文件,将配置文件所有内容删除,粘贴如下配置
! 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 nod55 ##此项为当前主机的主机名
}
vrrp_script chk_down {
script "[[-e /etc/keepalived/down ]] && exit 1 || exit 0"
interval 1
weight -2
} ##此处为定义一个脚本,用于在线将主节点设置为维护模式,此处脚本的意义为:每隔一秒检查一下/etc/keepalived/目录下是否存在一个名为down的文件,此文件一旦存在,便会执行将调用此脚本的VRRP实例的优先级(priority)减去2.
vrrp_instance VI_1 {
state MASTER ##此处声明当前节点为MASTER节点
interface eth0 ##声明将VIP绑定在eth0网卡
virtual_router_id 13 ##主备节点此处必须相同,若有多个VRRP实例,每个实例的此项不能相同
priority 100 ##设置优先级,主节点必须大于备节点
advert_int 1 ##keepalive的状态通告周期时间/s
authentication {
auth_type PASS ##状态通告信息使用明文验证
auth_pass 5ffffc4b ##状态通告信息验证密码
}
virtual_ipaddress {
172.16.13.100/32 dev eth0 label eth0:1 ##VIP地址
}
track_script {
chk_down
} ##此处为调用前面所定义的脚本
}
virtual_server 172.16.13.100 80 {
delay_loop 3 ##对RealServer的健康状态探测的时间间隔
lb_algo rr ##LVS的调度算法
lb_kind DR ##LVS的类型,这里使用的是DR模型
nat_mask 255.255.255.255 ##VIP的掩码,需和VRRP实例中定义的掩码一致
protocol TCP
real_server 172.16.13.22 80 {
weight 1
HTTP_GET { ##声明状态监测的方法
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 172.16.13.33 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
>>>启动keepalived进程,并添加至开机自动启动
[[email protected] ~]# service keepalived start
[[email protected] ~]# chkconfig --add keepalived; chkconfig keepalived on
(nod44)MASTER的配置:提前配置好eth0的IP为172.16.13.44/16,网关无需指定
>>>关闭selinux,清空防火墙规则
[[email protected] ~]# setenforce 0
[[email protected] ~]# iptables –F
>>>修改主机名并添加主备节点的hosts文件解析
[[email protected] ~]# sed -i ‘s/\(HOSTNAME=\).*/\1nod44/‘ /etc/sysconfig/network && hostname nod44
[[email protected] ~]# echo -e ‘127.0.0.1 nod44\n172.16.13.55nod55‘ >> /etc/hosts ##172.16.13.55为MASTER节点的DIP
>>>安装keepalived,并进行配置;
[[email protected] ~]# yum install -y keepalived ##需事先配置好yum源
[[email protected] ~]# vim /etc/keepalived/keepalived.conf ##编辑keepalived配置文件,将配置文件所有内容删除,粘贴如下配置
! 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 nod44
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 13
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 5ffffc4b
}
virtual_ipaddress {
172.16.13.100/32 dev eth0 label eth0:1
}
}
virtual_server 172.16.13.100 80 {
delay_loop3
lb_algo rr
lb_kind DR
nat_mask 255.255.255.255
protocol TCP
real_server 172.16.13.22 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 172.16.13.33 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
>>>启动keepalived进程,并添加至开机自动启动
[[email protected] ~]# service keepalived start
[[email protected] ~]# chkconfig --add keepalived; chkconfig keepalived on
(nod22)RS1的配置:提前配置好eth0的IP为172.16.13.22/16,网关需指向172.16.13.1(router的IP)
>>>关闭selinux,清空防火墙规则
[[email protected] ~]# setenforce 0
[[email protected] ~]# iptables –F
>>>安装httpd,提供网页文件并启动httpd
[[email protected] ~]# yum install -y httpd ##需事先配置好yum源
[[email protected] ~]# echo "nod22" >/var/www/html/index.html
[[email protected] ~]# service httpd start
>>>在物理机测试http服务是否正常
>>>在/root目录下编写如下脚本,命名为initset.sh(自定义脚本名),内容如下
由于LVS-DR模型RS上需要配置有VIP地址并隔离本机的VIP的ARP通告,以防影响VIP对外部请求的接收及调度,此脚本便是实现这些功能,并设置VIP地址到lo网卡的别名上,注意,此处隔离ARP通告和设置IP地址的顺序不能随便
#!/bin/sh
#
case $1 in
start)
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig lo:0 172.16.13.100/32 broadcast 172.16.13.100 up
;;
stop)
ifconfig lo:0 down
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
;;
Esac
>>>运行此脚本,并向其传递start参数
[[email protected] ~]# bash /root/initset.sh start
(nod33)RS2的配置:提前配置好eth0的IP为172.16.13.33/16,网关需指向172.16.13.1(router的IP)
>>>关闭selinux,清空防火墙规则
[[email protected] ~]# setenforce 0
[[email protected] ~]# iptables –F
>>>安装httpd,提供网页文件并启动httpd
[[email protected] ~]# yum install -y httpd ##需事先配置好yum源
[[email protected] ~]# echo "nod33" >/var/www/html/index.html
[[email protected] ~]# service httpd start
>>>在物理机测试http服务是否正常
>>>在/root目录下编写如下脚本,命名为initset.sh(自定义脚本名),内容如下
#!/bin/sh
#
case $1 in
start)
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig lo:0 172.16.13.100/32 broadcast 172.16.13.100 up
;;
stop)
ifconfig lo:0 down
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
;;
Esac
>>>运行此脚本,并向其传递start参数
[[email protected] ~]# bash /root/initset.sh start
(nod1)ROUTER的网卡配置,无需指定网关
>>>关闭selinux及清空防火墙规则
[[email protected] ~]# iptables -F
[[email protected] ~]# setenforce 0
》》》开启主机的核心转发功能
[[email protected] ~]# echo 1 >/proc/sys/net/ipv4/ip_forward
>>>网卡配置
(Client)客户端IP设置为192.168.1.12/24
@@@此处再次声明:Client和ROUTER(nod1)的eth1连接在自定义网络vmnet10上,MASTER、BACKUP及RS的网卡都为桥接模式
第一步:由于在定义keepalived时,配置文件中创建了降级维护的脚本,因此首先在MASTER节点(nod55)上测试keepalived主备节点是否正常
>>>在/etc/keepalived/目录下创建一个名为“down”的文件,测试VIP是否能正常转移
[[email protected] ~]# cd /etc/keepalived/
[[email protected] keepalived]# touch down
[[email protected] keepalived]# ls
down keepalived.conf keepalived.conf.bak
测试正常,将nod55的down文件删除,使nod55再次成为MASTER节点
第二步:客户端使用curl命令进行请求测试:
测试正常
第三部:将MASTER节点置为维护状态(在nod55主机的/etc/keepalived目录下创建名为down的文件),然后再从客户端进行访问测试