LVS/DR + keepalived配置

本次配置在LVS/DR的基础上配置

这里为节省资源,把rs2作为keepalived

前面的lvs虽然已经配置成功也实现了负载均衡,但是我们测试的时候发现,当某台real server把httpd进程停掉,那么director照样会把请求转发过去,这样就造成了某些请求不正常。

所以需要有一种机制用来检测real server的状态,这就是keepalived。它的作用除了可以检测rs状态外,还可以检测备用director的状态,也就是说 keepalived可以实现ha集群的功能

启动keepalived服务会自动生成vip和ipvsadm规则,不需要再去执行/usr/local/sbin/lvs_dr.sh 脚本。

[root@rs1 ~]# /etc/init.d/nginx stop

[root@hy ~]# curl 192.168.11.100
curl: (7) couldn't connect to host
[root@hy ~]# curl 192.168.11.100
curl: (7) couldn't connect to host
[root@hy ~]# curl 192.168.11.100
curl: (7) couldn't connect to host
[root@hy ~]# curl 192.168.11.100
rs2rs2
[root@hy ~]# curl 192.168.11.100
curl: (7) couldn't connect to host
[root@hy ~]# curl 192.168.11.100
curl: (7) couldn't connect to host
[root@hy ~]# curl 192.168.11.100
curl: (7) couldn't connect to host
[root@hy ~]# curl 192.168.11.100
rs2rs2
View Code

前期准备:

为节约资源,这里dir不做keepalived,只给rs1作keepalived

三台机器:dir、rs1、rs2;rs2作备用机器

下面我们使用keepaliave操作和之前的ipvsadm操作是有些冲突的,所以若是之前配置过DR,请首先做如下操作:

[root@dir ~]ipvsadm -C

[root@dir ~]# ifconfig eth0:0  down

开始实验 :

[root@dir ~]# yum install -y keepalived

[root@rs2 ~]#yum install -y keepalived

主director的配置文件

vim /etc/keepalived/keepalived.conf

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 {
        192.168.11.100
    }
}
virtual_server 192.168.11.100 80{
    delay_loop 6  #(每隔6秒查询realserver状态)
    lb_algo rr     #(lvs 算法)
    lb_kind DR    #(Direct Route)
    nat_mask 255.255.255.0
    persistence_timeout 0 #(同一IP的连接0秒内被分配到同一台realserver)
    protocol TCP  #(用TCP协议检查realserver状态)

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

rs2

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 {
        192.168.11.100
    }
}
virtual_server 192.168.11.100 80{
    delay_loop 6  #(每隔6秒查询realserver状态)
    lb_algo rr     #(lvs 算法)
    lb_kind DR    #(Direct Route)
    nat_mask 255.255.255.0
    persistence_timeout 0 #(同一IP的连接0秒内被分配到同一台realserver)
    protocol TCP  #(用TCP协议检查realserver状态)

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

 [root@dir ~]# /etc/init.d/keepalived start

 [root@rs2 ~]# /etc/init.d/keepalived start

 [root@rs1 ~]# /etc/init.d/nginx start

[root@dir ~]# ip add

测试:

两台机器都可以访问

[root@hy ~]# curl 192.168.11.100
rs1rs1
[root@hy ~]# curl 192.168.11.100
rs2rs2
[root@hy ~]# curl 192.168.11.100
rs1rs1
[root@hy ~]# curl 192.168.11.100
rs2rs2
[root@hy ~]# curl 192.168.11.100
rs1rs1
[root@hy ~]# curl 192.168.11.100
rs2rs2
View Code

[root@rs1 ~]# /etc/init.d/nginx stop只能访问rs2

[root@hy ~]# curl 192.168.11.100
rs2rs2
[root@hy ~]# curl 192.168.11.100
rs2rs2
[root@hy ~]# curl 192.168.11.100
rs2rs2
[root@hy ~]# curl 192.168.11.100
rs2rs2
[root@hy ~]# curl 192.168.11.100
rs2rs2
[root@hy ~]# curl 192.168.11.100
rs2rs2
View Code

 

posted @ 2016-02-19 14:50  沐风先生  阅读(222)  评论(0编辑  收藏  举报