zllbirdonland 2019-12-20
为方便进行原理分析,将Client与群集机器放在同一网络中,数据包流经的路线为1-2-3-4
VRRP,虚拟路由冗余协议,是针对路由器的一-种备份解决方案
~]# yum install keepalived ipvsadm -y [ ~]# vim /etc/sysctl.conf //配置路由转发功能 net.ipv4.ip_forward=1 //路由功能 net.ipv4.conf.all.send_redirects = 0 net.ipv4.conf.default.send_redirects = 0 //proc响应关闭重定向功能 net.ipv4.conf.ens33.send_redirects = 0 [ ~]# sysctl -p //加载生效 [ ~]# cd /etc/sysconfig/network-scripts/ [ network-scripts]# cp -p ifcfg-ens33 ifcfg-ens33:0 //复制虚拟网卡的配置文件 [ network-scripts]# vim ifcfg-ens33:0 DEVICE=ens33:0 //添加配置文件信息,原有的信息删除 ONBOOT=yes IPADDR=192.168.100.10 NETMASK=255.255.255.0 [ network-scripts]# cd /etc/init.d/ //创建脚本便于service使用 [ init.d]# vim 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 [ init.d]# chmod +x dr.sh //给执行权限
[ init.d]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 //修改网卡信息 BOOTPROTO=static IPADDR=192.168.100.110 NETMASK=255.255.255.0 GATEWAY=192.168.100.1 [ init.d]# service network restart //重启网络服务 [ init.d]# ifup ens33:0 //开启虚拟网卡 [ init.d]# service dr.sh start //开启lvs服务 ipvsadm starting --------------------[ok] [ init.d]# systemctl stop firewalld.service //关闭防火墙 [ init.d]# setenforce 0
[ ~]# yum install keepalived ipvsadm -y [ ~]# vim /etc/sysctl.conf //配置路由转发功能 net.ipv4.ip_forward=1 //路由功能 net.ipv4.conf.all.send_redirects = 0 net.ipv4.conf.default.send_redirects = 0 //proc响应关闭重定向功能 net.ipv4.conf.ens33.send_redirects = 0 [ ~]# sysctl -p //加载生效 [ ~]# cd /etc/sysconfig/network-scripts/ [ network-scripts]# cp -p ifcfg-ens33 ifcfg-ens33:0 //复制虚拟网卡的配置文件 [ network-scripts]# vim ifcfg-ens33:0 DEVICE=ens33:0 //添加配置文件信息,原有的信息删除 ONBOOT=yes IPADDR=192.168.100.10 NETMASK=255.255.255.0 [ network-scripts]# cd /etc/init.d/ //创建脚本便于service使用 [ init.d]# vim 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 [ init.d]# chmod +x dr.sh //给执行权限 [ init.d]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 //修改网卡信息,切换为仅主机模式 BOOTPROTO=static IPADDR=192.168.100.111 NETMASK=255.255.255.0 GATEWAY=192.168.100.1 [ init.d]# service network restart //重启网络服务 [ init.d]# ifup ens33:0 //开启虚拟网卡 [ init.d]# service dr.sh start //开启lvs服务 ipvsadm starting --------------------[ok] [ init.d]# systemctl stop firewalld.service ##关闭防火墙 [ init.d]# setenforce 0
[ ~]# yum install httpd -y //安装web服务 [ ~]# systemctl stop firewalld.service //关闭防火墙 [ ~]# setenforce 0 [ ~]# echo "this is kgc web" > /var/www/html/index.html //创建网站页面 [ ~]# cd /etc/sysconfig/network-scripts/ [ network-scripts]# cp -p ifcfg-lo ifcfg-lo:0 //修改回环网卡的虚拟网卡 [ network-scripts]# vim ifcfg-lo:0 DEVICE=lo:0 IPADDR=192.168.100.10 NETMASK=255.255.255.0 ONBOOT=yes [ network-scripts]# cd /etc/init.d/ [ init.d]# vim web.sh //编写arp脚本 #!/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 [ init.d]# chmod +x web.sh //给权限
[ init.d]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 //修改网卡,仅主机模式 BOOTPROTO=static IPADDR=192.168.100.200 NETMASK=255.255.255.0 GATEWAY=192.168.100.1 [ init.d]# service network restart //重启网络服务 [ init.d]# service web.sh start //启动脚本 RealServer Start OK [ init.d]# systemctl start httpd.service //开启web服务 [ init.d]# ifup lo:0 //开启回环虚拟网卡
[ ~]# yum install httpd -y //安装web服务 [ ~]# systemctl stop firewalld.service //关闭防火墙 [ ~]# setenforce 0 [ ~]# echo "this is accp web" > /var/www/html/index.html //创建网站页面 [ ~]# cd /etc/sysconfig/network-scripts/ [ network-scripts]# cp -p ifcfg-lo ifcfg-lo:0 //修改回环网卡的虚拟网卡 [ network-scripts]# vim ifcfg-lo:0 DEVICE=lo:0 IPADDR=192.168.100.10 NETMASK=255.255.255.0 ONBOOT=yes [ network-scripts]# cd /etc/init.d/ [ init.d]# vim web.sh //编写arp脚本 #!/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 [ init.d]# chmod +x web.sh //给权限
[ init.d]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 //修改网卡,仅主机模式 BOOTPROTO=static IPADDR=192.168.80.222 NETMASK=255.255.255.0 GATEWAY=192.168.80.1 []# service network restart //重启网络服务 []# service web.sh start //启动脚本 RealServer Start OK [ init.d]# systemctl start httpd.service //开启web服务 [ init.d]# ifup lo:0 //开启回环虚拟网卡
[ init.d]# vim /etc/keepalived/keepalived.conf global_defs { ... smtp_server 127.0.0.1 //指向本地 router_id LVS_01 //指定名称,备份服务器不同名称 ... } vrrp_instance VI_1 { state MASTER //备份服务器是BACKUP virtual_router_id 10 //组号相同 ... auth_pass abc123 //验证密码 priority 100 //优先级备份小于主服务器 ... virtual_ipaddress { 192.168.100.10 } ... virtual_server 192.168.100.10 80 { ... real_server 192.168.100.200 80 { 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 { weight 1 TCP_CHECK { //tcp connect_port 80 //添加端口 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } } [ init.d]# systemctl start keepalived.service [ init.d]# scp /etc/keepalived/keepalived.conf :/etc/keepalived/ //复制到lvs2服务器上
[ init.d]# vim /etc/keepalived/keepalived.conf ##修改keepalive配置文件 router_id LVS_02 ##router_id不能相同 ... state BACKUP ##备服务器 ... priority 90 ##优先级低于主服务器 [ init.d]# systemctl start keepalived.service ##启动keepalive服务