web集群负载均衡组:基于CentOS 7构建 LVS-DR 群集并配置服务启动脚本

目录

    • 1、准备环境
    • 2、配置lvs集群

配置LVS-DR模式主要注意的有
1、vip绑定在RS的lo接口;
2、RS做arp抑制;

1、准备环境

准备web访问环境,需要两台web服务器作为RS,配置RIP
准备第一台web服务器ip地址为:192.168.142.130

[root@node2 ~]# yum install nginx
[root@node2 ~]# cd /usr/share/nginx/html/
[root@node2 html]# echo "web test page,`hostname -I`." > index.html 
[root@node2 html]# systemctl restart nginx

准备第二台web服务器ip地址为:192.168.142.131

[root@node3 ~]# yum install nginx
[root@node3 ~]# cd /usr/share/nginx/html/
[root@node3 html]# echo "web test page,`hostname -I`." > index.html 
[root@node3 html]# systemctl restart nginx

2、配置lvs集群

1、在RS上配置服务启动脚本
配置lvs_rs的服务启动脚本
注意:使用此脚本需要修改VIP地址,以及个人的网卡信息如 ens33

[root@node3 ~]# 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.142.90
. /etc/rc.d/init.d/functions
start() {
     PID=`ifconfig | grep lo:10 | wc -l`
     if [ $PID -ne 0 ];
     then
         echo "The LVS-DR-RIP Server is already running !"
     else
         /sbin/ifconfig lo:10 $VIP netmask 255.255.255.255 broadcast $VIP up
         /sbin/route add -host $VIP dev lo:10
         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:10
         /sbin/ifconfig lo:10 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

开启lvs_rs的自启动

[root@node3 ~]# chmod +x /etc/init.d/lvs_rs 
[root@node3 ~]# chkconfig --add lvs_rs
[root@node3 ~]# service lvs_rs start 

将配置好的脚本发送给另一台web服务器

[root@node3 ~]# scp /etc/init.d/lvs_rs 192.168.142.130:/etc/init.d/

脚本启动成功可以查看两台web服务器的网卡绑定信息
web集群负载均衡组:基于CentOS 7构建 LVS-DR 群集并配置服务启动脚本_第1张图片
web集群负载均衡组:基于CentOS 7构建 LVS-DR 群集并配置服务启动脚本_第2张图片

先查看本地的IP地址信息
web集群负载均衡组:基于CentOS 7构建 LVS-DR 群集并配置服务启动脚本_第3张图片
安装服务

[root@node1 ~]# yum install ipvsadm -y

2、配置LVS虚拟IP(VIP)
#这里采用子接口配置

[root@node1 ~]# ifconfig ens33:10 192.168.142.90 netmask 255.255.255.0 up  #这里采用子接口配置

再次查看IP地址信息
web集群负载均衡组:基于CentOS 7构建 LVS-DR 群集并配置服务启动脚本_第4张图片

[相关参数说明]
[root@Directory ~]# ipvsadm –help
-A 添加虚拟服务器
-t 设置群集地址(VIP,Virtual IP)
-s 指定负载调度算法
-a 添加真实服务器
-d 删除真实服务器
-r 指定真实服务器(Real Server)的地址
-m 使用NAT模式;-g、-i分别对应DR、TUN模式
-w 为节点服务器设置权重,默认为1

3)手工执行配置添加LVS服务并增加两台RS
web集群负载均衡组:基于CentOS 7构建 LVS-DR 群集并配置服务启动脚本_第5张图片
配置成功后可以在另外一天设备上访问VIP
web集群负载均衡组:基于CentOS 7构建 LVS-DR 群集并配置服务启动脚本_第6张图片
3、可以配置LVS的服务启动脚本
可以不用逐一添加ipvsadm

[root@node1 ~]# vim /etc/init.d/lvs_dr 
# 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.142.90
RIP1=192.168.142.130
RIP2=192.168.142.131
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}:10 $VIP broadcast $VIP netmask 255.255.255.255 up
           /sbin/route add -host $VIP dev ${DipName}:10
           #Clear IPVS Table
           /sbin/ipvsadm -C
           #Set Lvs
           /sbin/ipvsadm -At $VIP:80 -s wrr
           /sbin/ipvsadm -at $VIP:80 -r $RIP1:80 -g -w 2
           /sbin/ipvsadm -at $VIP:80 -r $RIP2:80 -g -w 1
           /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}:10
           /sbin/ifconfig ${DipName}:10 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

脚本内设置为加权轮询,可以查看ipvsadm
web集群负载均衡组:基于CentOS 7构建 LVS-DR 群集并配置服务启动脚本_第7张图片
通过再次访问VIP可以看出
web集群负载均衡组:基于CentOS 7构建 LVS-DR 群集并配置服务启动脚本_第8张图片

你可能感兴趣的:(web集群,负载均衡,centos,linux,服务器,nginx)