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


环境搭建工作:
主director    192.168.11.30    eth1网卡
从director    192.168.11.40    eth1网卡
real server1:    192.168.11.100    eth0网卡
real server2:    192.168.11.101    eth0网卡

用来curl测试的linux主机 192.168.11.0 网段即可;
主dr和备用dr都需要安装keepalived,ipvsadm;
两台rs安装nginx;


安装好后,主director的配置文件
vim /etc/keepalived/keepalived.conf  //加入如下:
vrrp_instance VI_1 {
    state MASTER  #备用服务器上为 BACKUP
    interface eth1
    virtual_router_id 51
    priority 100  #优先级,数值越大优先级越高;备用服务器上为90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.11.110
    }
}
virtual_server 192.168.11.110 80 {
    delay_loop 6                  #(每隔6秒查询realserver状态,是否存活)
    lb_algo wlc                  #(轮询算法)
    lb_kind DR                  #(Direct Route)
    persistence_timeout 0    #(同一IP的连接多少秒内被分配到同一台realserver,0表示不连接)
    protocol TCP                #(用TCP协议检查realserver状态)
    real_server 192.168.11.100 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.101 80 {
        weight 100
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }
}

从director的配置文件只需要修改下面两项:
state MASTER  -> state BACKUP
priority 100 -> priority 90
配置完keepalived后,需要开启端口转发(主从dr都要做):
echo 1 > /proc/sys/net/ipv4/ip_forward
然后,两个rs上执行 /usr/local/sbin/lvs_dr_rs.sh 脚本,启动nginx服务
# /etc/init.d/nginx start

最后,两个director上启动keepalived服务(先主后从):
# /etc/init.d/keepalived start

另外,需要注意的是,启动keepalived服务会自动生成vip和ipvsadm规则.


使用命令#ip addr    查看dr的虚拟ip地址;直接使用ifconfig不显示虚拟ip;
[root@dr1 keepalived]# ip addr
eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
    link/ether 00:0c:29:97:c3:f6 brd ff:ff:ff:ff:ff:ff
    inet 192.168.11.30/24 brd 192.168.11.255 scope global eth1
   
    inet 192.168.11.110/32 scope global eth1
    inet6 fe80::20c:29ff:fe97:c3f6/64 scope link 
    valid_lft forever preferred_lft forever

在其他机器curl测试,请求rs1和rs2次数相当;
[root@localhost ~]# curl 192.168.11.110
rs1rs1
[root@localhost ~]# curl 192.168.11.110
rs2rs2
[root@localhost ~]# curl 192.168.11.110
rs1rs1
[root@localhost ~]# curl 192.168.11.110
rs2rs2

rs2上面stop nginx,然后curl测试,发现所有的请求都到rs1上面了;
日志里面也会记录remove rs2;日志文件:/var/log/messages
[root@rs2 ~]# /etc/init.d/nginx stop

[root@localhost ~]# curl 192.168.11.110
rs1rs1
[root@localhost ~]# curl 192.168.11.110
rs1rs1
[root@localhost ~]# curl 192.168.11.110
rs1rs1
[root@localhost ~]# curl 192.168.11.110
rs1rs1

[root@dr1 ~]# tail -2 /var/log/messages
Jun  9 23:27:19 localhost Keepalived_healthcheckers[1572]: TCP connection to [192.168.11.101]:80 failed !!!
Jun  9 23:27:19 localhost Keepalived_healthcheckers[1572]: Removing service [192.168.11.101]:80 from VS [192.168.11.110]:80

rs2启动nginx,日志文件记录adding rs2;curl测试,发现请求平均分配到rs1和rs2上面了;

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

[root@dr1 ~]# tail -2 /var/log/messages
Jun  9 23:31:38 localhost Keepalived_healthcheckers[1572]: TCP connection to [192.168.11.101]:80 success.
Jun  9 23:31:38 localhost Keepalived_healthcheckers[1572]: Adding service [192.168.11.101]:80 to VS [192.168.11.110]:80

[root@localhost ~]# curl 192.168.11.110
rs1rs1
[root@localhost ~]# curl 192.168.11.110
rs2rs2
[root@localhost ~]# curl 192.168.11.110
rs1rs1
[root@localhost ~]# curl 192.168.11.110
rs2rs2

加入dr2 备用dircetor机器;
主上停止keepalive服务;stop之后,在从上ip addr查看绑定虚拟ip,说明从接管了服务;切换速度很快;
主上启动keepalived服务后,主绑定虚拟ip,接管服务;
[root@dr2 keepalived]# ip addr
eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
    link/ether 00:0c:29:af:73:3f brd ff:ff:ff:ff:ff:ff
    inet 192.168.11.40/24 brd 192.168.11.255 scope global eth1
    inet 192.168.11.110/32 scope global eth1

nc命令可以扫描端口是否打开:

在其他机器上扫描,11.100 和11.101,11.110的80端口是否打开;

#nc -z -w2 192.168.11.110 80
[root@localhost ~]# nc -z -w2 192.168.11.100 80
Connection to 192.168.11.100 80 port [tcp/http] succeeded!
[root@localhost ~]# nc -z -w2 192.168.11.101 80
Connection to 192.168.11.101 80 port [tcp/http] succeeded!
[root@localhost ~]# nc -z -w2 192.168.11.110 80
Connection to 192.168.11.110 80 port [tcp/http] succeeded!

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

猜你喜欢

转载自www.linuxidc.com/Linux/2015-06/118647.htm