首页 > 代码库 > Linux架构之LVS-DR和keepalived配置
Linux架构之LVS-DR和keepalived配置
简介
LVS是linux virtual server的简写linux虚拟服务器,是一个虚拟的服务器集群系统,可以再unix/linux平台下实现负载均衡集群功能。
DR模式(直接路由模式)
Virtual server via direct routing (vs/dr)
DR模式是通过改写请求报文的目标MAC地址,将请求发给真实服务器的,而真实服务器响应后的处理结果直接返回给客户端用户。同TUN模式一样,DR模式可以极大的提高集群系统的伸缩性。而且DR模式没有IP隧道的开销,对集群中的真实服务器也没有必要必须支持IP隧道协议的要求。但是要求调度器LB与真实服务器RS都有一块网卡连接到同一物理网段上,必须在同一个局域网环境。
DR模式是互联网使用比较多的一种模式。
DR模式原理图:
LVS调度算法的生产环境选型:
1、一般的网络服务,如http,mail,mysql等常用的LVS调度算法为:
a.基本轮询调度rr
b.加权最小连接调度wlc
c.加权轮询调度wrc
DR配置
Director server | chumjtest01 | eth0 172.31.24.199 | VIP:172.31.24.197 |
Realserver01 | chumjtest02 | eth0 172.31.24.200 | VIP lo:0:172.31.24.197 |
Realserver02 | chumjtest03 | eth0 172.31.24.198 | VIP lo:0:172.31.24.197 |
实验准备
1.关闭防火墙 iptables -F;
2.关闭selinux: setenforce 0
3.在Director Server安装好lvs
4.realserver都需要安装nginx
Dirserver配置
首先为了执行方便编写一个脚本
#!/bin/bash echo 1 > /proc/sys/net/ipv4/ip_forward //开启转发功能 ipv=/sbin/ipvsadm vip=172.31.24.197 //VIP的IP地址 rs1=172.31.24.200 //RS1的IP地址 rs2=172.31.24.198 //RS2的IP地址 ifconfig eth0:0 $vip broadcast $vip netmask 255.255.255.255 up route add -host $vip dev eth0:0 $ipv -C $ipv -A -t $vip:80 -s rr //轮询,意思是一次转发, $ipv -a -t $vip:80 -r $rs1:80 -g -w 1 //g表示LVS-DR模式 w表示权重 $ipv -a -t $vip:80 -r $rs2:80 -g -w 1
Realserver 配置
同样建立一个脚本,分别在放在两台RS上面。
#!/bin/bash vip=172.31.24.197 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
然后director上执行: bash /usr/local/sbin/lvs_dr.sh
两台rs上执行: bash /usr/local/sbin/lvs_dr_rs.sh
测试
[root@chumjtest04 ~]# curl 172.31.24.197 Chumjtest03
注:
主要realserver 权重不一样要修改dirserver配置文件
#!/bin/bash echo 1 > /proc/sys/net/ipv4/ip_forward //开启转发功能 ipv=/sbin/ipvsadm vip=172.31.24.197 //VIP的IP地址 rs1=172.31.24.200 //RS1的IP地址 rs2=172.31.24.198 //RS2的IP地址 ifconfig eth0:0 $vip broadcast $vip netmask 255.255.255.255 up route add -host $vip dev eth0:0 $ipv -C $ipv -A -t $vip:80 -s wrr //加权轮询,依照优先级转发 $ipv -a -t $vip:80 -r $rs1:80 -g -w 3 //w表示权重,修改权重 $ipv -a -t $vip:80 -r $rs2:80 -g -w 1
Keepalived安装
如果由之前的配置要清理下
ipvsadm -C
ifconfig eth0:0 down
由于环境限制这里将RS02当做keepalived备机,在真是环境中不建议这么做。最好另外有一台设备当做备机
进入keepalived,对配置文件进行配置
[root@chumjtest01 sbin]# cat /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.31.110 } } virtual_server 192.168.31.110 80 { delay_loop 6 #(每隔10秒查询realserver状态) lb_algo wlc #(lvs 算法) lb_kind DR #(Direct Route) persistence_timeout 60 #(同一IP的连接60秒内被分配到同一台realserver) protocol TCP #(用TCP协议检查realserver状态) real_server 192.168.31.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.31.101 80 { weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
将主机的keepalived配置文件拷贝到备机上
只需要修改
state MASTER -> state BACKUP
priority 100 -> priority 90
[root@chumjtest01 sbin]# scp keepalived.conf root@172.31.24.200:/etc/keepalived 进入chumjtest02 [root@chumjtest02 keepalived]# vim keepalived.conf 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 { 172.31.24.197 } } virtual_server 172.31.24.197 80 { delay_loop 6 #(每隔10秒查询realserver状态) lb_algo wlc #(lvs 算法) lb_kind DR #(Direct Route) persistence_timeout 60 #(同一IP的连接60秒内被分配到同一台realserver) protocol TCP #(用TCP协议检查realserver状态) real_server 172.31.24.200 80 { weight 100 #(权重) TCP_CHECK { connect_timeout 10 #(10秒无响应超时) nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 172.31.24.198 80 { weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
注:
1.配置完keepalived后需要开启端口转发(主从都需要)
2.RS上都需要执行lvs_dr_rs.sh脚本
3.两个director上启动keepalived服务(先主后从)
启动keepalived服务会自动生成vip和ipvsadm规则,不需要再去执行上面提到的/usr/local/sbin/lvs_dr.sh 脚本。
启动keepalived
[root@chumjtest01 ~]# service keepalived restart Stopping keepalived: [ OK ] Starting keepalived: [ OK ] [root@chumjtest01 ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 172.31.24.197:80 wlc persistent 60 -> 172.31.24.198:80 Route 100 0 0 -> 172.31.24.200:80 Route 100 0 0 [root@chumjtest01 sbin]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000 link/ether 00:50:56:b3:79:e3 brd ff:ff:ff:ff:ff:ff inet 172.31.24.199/24 brd 172.31.24.255 scope global eth0 inet 172.31.24.197/32 scope global eth0 //VIP已经启动 inet6 fe80::250:56ff:feb3:79e3/64 scope link valid_lft forever preferred_lft forever
测试
可以将keepalived的master服务停止,看下在备机上是否启动。
[root@chumjtest01 sbin]# service keepalived stop Stopping keepalived: [ OK ] [root@chumjtest01 sbin]#
从备机的日志上看到VIP已经在备机上启动
[root@chumjtest02 sbin]# tail -f /var/log/messages Jan 11 17:21:00 chumjtest02 Keepalived_vrrp[1998]: VRRP_Instance(VI_1) Transition to MASTER STATE Jan 11 17:21:01 chumjtest02 Keepalived_vrrp[1998]: VRRP_Instance(VI_1) Entering MASTER STATE Jan 11 17:21:01 chumjtest02 Keepalived_vrrp[1998]: VRRP_Instance(VI_1) setting protocol VIPs. Jan 11 17:21:01 chumjtest02 Keepalived_vrrp[1998]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.31.24.197 Jan 11 17:21:01 chumjtest02 Keepalived_healthcheckers[1997]: Netlink reflector reports IP 172.31.24.197 added Jan 11 17:21:06 chumjtest02 Keepalived_vrrp[1998]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.31.24.197
[root@chumjtest02 sbin]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet 172.31.24.197/32 brd 172.31.24.197 scope global lo:0 inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000 link/ether 00:50:56:b3:0e:c3 brd ff:ff:ff:ff:ff:ff 3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:50:56:b3:68:a0 brd ff:ff:ff:ff:ff:ff inet 172.31.24.200/24 brd 172.31.24.255 scope global eth0 inet 172.31.24.197/32 scope global eth0 //VIP已经在备机上启动 inet6 fe80::250:56ff:feb3:68a0/64 scope link valid_lft forever preferred_lft forever
注:
keepalived的日志在/var/log/messages,可以通过检查日志来发现问题。
Linux架构之LVS-DR和keepalived配置