trhhrth 发表于 2014-9-23 10:15:03

keepalived+LVS

本节我们来讲一讲keepalived,它是基于虚拟路由冗余协议vrrp (Virtual Router Redundancy Protocol)从而实现模拟路由器的高可用,通常Keepalived用于实现前端高可用,它是轻量级的高可用,且不需要共享存储,常用于两个节点的高可用,一个是主节点master,一个是从节点backup,但是对外边来说只有一个IP地址,通常这个IP地址一开始都是在master,当master宕机时,backup会将该IP抢走并提供服务。好了下面我们就来实现下这个过程。
实验拓扑图:

本实验是实现keepalived+lvs的DR模型,这里介绍下DR模型的工作机制,当外部的请求送给director(即DR)时,DR将请求转发给内部的real server(即RS)进行响应,而响应后的结果并没有通过DR返回给用户,而是RS直接响应给互联网上的客户端,但是由于RS在内网当中,因此需要额外加个路由器给RS,并且RS必须将网关指向该路由器。
为了实验简便,本实验涉及的服务全部为yum安装,这里就不给出了。本实验是在已安装好的前提下配置的。
RS配置
RS1:

1
2
3
4
5
6
7
8
9
echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
ifconfig eth0 192.168.1.21/24 up
ifconfig lo:0 172.16.6.31 netmask 255.255.255.255 broadcast 172.16.6.31
route add -host 172.16.6.31dev lo:0配置为lo:0端口
ifconfig lo:1 172.16.6.32netmask 255.255.255.255 broadcast 172.16.6.32
route add -host 172.16.6.32dev lo:1





RS2:

1
2
3
4
5
6
7
8
9
echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
ifconfig eth0 192.168.1.22/24 up            与RS1相比只有IP不同
ifconfig lo:0 172.16.6.31 netmask 255.255.255.255 broadcast 172.16.6.31
route add -host 172.16.6.31dev lo:0
ifconfig lo:1 172.16.6.32netmask 255.255.255.255 broadcast 172.16.6.32
route add -host 172.16.6.32dev lo:1





