lvs keepalive实现双主形式(选用D中华V),同时完

作者:电脑系统

vrrp_instance VI_1 {
state BACKUP                                  #另外一台keepalive这里换成MASTER
interface eth0
virtual_router_id 51 
priority 98                                         #这里100
advert_int 1
authentication {
auth_type PASS
auth_pass 11112222
}
virtual_ipaddress {
10.60.196.186/32 label eth0:0
}
}

<h1>realserver1</h1>

service httpd start

real2上:

ifconfig lo:0 192.168.19.150 netmask 255.255.255.255 broadcast 192.168.19.150 up
ifconfig lo:1 192.168.19.151 netmask 255.255.255.255 broadcast 192.168.19.151 up
route add -host 192.168.19.150 dev lo:0
route add -host 192.168.19.151 dev lo:1

配置完keepalived后,需要开启端口转发(主从都要做):

如果还需要外网可以通过另外搭建搭建两台机器做iptables转发   上面需要有一个公网IP和一个内网IP,通过iptables把外网请求全部转发到VIP上面,一台做一个VIP转发 (iptables这里就不写了),这样就可以实现跨网段的通讯了!    不同网段原因一样

vim /var/www/html/index.html

real_server 10.222.138.201 80 {
weight 100 #(权重)
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 10.222.138.202 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}

 

<h1>realserver2</h1>

service httpd start

美高梅手机登录网站 1

二、安装并配置keepalived

1.编译安装keepalived,在keep1和keep2上操作:

wget
tar xf keepalived-1.2.19.tar.gz
cd keepalived-1.2.19
./configure --prefix=/usr/local/keepalived
make && make install
cp -p /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
chkconfig --add keepalived
chkconfig keepalived on
ln -sv /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
cp -p /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
ln -sv /usr/local/keepalived/etc/keepalived/ /etc/keepalived
yum -y install ipvsadm                //安装LVS工具

复制代码

  2.配置keepalived:

    keep1上:

real server2(eth0 rip: 10.222.138.202, vip lo:0: 10.222.138.10)

real_server 10.60.196.181 33001 {
weight 1
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}

vim /var/www/html/index.html

state MASTER  -> state BACKUP

ifconfig eth0:1 10.60.196.185 netmask 255.255.255.0

vim /etc/keepalived/keepalived.conf

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 31
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass abcd
    }
    virtual_ipaddress {
        192.168.19.150
    }
}

vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 41
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass abcd
    }
    virtual_ipaddress {
        192.168.19.151
    }
}

virtual_server 192.168.19.150 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    protocol TCP

  real_server 192.168.19.29 {
        weight 1
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }

  real_server 192.168.19.34 {
        weight 1
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
}

virtual_server 192.168.19.151 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    protocol TCP

  real_server 192.168.19.29 {
        weight 1
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }

  real_server 192.168.19.34 {
        weight 1
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
}

3.两台机器启动keepalived:

service keepalived start
ipvsadm -L -n              //用LVS工具查看keepalived运行
ip addr show              //查看VIP

美高梅手机登录网站 2

    两台机器上LVS规则都已经生效,且2个VIP分别运行在2个节点:

      keep1上:

美高梅手机登录网站 3

      keep2上:

美高梅手机登录网站 4

    分别打开测试页面进行测试:

美高梅手机登录网站 5

美高梅手机登录网站 6

美高梅手机登录网站 7

美高梅手机登录网站 8

    停掉任何一个节点,资源都会自动转移:

美高梅手机登录网站 9

  至此,演示完毕,谢谢!

CentOS 6.3下Haproxy Keepalived Apache配置笔记

Haproxy KeepAlived 实现WEB群集 on CentOS 6

Keepalived Haproxy配置高可用负载均衡

Haproxy Keepalived构建高可用负载均衡

CentOS 7 上配置LVS Keepalived ipvsadm

Keepalived高可用集群搭建

LVS/DR Keepalived搭建负载均衡集群

LVS Keepalived实现四层负载及高可用 

