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

本文涉及的产品
传统型负载均衡 CLB,每月750个小时 15LCU
网络型负载均衡 NLB,每月750个小时 15LCU
应用型负载均衡 ALB,每月750个小时 15LCU
简介:

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

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

keepalived调用lvs来实现自己的规则;

yum install -y keepalived ipvsadm


环境搭建工作:

主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   //加入如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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服务

1
# /etc/init.d/nginx start

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

1
# /etc/init.d/keepalived start

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


使用命令#ip addr    查看dr的虚拟ip地址;直接使用ifconfig不显示虚拟ip;

1
2
3
4
5
6
7
8
[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次数相当;

1
2
3
4
5
6
7
8
[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

1
[root@rs2 ~] # /etc/init.d/nginx stop
1
2
3
4
5
6
7
8
[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
1
2
3
[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上面了;

1
[root@rs2 ~] # /etc/init.d/nginx start
1
2
3
[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
1
2
3
4
5
6
7
8
[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,接管服务;

1
2
3
4
5
[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

1
2
3
4
5
6
[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!






本文转自 模范生 51CTO博客,原文链接:http://blog.51cto.com/mofansheng/1660329,如需转载请自行联系原作者

相关实践学习
SLB负载均衡实践
本场景通过使用阿里云负载均衡 SLB 以及对负载均衡 SLB 后端服务器 ECS 的权重进行修改,快速解决服务器响应速度慢的问题
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
9天前
|
运维 负载均衡 网络协议
LVS+Keepalived 负载均衡
LVS+Keepalived 负载均衡
32 1
LVS+Keepalived 负载均衡
|
6天前
|
域名解析 运维 负载均衡
LVS+Keepalived 负载均衡(二)28-1
【8月更文挑战第28天】LVS+Keepalived 负载均衡 配置 LVS VIP
21 5
|
2月前
|
负载均衡 网络协议
使用LVS搭建集群实现负载均衡(二)安装使用
【8月更文挑战第8天】使用LVS搭建集群实现负载均衡(二)安装使用
43 4
|
2月前
|
存储 负载均衡 算法
使用LVS搭建集群实现负载均衡(一)
【8月更文挑战第8天】使用LVS搭建集群实现负载均衡
68 4
|
2月前
|
缓存 负载均衡 算法
在Linux中, LVS负载均衡有哪些策略?
在Linux中, LVS负载均衡有哪些策略?
|
2月前
|
负载均衡 监控 算法
在Linux中,如何配置和管理LVS集群?
在Linux中,如何配置和管理LVS集群?
|
12月前
|
负载均衡 应用服务中间件 Linux
企业实战(13)LVS负载均衡NAT(网络地址转换)模式实战详解(一)
企业实战(13)LVS负载均衡NAT(网络地址转换)模式实战详解(一)
155 0
|
12月前
|
存储 负载均衡 网络协议
企业实战(13)LVS负载均衡DR(直接路由)模式实战详解(二)
企业实战(13)LVS负载均衡DR(直接路由)模式实战详解(二)
199 0
|
11月前
|
负载均衡 应用服务中间件 Linux
Nginx系列教程(14) - LVS+KeepAlived+Nginx实现高性能负载均衡集群
Nginx系列教程(14) - LVS+KeepAlived+Nginx实现高性能负载均衡集群
841 0
|
5月前
|
负载均衡 网络协议 算法
LVS 负载均衡部署的三种模式 与搭建dr模式具体步骤
LVS 负载均衡部署的三种模式 与搭建dr模式具体步骤
下一篇
无影云桌面