Linux---LVS负载均衡---DR模式

Linux—LVS负载均衡—DR模式

一.工作模式

直接路由:简称DR模式,采用半开放式的网络结构,与TUN模式的结构类似,但各节点并不是分散在各地,而是与调度器位于同一个物理网络。负载调度器与各个节点服务器通过本地网络连接,不需要建立专用的IP隧道

Linux---LVS负载均衡---DR模式_第1张图片

二.构建LVS-DR模式

(1)配置环境:

两台LVS调度器,两台web服务器,一台外部客户端

(2)配置实验 :

1)配置第两台调度器
1.安装ipvsadm
[root@localhost ~]# yum install keepalived ipvsadm -y
2.更改为仅主机模式,修改网卡

Linux---LVS负载均衡---DR模式_第2张图片

Linux---LVS负载均衡---DR模式_第3张图片

3.添加虚拟VIP
[root@localhost network-scripts]# cp -p ifcfg-ens33 ifcfg-ens33:0
[root@localhost network-scripts]# vim ifcfg-ens33:0

DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.100.10
NETMASK=255.255.255.0
4.开启路由转发功能,关闭重定向功能
[root@localhost network-scripts]# vim /etc/sysctl.conf
[root@localhost network-scripts]# 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
5.编写脚本:
cd /etc/init.d/

vim dr.sh

#!/bin/bash
GW=192.168.100.1
VIP=192.168.100.10
RIP1=192.168.100.201
RIP2=192.168.100.202
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
        /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
        /sbin/ipvasdm -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 "ipvsadm stoped -------------[ok]"
        ;;
        status)
        if [ ! -e /var/lock/subsys/ipvsadm ];then
        echo "ipvsadm stoped-----------"
        exit 1
                else
                echo "ipvsadm Runing -------[ok]"
        fi
        ;;
        *)
        echo "Usage: $0 {start|stop|status}"
        exit 1
        esac
        exit 0
6.添加权限:
[root@localhost init.d]# chmod +x dr.sh 
7.开启脚本和服务
[root@localhost init.d]# ifup ens33:0
[root@localhost init.d]# service dr.sh start
ipvsadm starting --------------[ok]
8.关闭防火墙
[root@localhost init.d]# systemctl stop firewalld
[root@localhost init.d]# setenforce 0
2)配置两台节点web
1.安装httpd
yum install httpd -y
2.更改为仅主机模式,修改IP地址

Linux---LVS负载均衡---DR模式_第4张图片

Linux---LVS负载均衡---DR模式_第5张图片

3.添加web信息
[root@localhost ~]# cd /var/www/html
[root@localhost html]# echo "

this is kgc web

" > index.html [root@localhost html]# echo "

this is accp web

" > index.html
4.添加虚拟网卡
[root@localhost html]# cd /etc/sysconfig/network-scripts/

[root@localhost network-scripts]# cp -p ifcfg-lo ifcfg-lo:0

[root@localhost network-scripts]# vim ifcfg-lo:0

DEVICE=lo:0
IPADDR=192.168.100.100
NETMASK=255.255.255.0
ONBOOT=yes
5.编写脚本:
[root@localhost network-scripts]# cd /etc/init.d
[root@localhost init.d]# vim 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
	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
	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
6.添加权限开启服务
[root@localhost init.d]# chmod +x web.sh 
[root@localhost init.d]# ifup lo:0
[root@localhost init.d]# service web.sh start 
RealServer Start OK 
[root@localhost init.d]# systemctl start httpd.service

[root@localhost init.d]# systemctl restart network
7.关闭防火墙
[root@localhost init.d]# systemctl stop firewalld.service 
[root@localhost init.d]# setenforce 0

3)查看两台节点web

Linux---LVS负载均衡---DR模式_第6张图片
Linux---LVS负载均衡---DR模式_第7张图片

你可能感兴趣的:(LINUX)