LVS Keepalived高可用负载均衡集群架构实验 

Heartbeat LVS构建高可用负载均衡集群 

搭建LVS负载均衡测试环境 

一个针对LVS的压力测试报告

Keepalived构建高可用LVS集群 

Keepalived 的详细介绍:请点这里
Keepalived 的下载地址:请点这里

本文永久更新链接地址:

背景知识: Keepalived:Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障...

 

lvs1: DIP 10.60.196.183

vim /etc/keepalived/keepalived.conf

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 31
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass abcd
    }
    virtual_ipaddress {
        192.168.19.150
    }
}

vrrp_instance VI_2 {
    state BACKUP
    interface eth0
    virtual_router_id 41
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass abcd
    }
    virtual_ipaddress {
        192.168.19.151
    }
}

virtual_server 192.168.19.150 80 {
    delay_loop 6
    lb_algo rr                        //LVS算法
    lb_kind DR                        //调度类型
    protocol TCP

  real_server 192.168.19.29 {
        weight 1
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }

  real_server 192.168.19.34 {
        weight 1
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
}

virtual_server 192.168.19.151 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    protocol TCP

  real_server 192.168.19.29 {
        weight 1
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }

  real_server 192.168.19.34 {
        weight 1
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
}

keep2上:

[root@rs1 ~]# cat /usr/local/sbin/lvs_rs.sh
#! /bin/bash
vip=10.222.138.10
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip 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

RS1和RS2配置相同

基于Keepalived双主模型的高可用LVS

背景知识:

  Keepalived:Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web 服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器。

  LVS:LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统。

实验系统:CentOS 6.6_x86_64

实验前提:提前准备好编译环境,防火墙和selinux都关闭

实验说明:本实验共有4台主机,其中keep1和keep2为2台前端的keepalived服务器,real1和real2为LVS中的realserver,IP地址对应如拓扑图。

实验软件:httpd-2.2.15 keepalived-1.2.19

实验拓扑:

美高梅手机登录网站 10

一、配置realserver

1.安装httpd:

yum -y install httpd

2.配置内核参数:

echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore          //仅在请求的地址配置在请求报文的接口进行响应
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce        //表示仅通告网络直连的接口的地址
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce

3.增加测试页面并配置VIP:

real1上:

ifconfig lo:0 192.168.19.150 netmask 255.255.255.255 broadcast 192.168.19.150 up    //配置VIP
ifconfig lo:1 192.168.19.151 netmask 255.255.255.255 broadcast 192.168.19.151 up
route add -host 192.168.19.150 dev lo:0                                            //配置路由
route add -host 192.168.19.151 dev lo:1

三台机器:

ifconfig eth0:0 10.60.196.186 netmask 255.255.255.0

 

real_server 10.60.196.181 33001 {
weight 1
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}

 

real_server 10.60.196.182 33001 {
weight 1
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}

 

    因业务需求,需要把lvs备机也使用上,故! 使用双主,相互是主的同时也相互是备机。本人用nat测试发现RS无法实现负载均衡,故采用DR模式来实现非web端的负载均衡

 

! Configuration File for keepalived

 LVS/DR keepalived配置

real_server 10.60.196.182 33001 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}

echo 1 > /proc/sys/net/ipv4/ip_forward


 

route add -host 10.60.196.186 dev eth0:0

dr上执行:

virtual_server 10.60.196.185 33001 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol UDP
persistence_timeout 0

dir backup :

 

注意:前面虽然我们已经配置过一些操作,但是下面我们使用keepaliave操作和之前的操作是有些冲突的,所以若是之前配置过DR,请首先做如下操作:

 

 

 

/etc/init.d/keepalived start

LVS:需要开启IP转发

real_server 10.222.138.201 80 {
weight 100 #(权重)
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 10.222.138.202 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}

real_server 10.60.196.182 33001 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}

[root@dir network-scripts]# vim /etc/keepalived/keepalived.conf

