Linux负载均衡解决方案 -- LVS-DR 模式 案例实战

LVS-DR 模式 案例实战

  • 一、环境准备
  • 二、案例实战
  • 三、测试

一、环境准备

架构设计
Linux负载均衡解决方案 -- LVS-DR 模式 案例实战_第1张图片

环境配置

硬件配置

操作系统:centos-7.8 cpu:1*1 硬盘:20G 内存:2G

网络配置

role host ip VIP OS
Client node05 192.168.5.5 ---- Centos-7.8
Director-Server node01 192.168.5.4 192.168.5.100 Centos-7.8
Web1 node02 192.168.5.11 192.168.5.100 Centos-7.8
Web2 node03 192.168.5.12 192.168.5.100 Centos-7.8
Web3 node04 192.168.5.13 192.168.5.100 Centos-7.8

二、案例实战

Director-Server 编写脚本

#!/bin/sh
# 
# Startup script handle the initialisation of LVS
# chkconfig: - 28 72
# description: Initialise the Linux Virtual Server for DR
# 
### BEGIN INIT INFO
# Provides: ipvsadm
# Required-Start: $local_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Short-Description: Initialise the Linux Virtual Server
# Description: The Linux Virtual Server is a highly scalable and highly
#   available server built on a cluster of real servers, with the load
#   balancer running on Linux.
# description: start LVS of DR
LOCK=/var/lock/ipvsadm.lock
VIP=192.168.5.100
RIP1=192.168.5.11
RIP2=192.168.5.12
RIP3=192.168.5.13
DipName=ens33

. /etc/rc.d/init.d/functions
start() {
		PID=`ipvsadm -Ln | grep ${VIP} | wc -l`
		if  [ $PID -gt 0 ];
		then
		    echo "The LVS-DR Server is already running !"
		else
            #Set the Virtual IP Address
            /sbin/ifconfig ${DipName}:100 $VIP broadcast $VIP netmask 255.255.255.255 up
            /sbin/route add -host $VIP dev ${DipName}:100
            #Clear IPVS Table
            /sbin/ipvsadm -C
            #Set Lvs
            /sbin/ipvsadm -At $VIP:80 -s rr
            /sbin/ipvsadm -at $VIP:80 -r $RIP1:80 -g
            /sbin/ipvsadm -at $VIP:80 -r $RIP2:80 -g
            /sbin/ipvsadm -at $VIP:80 -r $RIP3:80 -g
            /bin/touch $LOCK
            #Run Lvs
            echo "starting LVS-DR Server is ok !"
        fi
} 
stop()      {

            #clear Lvs and vip
            /sbin/ipvsadm -C
            /sbin/route del -host $VIP dev ${DipName}:100
            /sbin/ifconfig ${DipName}:100 down >/dev/null
            rm -rf $LOCK
            echo "stopping LVS-DR server is ok !"
} 
status() {
       if [ -e $LOCK ];
       then
            echo "The LVS-DR Server is already running !"
       else
            echo "The LVS-DR Server is not running !"
       fi
} 
case "$1" in
  start)
       start
       ;;
  stop)
       stop
       ;;
  restart)
       stop
       start
       ;;
  status)
       status
       ;;
*)
       echo "Usage: $1 {start|stop|restart|status}"
       exit 1
esac
exit 0



# 添加脚本到系统服务,并启动服务
[root@node04 ~]# chmod +x /etc/init.d/lvs 
[root@node04 ~]# chkconfig --add lvs
[root@node04 ~]# chkconfig lvs on
[root@node04 ~]# systemctl start lvs

查看lvs集群
Linux负载均衡解决方案 -- LVS-DR 模式 案例实战_第2张图片

Web1、Web2、Web3分别配置脚本

# 提供脚本,以Web1为例,其他节点一样
[root@node01 ~]# vim  /etc/init.d/lvs_rs 
#!/bin/sh
# 
# Startup script handle the initialisation of LVS
# chkconfig: - 28 72
# description: Initialise the Linux Virtual Server for DR
# 
### BEGIN INIT INFO
# Provides: ipvsadm
# Required-Start: $local_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Short-Description: Initialise the Linux Virtual Server
# Description: The Linux Virtual Server is a highly scalable and highly
#   available server built on a cluster of real servers, with the load
#   balancer running on Linux.
# description: start LVS of DR-RIP
LOCK=/var/lock/ipvsadm.lock
VIP=192.168.5.100
. /etc/rc.d/init.d/functions
start() {
	 PID=`ifconfig | grep lo:100 | wc -l`
	 if [ $PID -ne 0 ];
	 then
	     echo "The LVS-DR-RIP Server is already running !"
	 else
	    /sbin/ifconfig lo:100 $VIP netmask 255.255.255.255 broadcast $VIP up
	    /sbin/route add -host $VIP dev lo:100
	    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/ens33/arp_ignore
	    echo "2" >/proc/sys/net/ipv4/conf/ens33/arp_announce
	    echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
	    echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
	    /bin/touch $LOCK
	    echo "starting LVS-DR-RIP server is ok !"
	 fi
	 } 
	 
stop() {
       /sbin/route del -host $VIP dev lo:100
       /sbin/ifconfig lo:100 down >/dev/null
       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
       echo "0" >/proc/sys/net/ipv4/conf/ens33/arp_ignore
       echo "0" >/proc/sys/net/ipv4/conf/ens33/arp_announce
       echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
       echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
       rm -rf $LOCK
       echo "stopping LVS-DR-RIP server is ok !"
}

status() {
     if [ -e $LOCK ];
     then
        echo "The LVS-DR-RIP Server is already running !"
     else
        echo "The LVS-DR-RIP Server is not running !"
     fi
} 

case "$1" in
   start)
        start
        ;;
   stop)
        stop
        ;;
   restart)
        stop
        start
        ;;
   status)
        status
        ;;
   *)
        echo "Usage: $1 {start|stop|restart|status}"
        exit 1
esac
exit 0


# 添加脚本到系统服务,并启动服务
[root@node01 ~]# chmod +x /etc/init.d/lvs_rs 
[root@node01 ~]# chkconfig --add  lvs_rs
[root@node01 ~]# chkconfig lvs_rs on
[root@node01 ~]# systemctl start lvs_rs

查看VIP
Linux负载均衡解决方案 -- LVS-DR 模式 案例实战_第3张图片
查看路由

[root@node01 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.5.2     0.0.0.0         UG    100    0        0 ens33
192.168.5.0     0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.5.100   0.0.0.0         255.255.255.255 UH    0      0        0 lo
[root@node01 ~]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.5.2     0.0.0.0         UG        0 0          0 ens33
192.168.5.0     0.0.0.0         255.255.255.0   U         0 0          0 ens33
192.168.5.100   0.0.0.0         255.255.255.255 UH        0 0          0 lo
[root@node01 ~]# ip route
default via 192.168.5.2 dev ens33 proto static metric 100 
192.168.5.0/24 dev ens33 proto kernel scope link src 192.168.5.11 metric 100 
192.168.5.100 dev lo scope link src 192.168.5.100

三、测试

实现负载均衡!!!
Linux负载均衡解决方案 -- LVS-DR 模式 案例实战_第4张图片

你可能感兴趣的:(Web,Server,Clusters,Web集群技术,LVS-DR,模式,案例实战,运维)