Lvs-dr:Direct Routing,被称为直接路由;通过修改请求报文的MAC地址来进行转发;源MAC地址是DIP所在接口的MAC地址,目标MAC时前端主机挑选出某台后端的RS的RIP所在接口的MAC地址;从新封装时IP地址地址不会发生变化;下图为lvs-dr结构图
1、当客户端请求资源时,路由1会把请求报文发送给前传主机;在发送给前端主机时路由1不会变更请求报文的IP守护,而是在报文上再添加一层MAC守护
2、当前端主机收到请求报文后,拆封原来的MAC守护;自己再添加一层MAC守护发送给后端主机
3、当后端主机的网卡eth1收到请求报文之后,发现本机的lo网卡上具有报文守护中的VIP地址;随后再将请求发送 给lo
4、lo收到请求之后,处理请求,构建响应报文;随后在构建好的响应报文之后增加一层IP守护;经由eth1转发给路由2;最后再经由路由2发送个给客户端
相关特点:
(1)、请求报文必须通过前端主机,响应报文不能通过前端主机
(2)、每个后端主机需要修改内核参数
(3)、前端主机和后端主机必须在同一个物理网络,同时后端主机的网关不能指向前端主机
keepalived:vrrp协议是HA高可用集群的实现方式;而keepalived是vrrp协议的实现方式
keepalived是基于vrrp协议实现的一个高可用集群解决方案,可以利用keepalived来解决单点故障问题,使用keepalived实现的高可用集群中,一般有两个服务器,一个主服务器(MASTER)、一个备服务器(BACKUP);主服务器会定时发送特殊消息给备服务器,证明自己是完好的、正常的提供服务;当备服务器接受不到主服务器发送的消息时,此时备服务器会直接夺取主服务器的IP来提供服务
实验准备:
虚拟机1:172.18.42.100;作为主节点
虚拟机2:172.18.42.200;作为备节点
虚拟机3:172.18.42.111;作为Web Server 1提供web服务
虚拟机4:172.18.42.222;作为Web Server 2提供web服务
lo:1:172.18.42.42;作为RS的VIP
一、在虚拟机1上安装keepalived服务;随后编辑其配置文件
[[email protected] ~]# yum install keepalived -y ##yum安装keepalived [[email protected] ~]# vim /etc/keepalived/keepalived.conf ##编辑配置文件 ! Configuration File for keepalived global_defs { ##定义全局配置段 notification_email { ##通知邮件发送给谁 [email protected] ##发送给本机 } notification_email_from ##指明邮件发件人是谁 smtp_server 127.0.0.1 ##发件人地址 smtp_connect_timeout 30 ##发送邮件的超时时长 router_id wtc ##路由设备的标识号 vrrp_mcast_group4 244.0.42.20 ##vrrp协议通过ipv4来组播通知 } vrrp_instance VI_1 { ##定义虚拟路由配置 state MASTER ##指明当前节点此虚拟路由的初始状态 interface eth0 ##指明绑定那个网卡接口来实现组播(vrrp工作绑定的接口) virtual_router_id 110 ##虚拟路由ID号,有效范围时8位二进制:0-255 priority 100 ##指明当前虚拟路由的nice值(优先级) advert_int 1 ##vrrp通告的时间间隔;默认为1s authentication { ##认证机制 auth_type PASS ##简单认证方式 auth_pass lm97gg51 ##密码 } virtual_ipaddress { ##指定虚拟IP 172.18.42.42 dev eth0 label eth0:1 ##使用网卡接口别名 } } virtual_server 172.18.42.42 80 { ##定义虚拟路由配置 delay_loop 6 ##指明服务轮询时间间隔 lb_algo rr ##指明负载均衡的调度方法 lb_kind DR ##指明集群的类型 protocol TCP ##指明服务协议 real_server 172.18.42.111 80 { ##指明Web主机1的IP地址及端口 weight 1 ##指明权重; HTTP_GET { ##获取HTTP服务; url { path / ##健康状态检测时请求资源的URL status_code 200 ##基于获取的内容进行健康状态判定 } connect_timeout 3 ##连接的超时时长 nb_get_retry 3 ##尝试次数 delay_before_retry 3 ##两次尝试之间的时间间隔 } } real_server 172.18.42.222 80 { ##指明Web主机2的地址及端口 weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } }
在虚拟机2上的keepalived配置文件只需要把state改成BACKUP、priority改成比100小的数就行了
二、测试是否实现高可用
[[email protected] keepalived]# ifconfig ##虚拟机1 eth0:1 Link encap:Ethernet HWaddr 00:0C:29:B1:AB:69 inet addr:172.18.42.42 Bcast:0.0.0.0 Mask:255.255.255.255 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 ##说明主节点上的VIP已经OK [[email protected] keepalived]# ifconfig ##虚拟机2 eth0 Link encap:Ethernet HWaddr 00:0C:29:40:4A:7F inet addr:172.18.42.200 Bcast:172.18.255.255 Mask:255.255.0.0 inet6 addr: fe80::20c:29ff:fe40:4a7f/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:80542 errors:0 dropped:0 overruns:0 frame:0 TX packets:11417 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:11251698 (10.7 MiB) TX bytes:1622540 (1.5 MiB) ##备节点上的优先级没有主节点上的高;所以不会抢占VIP [[email protected] keepalived]# service keepalived stop ##关闭虚拟机1上的keepalived服务 Stopping keepalived: [ OK ] [[email protected] keepalived]# ifconfig ##主节点上的VIP没了 eth0 Link encap:Ethernet HWaddr 00:0C:29:B1:AB:69 inet addr:172.18.42.100 Bcast:172.18.255.255 Mask:255.255.0.0 inet6 addr: fe80::20c:29ff:feb1:ab69/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:19877 errors:0 dropped:0 overruns:0 frame:0 TX packets:3082 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:9751959 (9.3 MiB) TX bytes:299076 (292.0 KiB) [[email protected] keepalived]# ifconfig ##虚拟机2 eth0 Link encap:Ethernet HWaddr 00:0C:29:40:4A:7F inet addr:172.18.42.200 Bcast:172.18.255.255 Mask:255.255.0.0 inet6 addr: fe80::20c:29ff:fe40:4a7f/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:81871 errors:0 dropped:0 overruns:0 frame:0 TX packets:11648 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:11389486 (10.8 MiB) TX bytes:1653638 (1.5 MiB) eth0:1 Link encap:Ethernet HWaddr 00:0C:29:40:4A:7F inet addr:172.18.42.42 Bcast:0.0.0.0 Mask:255.255.255.255 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 ##备节点抢占了主节点的VIP [[email protected] keepalived]# service keepalived start ##开启虚拟机1的keepalived服务 Starting keepalived: [ OK ] [[email protected] keepalived]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:B1:AB:69 inet addr:172.18.42.100 Bcast:172.18.255.255 Mask:255.255.0.0 inet6 addr: fe80::20c:29ff:feb1:ab69/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:20864 errors:0 dropped:0 overruns:0 frame:0 TX packets:3313 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:9878452 (9.4 MiB) TX bytes:315410 (308.0 KiB) eth0:1 Link encap:Ethernet HWaddr 00:0C:29:B1:AB:69 inet addr:172.18.42.42 Bcast:0.0.0.0 Mask:255.255.255.255 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 ##主节点把VIP抢过来了 [[email protected] keepalived]# ifconfig ##虚拟机2 eth0 Link encap:Ethernet HWaddr 00:0C:29:40:4A:7F inet addr:172.18.42.200 Bcast:172.18.255.255 Mask:255.255.0.0 inet6 addr: fe80::20c:29ff:fe40:4a7f/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:83291 errors:0 dropped:0 overruns:0 frame:0 TX packets:12242 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:11562232 (11.0 MiB) TX bytes:1721278 (1.6 MiB) ##备节点上的VIP没有了
三、设置虚拟机3Web主机的内核参数
[[email protected] ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore ##仅在请求的目标IP在本地主机的某个接口上时,才给予响应 [[email protected] ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore [[email protected] ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce ##禁止本机向其他网络接口通告自己的网卡接口信息 [[email protected] ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce [[email protected] ~]# ifconfig lo:1 172.18.42.42 netmask 255.255.255.255 broadcast 172.18.42.42 ##添加RS上的VIP; [[email protected] ~]# route add -host 172.18.42.42 lo:1 ##指明发送响应报文还是经由此网卡接口
虚拟机4的设置方法相同
四、测试是否实现负载均衡
[[email protected] keepalived]# curl http://172.18.42.42 <h1> 172.18.42.111 Web Server 1 </h1> [[email protected] keepalived]# curl http://172.18.42.42 <h1> 172.18.42.222 Web Server 2 </h1> [[email protected] keepalived]# curl http://172.18.42.42 <h1> 172.18.42.111 Web Server 1 </h1> [[email protected] keepalived]# curl http://172.18.42.42 <h1> 172.18.42.222 Web Server 2 </h1> [[email protected] keepalived]# curl http://172.18.42.42 <h1> 172.18.42.111 Web Server 1 </h1> [[email protected] keepalived]# curl http://172.18.42.42 <h1> 172.18.42.222 Web Server 2 </h1>
五、测试是否对后端主机高可用
[[email protected] keepalived]# ipvsadm -Ln ##关闭虚拟机3的Web服务 IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 172.18.42.42:80 rr -> 172.18.42.222:80 ##自动删除虚拟机3的Web服务 0 [[email protected] keepalived]# curl http://172.18.42.42 <h1> 172.18.42.222 Web Server 2 </h1> [[email protected] keepalived]# curl http://172.18.42.42 <h1> 172.18.42.222 Web Server 2 ##响应请求的只有Web2了 </h1> [[email protected] keepalived]# ipvsadm -Ln ##开启虚拟机3的Web服务 IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 172.18.42.42:80 rr -> 172.18.42.111:80 ##自动添加虚拟机3的Web服务 -> 172.18.42.222:80 Route 1 0 [[email protected] keepalived]# curl http://172.18.42.42 <h1> 172.18.42.222 Web Server 2 </h1> [[email protected] keepalived]# curl http://172.18.42.42 <h1> 172.18.42.111 Web Server 1 </h1>
注意问题:
(1)各个节点之间的时间必须是要同步的;可基于ntpdate实现
(2)要确保内核参数arp_ignore不能对外响应、arp_announce不能通告