Director根据负载均衡算法选择 Realserver_1,不修改也不封装IP报文,而是将数据帧的MAC地址改为RealServer_1的MAC地址,然后在局域网上发送。IP包头及数据帧头信息如下
- RealServer_1收到这个帧,解封装后发现目标IP与本机匹配( RealServer事先绑定了VIP),于是处理这个报文。随后重新封装报文,发送到局域网。此时IP包头及数据帧头信息为
Client将收到回复报文。 Client认为得到正常的服务,而不会知道是哪一台服务器处理的
注意:如果跨网段,那么报文通过路由器经由 terne返回给用户
在局域网中具有相同的IP地址,势必会造成各服务器ARP通信的紊乱
对节点服务器进行处理,使其不响应针对VIP的ARP请求
发送ARP请求时, Linux默认使用IP包的源IP地址(即VIP)作为ARP请求包中的源IP地址,而不使用发送接口(例如ens33)的IP地址
路由器收到ARP请求后,将更新ARP表项
此时新来的请求报文,路由器根据ARP表项,会将该报文转发给RealServer,从而导致 Director的VIP失效
对节点服务器进行处理,设置内核参数arp_announce=2:系统不使用IP包的源地址来设置ARP请求的源地址,而选择发送接口的IP地址
[root@lvs1 ~]# yum install keepalived ipvsadm -y '//keepalived:双机热备要用到的,ipvsadm:调度管理要用'
[root@lvs2 ~]# yum install keepalived ipvsadm -y
[root@lvs1 ~]# vim /etc/sysctl.conf
'//尾行插入下段配置'
net.ipv4.ip_forward = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
[root@lvs1 ~]# sysctl -p '//重载配置,使之生效'
net.ipv4.ip_forward = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
[root@lvs1 network-scripts]# vim /etc/init.d/dr.sh
'//编辑以下脚本内容'
#!/bin/bash
GW=192.168.100.1
VIP=192.168.100.10 ##虚拟ip
RIP1=192.168.100.200 ##真实web服务器ip
RIP2=192.168.100.222
case "$1" in
start)
/sbin/ipvsadm --save > /etc/sysconfig/ipvsadm ##保存配置
systemctl start ipvsadm ##启动服务
/sbin/ifconfig ens33:0 $VIP broadcast $VIP netmask 255.255.255.255 broadcast $VIP up
##设置ens33:0地址,广播地址,子网掩码,并开启
/sbin/route add -host $VIP dev ens33:0 ##添加路由网段信息
/sbin/ipvsadm -A -t $VIP:80 -s rr ##指定虚拟服务访问入口,指定轮询算法
/sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g ##指定真实服务器,dr模式
/sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g
echo "ipvsadm starting --------------------[ok]"
;;
stop)
/sbin/ipvsadm -C ##清空缓存
systemctl stop ipvsadm ##关闭服务
ifconfig ens33:0 down ##关闭接口
route del $VIP ##删除路由信息
echo "ipvsamd stoped----------------------[ok]"
;;
status)
if [ ! -e /var/lock/subsys/ipvsadm ];then ##判断文件存在与否决定状态
echo "ipvsadm stoped---------------"
exit 1
else
echo "ipvsamd Runing ---------[ok]"
fi
;;
*)
echo "Usage: $0 {
start|stop|status}"
exit 1
esac
exit 0
[root@lvs1 network-scripts]# chmod +x /etc/init.d/dr.sh
[root@lvs1 ~]# cd /etc/sysconfig/network-scripts/
[root@lvs1 network-scripts]# cp -p ifcfg-ens33 ifcfg-ens33:0
[root@lvs1 network-scripts]# vim ifcfg-ens33:0 '//配置虚拟ip地址'
'//删除原本内容,添加以下配置'
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.100.10
NETMASK=255.255.255.0
[root@lvs1 network-scripts]# vim ifcfg-ens33
'//...省略内容,修改为static'
BOOTPROTO=static
'//...省略内容,尾行添加内容'
IPADDR=192.168.100.100
NETMASK=255.255.255.0
GATEWAY=192.168.100.1
[root@lvs2 network-scripts]# vim ifcfg-ens33
'//注意lvs2的IP地址为192.168.100.110'
IPADDR=192.168.100.110
[root@lvs1 network-scripts]# systemctl restart network
[root@lvs1 network-scripts]# ifup ens33:0
[root@lvs1 network-scripts]# service dr.sh start
ipvsadm starting --------------------[ok]
[root@lvs1 network-scripts]# systemctl stop firewalld.service
[root@lvs1 network-scripts]# setenforce 0
LVS-DR中,两台节点服务器要配置绑定VIP地址
VIP地址仅用作Web响应数据包的源地址,并不监听客户机的访问请求
都修改为仅主机模式
[root@web1 ~]# cd /etc/sysconfig/network-scripts/
[root@web1 network-scripts]# cp -p ifcfg-lo ifcfg-lo:0
[root@web1 network-scripts]# vim ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.100.10
NETMASK=255.255.255.0
ONBOOT=yes
[root@web1 network-scripts]# vim ifcfg-ens33
'//...省略内容,修改为static'
BOOTPROTO=static
'//...省略内容,尾行添加内容'
IPADDR=192.168.100.200
NETMASK=255.255.255.0
GATEWAY=192.168.100.1
'//其中web2服务器修改为192.168.100.222'
IPADDR=192.168.100.222
[root@lvs1 network-scripts]# systemctl restart network
[root@lvs1 network-scripts]# ifup ens33:0
[root@lvs1 network-scripts]# service dr.sh start
ipvsadm starting --------------------[ok]
[root@lvs1 network-scripts]# systemctl stop firewalld.service
[root@lvs1 network-scripts]# setenforce 0
LVS-DR中,两台节点服务器要配置绑定VIP地址
VIP地址仅用作Web响应数据包的源地址,并不监听客户机的访问请求
两台节点服务器为ens33配置IP地址,为lo:0配置VIP地址
[root@web1 ~]# cd /etc/sysconfig/network-scripts/
[root@web1 network-scripts]# cp -p ifcfg-lo ifcfg-lo:0
[root@web1 network-scripts]# vim ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.100.10
NETMASK=255.255.255.0
ONBOOT=yes
[root@web1 network-scripts]# vim ifcfg-ens33
'//...省略内容,修改为static'
BOOTPROTO=static
'//...省略内容,尾行添加内容'
IPADDR=192.168.100.200
NETMASK=255.255.255.0
GATEWAY=192.168.100.1
'//其中web2服务器修改为192.168.100.222'
IPADDR=192.168.100.222
配置抑制ARP脚本
[root@web1 network-scripts]# vim /etc/init.d/web.sh
#!/bin/bash
VIP=192.168.100.10
case "$1" in
start)
ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/sbin/route add -host $VIP dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore '//arp忽略'
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
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK "
;;
stop)
ifconfig lo:0 down
route del $VIP /dev/null 2>&1
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore '//arp开启'
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Stopd"
;;
*)
echo "Usage: $0 {
start|stop}"
exit 1
esac
exit 0
[root@web1 network-scripts]# chmod +x /etc/init.d/web.sh
[root@web1 ~]# yum install httpd -y
[root@web2 ~]# yum install httpd -y
[root@web1 ~]# vim /var/www/html/index.html
this is web1 web
[root@web2 ~]# vim /var/www/html/index.html
this is web2 web
[root@web1 network-scripts]# systemctl restart network
[root@web1 network-scripts]# ifup lo:0
[root@web1 network-scripts]# service web.sh start
RealServer Start OK
[root@web1 network-scripts]# systemctl start httpd
[root@web1 network-scripts]# systemctl stop firewalld.service
[root@web1 network-scripts]# setenforce 0
Keepalived采用VRRP热备份协议实现Linux服务器的多机热备功能
VRRP,虚拟路由冗余协议,是针对路由器的一种备份解决方案
由多台路由器组成一个热备组,通过共用的虚拟IP地址对外提供服务
每个热备组内同一时刻只有一台主路由器提供服务,其他路由器处于冗余状态
若当前在线的路由器失效,则其他路由器会根据设置的优先级自动接替虚拟IP地址,继续提供服务
[root@lvs1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1 '//邮件协议指向自己'
smtp_connect_timeout 30
router_id LVS_01 '//router_id不能相同'
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER '//主服务器为MASTER,备服务器为BACKUP'
interface eth0
virtual_router_id 10 '//主备组号要相同'
priority 100 '//优先级,备服务器的优先级要小于主'
advert_int 1
authentication {
auth_type PASS
auth_pass 1111 '//主备密码要相同'
}
virtual_ipaddress {
192.168.100.10
}
}
virtual_server 192.168.100.10 80 {
delay_loop 6
lb_algo rr '//轮询算法'
lb_kind DR '//修改为DR模式'
persistence_timeout 50
protocol TCP
real_server 192.168.100.200 80 {
'//节点1配置'
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.100.222 80 {
'//节点2配置'
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@lvs1 ~]# vim /etc/keepalived/keepalived.conf
...省略内容
router_id LVS_02 '//router_id不能相同'
...省略内容
vrrp_instance VI_1 {
state BACKUP '//此处选择为BACKUP备服务器'
interface eth0
virtual_router_id 10
priority 90 '//优先级需要低于主服务器'
...省略内容
'//其他配置都相同'
[root@lvs1 ~]# systemctl start keepalived
[root@lvs2 ~]# systemctl start keepalived