配置keepalived
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs
{
notification_email
{
root@localhost
}
notification_email_from kanotify@magedu.com
smtp_connect_timeout 3
smtp_server 127.0.0.1
router_id LVS_DEVEL
}
vrrp_script chk_schedown
{
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
#创建down文件权重减2
interval 2
weight -2
}
vrrp_instance VI_1            在第一个节点扮演的是主节点的角色
{
interface eth0         
state MASTER                  #此节点为主节点         
priority 100                  优先级
virtual_router_id 1         
garp_master_delay 1


authentication                  认证部分
{         
auth_type PASS      
auth_pass password               
}
track_interface
{
eth0                        
}
virtual_ipaddress
{
172.16.6.31/16               设定ipvs 规则的虚拟ip                     
   }
track_script
{
chk_schedown
}
}
virtual_server 172.16.6.31 80#配置一个VIP 工作在TCP的80端口上
{               

delay_loop 6
lb_algo rr   #负载均衡的调度算法
lb_kind DR   #工作在DR模型上
#persistence_timeout 50            #是否启用IPVS持久连接,这项要注销.连接50S才会切换
protocol TCP                     #采用的是TCP协议
# sorry_server 192.168.1.2 1358 #错误提示页面.要在这台服务器上开启http服务

real_server 192.168.1.21 80          #后面RIP地址
{
weight 1 #权重
HTTP_GET
{
url
{
path /
status_code 200 #取得主页面的状态,状态码为200就意味着请求成功;也可写入md5码,但要制定静态码
}
connect_timeout 3 #每次测试3秒
nb_get_retry 3 #测试次数
delay_before_retry 3 #测试失败在测试3次
}
}
real_server 192.168.1.22 80
{
weight 1
HTTP_GET
{
url
{
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
vrrp_instance VI_2               在第二个模型中扮演的备节点的角色
{
interface eth0
state BACKUP          #此节点为备节点
priority 99            
virtual_router_id 2
garp_master_delay 1
authentication {      
auth_type PASS
auth_pass 12345678
}
track_interface {
eth0
}
virtual_ipaddress {
172.16.6.32/16               设定ipvs 规则的虚拟
}
track_script {
chk_schedown
}
}
virtual_server 172.16.6.32 80#配置一个VIP 工作在TCP的80端口上
{               

delay_loop 6
#lb_algo rr      
lb_kind DR      
persistence_timeout 50      用于测试这边启用持久连接功能
protocol TCP
# sorry_server 192.168.1.2 1358   
real_server 192.168.1.21 80      
{
weight 1
HTTP_GET
{
url
{         
path /   
status_code 200
}
connect_timeout 3      
nb_get_retry 3      
delay_before_retry 3                        
}
}
real_server 192.168.1.22 80
{
weight 1
HTTP_GET
{
url
{
path /
status_code 200   
}
connect_timeout 3nb_get_retry 3delay_before_retry 3   
}
}
}




配置第二个节点keepalived
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs
{
notification_email
{
root@localhost
}
notification_email_from kanotify@magedu.com
smtp_connect_timeout 3
smtp_server 127.0.0.1
router_id LVS_DEVEL
}
vrrp_script chk_schedown
{
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
#创建down文件权重减2
interval 2
weight -2
}
vrrp_instance VI_1            在第一个节点扮演的是从节点的角色
{
interface eth0         
state BACKUP                  #此节点为从节点         
priority 99                      优先级
virtual_router_id 1         
garp_master_delay 1


authentication                  认证部分
{         
auth_type PASS      
auth_pass password               
}
track_interface
{
eth0                        
}
virtual_ipaddress
{
172.16.6.31/16                设定ipvs 规则的虚拟ip                     
   }
track_script
{
chk_schedown
}
}
virtual_server 172.16.6.31 80#配置一个VIP 工作在TCP的80端口上
{               

delay_loop 6
lb_algo rr   #负载均衡的调度算法
lb_kind DR   #工作在DR模型上
#persistence_timeout 50            #是否启用IPVS持久连接,这项要注销.连接50S才会切换
protocol TCP                     #采用的是TCP协议
# sorry_server 192.168.1.2 1358 #错误提示页面.要在这台服务器上开启http服务

real_server 192.168.1.21 80          #后面RIP地址
{
weight 1 #权重
HTTP_GET
{
url
{
path /
status_code 200 #取得主页面的状态,状态码为200就意味着请求成功;也可写入md5码,但要制定静态码
}
connect_timeout 3 #每次测试3秒
nb_get_retry 3 #测试次数
delay_before_retry 3 #测试失败在测试3次
}
}
real_server 192.168.1.22 80
{
weight 1
HTTP_GET
{
url
{
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
vrrp_instance VI_2               在第二个模型中扮演的主节点的角色
{
interface eth0
state MASTER         #此节点为主节点
priority 100             注意修改节点状态的时候优先级也要改
virtual_router_id 2
garp_master_delay 1
authentication {      
auth_type PASS
auth_pass 12345678
}
track_interface {
eth0
}
virtual_ipaddress {
172.16.6.32/16               设定ipvs 规则的虚拟
}
track_script {
chk_schedown
}
}
virtual_server 172.16.6.32 80#配置一个VIP 工作在TCP的80端口上
{               

delay_loop 6
#lb_algo rr      
lb_kind DR      
persistence_timeout 50      用于测试这边启用持久连接功能
protocol TCP
# sorry_server 192.168.1.2 1358   
real_server 192.168.1.21 80      
{
weight 1
HTTP_GET
{
url
{         
path /   
status_code 200
}
connect_timeout 3      
nb_get_retry 3      
delay_before_retry 3                        
}
}
real_server 192.168.1.22 80
{
weight 1
HTTP_GET
{
url
{
path /
status_code 200   
}
connect_timeout 3nb_get_retry 3delay_before_retry 3   
}
}
}

OK,全部配置完成之后就可以了,本节就到这里,谢谢!

页: [1]
查看完整版本: keepalived+LVS