DR1:10.60.196.181

前面的lvs虽然已经配置成功也实现了负载均衡,但是我们测试的时候发现,当某台real server把httpd进程停掉,那么director照样会把请求转发过去,这样就造成了某些请求不正常。所以需要有一种机制用来检测real server的状态,这就是keepalived。它的作用除了可以检测rs状态外,还可以检测备用director的状态,也就是说keepalived可以实现ha集群的功能,当然了也需要一台备用director.

        VIP 10.60.196.185

yum install -y keepalived (分别在2台director 上面安装)

 

测试: 关闭其中一台rs1的nginx服务, 客户端访问时只达到rs2,比较智能。

配置好以后添加路由

dir master:

rs:需要添加路由同时绑定VIP

ipvsadm -C

route add -host 10.60.196.185 dev eth0:1

以上为主director的配置文件,从director的配置文件只需要修改

virtual_server 10.60.196.186 33001 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol UDP
persistence_timeout 0

然后,两个rs上执行 /usr/local/sbin/lvs_dr_rs.sh 脚本  (配置文件上次lvs-dr实验已建立)

 

 

我这里TCP和UDP同时使用

ifconfig eth0:1  down

real_server 10.60.196.181 33001 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}

 

DR2:10.60.196.182

 

global_defs {
notification_email {
root@localhost
}
notification_email_from admin@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}

 

real_server 10.60.196.181 33001 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}

real server1(eth0 rip: 10.222. 138.201, vip lo:0: 10.222.138.10)

route add -host 10.60.196.185 dev eth0:1

 

         VIP 10.60.196.186

 

lvs2:DIP 10.60.196.184

备用director也需要安装一下keepalived软件

route add -host 10.60.196.186 dev eth0:0

vrrp_instance VI_1 {
state BACKUP #备用服务器上为 BACKUP
interface eth0
virtual_router_id 51
priority 90 #备用服务器上为90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.222.138.10
}
}
virtual_server 10.222.138.10 80 {
delay_loop 6 #(每隔10秒查询realserver状态)
lb_algo rr #(lvs 算法)
lb_kind DR #(Direct Route)
persistence_timeout 00 #(同一IP的连接60秒内被分配到同一台realserver)
protocol TCP #(用TCP协议检查realserver状态)

 

 

vrrp_instance VI_2 {
state MASTER            #另一台BUCKUP
interface eth0
virtual_router_id 150
priority 100          # 这里99
advert_int 1
authentication {
auth_type PASS
auth_pass 11112222
}
virtual_ipaddress {
10.60.196.185/32 label eth0:1
}
}

 

vim /etc/sysctl.conf

做到可负载均衡和高可用的结合。

virtual_server 10.60.196.185 33001 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
persistence_timeout 0

[root@dir02 ~]# vim /etc/keepalived/keepalived.conf

virtual_server 10.60.196.186 33001 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
persistence_timeout 0

vrrp_instance VI_1 {
state MASTER #备用服务器上为 BACKUP
interface eth0
virtual_router_id 51
priority 100 #备用服务器上为90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.222.138.10
}
}
virtual_server 10.222.138.10 80 {
delay_loop 6 #(每隔10秒查询realserver状态)
lb_algo rr #(lvs 算法)
lb_kind DR #(Direct Route)
persistence_timeout 00 #(同一IP的连接60秒内被分配到同一台realserver)
protocol TCP #(用TCP协议检查realserver状态)

keepalive配置:

 

real_server 10.60.196.182 33001 {
weight 1
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}

另外,需要注意的是,启动keepalived服务会自动生成vip和ipvsadm规则,不需要再去执行上面提到的/usr/local/sbin/lvs_dr.sh 脚本。

priority 100 -> priority 90

director(eth010.222.138.200, vip eth0:1: 10.222.138.10)

最后,两个director上启动keepalived服务(先主后从):

关闭 dir上的keepalived, 备用的keepalived开始启用。

本文由分分快三计划发布,转载请注明来源

关键词: lvs