LVS-DR模式配置脚本

LVS-DR模式配置脚本

  • 实验环境,需要4台虚拟机

    IP 说明
    172.25.254.101 客户端
    172.25.254.102 负载均衡器DS
    172.25.254.103 真实服务器RS
    172.25.254.104 真实服务器RS

    VIP:172.25.254.255/32

  • 系统必须有ipvsadm和ifconfig命令

    复制代码
    dnf install ipvsadm
    dnf install net-tools
  • ipvsadm命令

    • -A 添加虚拟服务器
    • -t 设置群集地址(VIP,Virtual IP)
    • -s 指定负载调度算法
    • -a 添加真实服务器
    • -d 删除真实服务器
    • -r 指定真实服务器(Real Server)的地址
    • -m 使用NAT模式;-g、-i分别对应DR、TUN模式
    • -w 为节点服务器设置权重,默认为1
    • -L 列出当前 IPVS 规则的详细信息,包括虚拟服务和真实服务器的配置。
    • -n 和其他子命令搭配使用,主要功能是让输出结果以数字形式呈现 IP 地址和端口号,而非解析成域名和服务名
  • 在DS上编写LVS-DR服务脚本

    /etc/init.d 目录是存放系统初始化脚本的常用位置,这些脚本用于在系统启动、停止或运行过程中管理服务

    [root@Rocky ~]# vim /etc/init.d/lvs_dr

    #!/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 #定义锁文件的路径,用于标记 LVS 是否正在运行。
    VIP=172.25.254.225 #制定和虚拟IP地址
    RIP1=172.25.254.103 #定义真实服务器的IP地址
    RIP2=192.168.95.104
    DipName=ens160 #指定网络设备名
    . /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 rr /sbin/ipvsadm -at VIP:80 -r RIP1:80 -g /sbin/ipvsadm -at VIP:80 -r RIP2: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}: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

    给服务脚本可执行权限

    [root@Rocky ~]# chmod +x /etc/init.d/lvs_dr

    将指定的服务脚本(lvs_dr)添加到 chkconfig 的管理列表中

    [root@Rocky ~]# chkconfig --add lvs_dr

    设置服务在所有运行级别自动启动

    [root@Rocky ~]# chkconfig lvs_dr on

  • 测试服务脚本lvs_dr

    [root@Rocky ~]# systemctl start lvs_dr
    [root@Rocky ~]# systemctl status lvs_dr
    ● lvs_dr.service - LSB: Initialise the Linux Virtual Server
    Loaded: loaded (/etc/rc.d/init.d/lvs_dr; generated)
    Active: active (exited) since Sat 2025-03-22 16:13:25 CST; 5s ago
    Docs: man:systemd-sysv-generator(8)
    Process: 13295 ExecStart=/etc/rc.d/init.d/lvs_dr start (code=exited, status=0/SUCCESS)

    Mar 22 16:13:25 Rocky lvs_dr[13304]: eui64 (Generic EUI-64)
    Mar 22 16:13:25 Rocky lvs_dr[13304]: <AF>=Address family. Default: inet
    Mar 22 16:13:25 Rocky lvs_dr[13304]: List of possible address families:
    Mar 22 16:13:25 Rocky lvs_dr[13304]: unix (UNIX Domain) inet (DARPA Internet) inet6 (IPv6)
    Mar 22 16:13:25 Rocky lvs_dr[13304]: ax25 (AMPR AX.25) netrom (AMPR NET/ROM) rose (AMPR ROSE)
    Mar 22 16:13:25 Rocky lvs_dr[13304]: ipx (Novell IPX) ddp (Appletalk DDP) ash (Ash)
    Mar 22 16:13:25 Rocky lvs_dr[13304]: x25 (CCITT X.25)
    Mar 22 16:13:25 Rocky lvs_dr[13305]: /etc/rc.d/init.d/lvs_dr: line 30: 255.255.255.255: command not found
    Mar 22 16:13:25 Rocky lvs_dr[13295]: starting LVS-DR Server is ok !
    Mar 22 16:13:25 Rocky systemd[1]: Started LSB: Initialise the Linux Virtual Server.
    [root@Rocky ~]# systemctl stop lvs_dr
    [root@Rocky ~]# systemctl status lvs_dr
    ● lvs_dr.service - LSB: Initialise the Linux Virtual Server
    Loaded: loaded (/etc/rc.d/init.d/lvs_dr; generated)
    Active: inactive (dead) since Sat 2025-03-22 16:13:42 CST; 2s ago
    Docs: man:systemd-sysv-generator(8)
    Process: 13317 ExecStop=/etc/rc.d/init.d/lvs_dr stop (code=exited, status=0/SUCCESS)
    Process: 13295 ExecStart=/etc/rc.d/init.d/lvs_dr start (code=exited, status=0/SUCCESS)

    Mar 22 16:13:25 Rocky lvs_dr[13304]: ax25 (AMPR AX.25) netrom (AMPR NET/ROM) rose (AMPR ROSE)
    Mar 22 16:13:25 Rocky lvs_dr[13304]: ipx (Novell IPX) ddp (Appletalk DDP) ash (Ash)
    Mar 22 16:13:25 Rocky lvs_dr[13304]: x25 (CCITT X.25)
    Mar 22 16:13:25 Rocky lvs_dr[13305]: /etc/rc.d/init.d/lvs_dr: line 30: 255.255.255.255: command not found
    Mar 22 16:13:25 Rocky lvs_dr[13295]: starting LVS-DR Server is ok !
    Mar 22 16:13:25 Rocky systemd[1]: Started LSB: Initialise the Linux Virtual Server.
    Mar 22 16:13:42 Rocky systemd[1]: Stopping LSB: Initialise the Linux Virtual Server...
    Mar 22 16:13:42 Rocky lvs_dr[13317]: stopping LVS-DR server is ok !
    Mar 22 16:13:42 Rocky systemd[1]: lvs_dr.service: Succeeded.
    Mar 22 16:13:42 Rocky systemd[1]: Stopped LSB: Initialise the Linux Virtual Server.
    [root@Rocky ~]# systemctl restart lvs_dr
    [root@Rocky ~]# systemctl status lvs_dr
    ● lvs_dr.service - LSB: Initialise the Linux Virtual Server
    Loaded: loaded (/etc/rc.d/init.d/lvs_dr; generated)
    Active: active (exited) since Sat 2025-03-22 16:13:59 CST; 1s ago
    Docs: man:systemd-sysv-generator(8)
    Process: 13317 ExecStop=/etc/rc.d/init.d/lvs_dr stop (code=exited, status=0/SUCCESS)
    Process: 13332 ExecStart=/etc/rc.d/init.d/lvs_dr start (code=exited, status=0/SUCCESS)

    Mar 22 16:13:59 Rocky lvs_dr[13341]: eui64 (Generic EUI-64)
    Mar 22 16:13:59 Rocky lvs_dr[13341]: <AF>=Address family. Default: inet
    Mar 22 16:13:59 Rocky lvs_dr[13341]: List of possible address families:
    Mar 22 16:13:59 Rocky lvs_dr[13341]: unix (UNIX Domain) inet (DARPA Internet) inet6 (IPv6)
    Mar 22 16:13:59 Rocky lvs_dr[13341]: ax25 (AMPR AX.25) netrom (AMPR NET/ROM) rose (AMPR ROSE)
    Mar 22 16:13:59 Rocky lvs_dr[13341]: ipx (Novell IPX) ddp (Appletalk DDP) ash (Ash)
    Mar 22 16:13:59 Rocky lvs_dr[13341]: x25 (CCITT X.25)
    Mar 22 16:13:59 Rocky lvs_dr[13342]: /etc/rc.d/init.d/lvs_dr: line 30: 255.255.255.255: command not found
    Mar 22 16:13:59 Rocky lvs_dr[13332]: starting LVS-DR Server is ok !
    Mar 22 16:13:59 Rocky systemd[1]: Started LSB: Initialise the Linux Virtual Server.

  • 在RS上编写脚本

    复制代码
    [root@Rocky ~]# 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=172.25.254.225
    . /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/ens160/arp_ignore
    		echo "2" >/proc/sys/net/ipv4/conf/ens160/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/ens160/arp_ignore
    	echo "0" >/proc/sys/net/ipv4/conf/ens160/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@Rocky ~]# chmod +x /etc/init.d/lvs_rs
    [root@Rocky ~]# chkconfig --add lvs_rs
    [root@Rocky ~]# chkconfig  lvs_rs on
  • 检验是否配置成功

    复制代码
    [root@Rocky ~]# ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
        inet 172.25.254.225/32 brd 172.25.254.225 scope global lo:10
           valid_lft forever preferred_lft forever
        inet6 ::1/128 scope host
           valid_lft forever preferred_lft forever
    2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
        link/ether 00:0c:29:d5:05:95 brd ff:ff:ff:ff:ff:ff
        altname enp3s0
        inet 172.25.254.103/24 brd 172.25.254.255 scope global noprefixroute ens160
           valid_lft forever preferred_lft forever
        inet6 fe80::20c:29ff:fed5:595/64 scope link noprefixroute
           valid_lft forever preferred_lft forever
    [root@Rocky ~]# route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         172.25.254.2    0.0.0.0         UG    100    0        0 ens160
    172.25.254.0    0.0.0.0         255.255.255.0   U     100    0        0 ens160
    172.25.254.225  0.0.0.0         255.255.255.255 UH    0      0        0 lo
    [root@Rocky ~]# cat /proc/sys/net/ipv4/conf/lo/arp_ignore
    1
    [root@Rocky ~]# cat /proc/sys/net/ipv4/conf/ens160/arp_ignore
    1
相关推荐
西阳未落7 分钟前
Linux(12)——基础IO(下)
linux·运维·服务器
什么半岛铁盒9 分钟前
云服务器Xshell登录拒绝访问排查
运维·服务器
我是哪吒13 分钟前
分布式微服务系统架构第144集:FastAPI全栈开发教育系统
后端·面试·github
cido18 分钟前
kali在apt update报错提示没有公钥
linux·apt·kali
梓羽玩Python1 小时前
PDF解剖大师来了!LandingAI开源神器,这个Python库让百页文档秒变结构化数据!
python·github
wqqqianqian1 小时前
国产linux系统(银河麒麟,统信uos)使用 PageOffice在线编辑word文件保存数据同时保存文件
linux·word·信创·国产·保存·pageoffice·在线编辑
国际云1 小时前
腾讯云国际版和国内版账户通用吗?一样吗?为什么?
大数据·运维·阿里云·云计算
心之语歌1 小时前
ubuntu24.04 搭建 java 环境服务,以及mysql数据库
linux·ubuntu
网安刚哥1 小时前
我们开源了一款AI产品……
程序员·开源·github
愚润求学1 小时前
【Linux】POSIX信号量
linux·运维