LVS的3种负载均衡技术的测试

2024-06-14 06:18
文章标签 技术 负载 测试 均衡 lvs

本文主要是介绍LVS的3种负载均衡技术的测试,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在测试之前安装ipvsadm
安装之前需要安装依赖:

yum install kernel-devel gcc openssl openssl-devel popt

到官网 【http://www.linuxvirtualserver.org 】下载ipvsadm时需要注意查看服务器内核版本

查询你的服务器内核版本:

rpm -q kernel-devel

根据服务器内核版本选择ipvsadm
下载后安装

tar -xvf ipvsadm-x.xx.tar.gzcd ipvsadm-x.xxmake && make install

测试用的是3台CentOS7主机
node1 负载均衡服务器
node2 主机1
node3 主机2

第一种负载均衡:NAT
根据下图配置
NAT搭建图

在node1中创建脚本:lvs-dr-node1.sh

#!/bin/sh
#------mini-HOWTO-setup-LVS-NAT-director----------#set ip_forward ON for vs-nat director (1 on, 0 off).
cat /proc/sys/net/ipv4/ip_forward
echo "1" >/proc/sys/net/ipv4/ip_forward#director is gw for realservers
#turn OFF icmp redirects (1 on, 0 off)
echo "0" >/proc/sys/net/ipv4/conf/all/send_redirects
cat       /proc/sys/net/ipv4/conf/all/send_redirects
echo "0" >/proc/sys/net/ipv4/conf/default/send_redirects
cat       /proc/sys/net/ipv4/conf/default/send_redirects
echo "0" >/proc/sys/net/ipv4/conf/eno16777736/send_redirects
cat       /proc/sys/net/ipv4/conf/eno16777736/send_redirects#setup DIP
/sbin/ifconfig eno16777736 10.0.0.1 broadcast 10.0.0.255 netmask 255.255.255.0#setup VIP
/sbin/ifconfig eno16777736:0 192.168.1.11 broadcast 192.168.1.255 netmask 255.255.255.0#set default gateway
/sbin/route add default gw 192.168.1.1 netmask 0.0.0.0 metric 1#clear ipvsadm tables
/sbin/ipvsadm -C#install LVS services with ipvsadm
#add telnet to VIP with rr sheduling
/sbin/ipvsadm -A -t 192.168.1.11:80 -s rr#first realserver
#forward telnet to realserver 10.0.0.2 using LVS-NAT (-m), with weight=1
/sbin/ipvsadm -a -t 192.168.1.11:80 -r 10.0.0.2:80 -m -w 1
#check that realserver is reachable from director
ping -c 1 10.0.0.2
#second realserver
#forward telnet to realserver 10.0.0.3 using LVS-NAT (-m), with weight=1
/sbin/ipvsadm -a -t 192.168.1.11:80 -r 10.0.0.3:80 -m -w 1
#checking if realserver is reachable from director
ping -c 1 10.0.0.3#list ipvsadm table
/sbin/ipvsadm
#------mini-HOWTO-setup-LVS-NAT-director----------

在node2中创建脚本:lvs-dr-node2.sh

#!/bin/sh
#---------mini-HOWTO-setup-LVS-NAT-realserver-------
#setup IP
/sbin/ifconfig eno16777736 10.0.0.2 broadcast 10.0.0.255 netmask 255.255.255.0
#installing default gw 10.0.0.1 for vs-nat'
/sbin/route add default gw 10.0.0.1
#show routing table
/bin/netstat -rn#checking if DEFAULT_GW is reachable
ping -c 1 10.0.0.1#looking for VIP on director from realserver
ping -c 1 10.0.0.3#set_realserver_ip_forwarding to OFF (1 on, 0 off).
echo "0" >/proc/sys/net/ipv4/ip_forward
cat       /proc/sys/net/ipv4/ip_forward
#---------mini-HOWTO-setup-LVS-NAT-realserver-------

在node3中创建脚本:lvs-nat-node3.sh

#!/bin/sh
#---------mini-HOWTO-setup-LVS-NAT-realserver-------
#setup IP
/sbin/ifconfig eno16777736 10.0.0.3 broadcast 10.0.0.255 netmask 255.255.255.0
#installing default gw 10.0.0.1 for vs-nat'
/sbin/route add default gw 10.0.0.1
#show routing table
/bin/netstat -rn#checking if DEFAULT_GW is reachable
ping -c 1 10.0.0.1#looking for VIP on director from realserver
ping -c 1 10.0.0.2#set_realserver_ip_forwarding to OFF (1 on, 0 off).
echo "0" >/proc/sys/net/ipv4/ip_forward
cat       /proc/sys/net/ipv4/ip_forward
#---------mini-HOWTO-setup-LVS-NAT-realserver-------

分别给脚本附加执行权限

chmod -x lvs-nat-xxxxx.sh

分别运行3个脚本

./lvs-nat-xxxx.sh

NAT测试结果
NAT结果图

第二种负载均衡:TUN
根据下图配置
TUN搭建图
在node1中创建脚本:lvs-tun-node1.sh

#!/bin/bash
#---------------mini-rc.lvs_dr-director------------------------
#set ip_forward OFF for lvs-dr director (1 on, 0 off)
#(there is no forwarding in the conventional sense for LVS-DR)
cat       /proc/sys/net/ipv4/ip_forward
echo "0" >/proc/sys/net/ipv4/ip_forward#director is not gw for realservers: leave icmp redirects on
echo 'setting icmp redirects (1 on, 0 off) '
echo "1" >/proc/sys/net/ipv4/conf/all/send_redirects
cat       /proc/sys/net/ipv4/conf/all/send_redirects
echo "1" >/proc/sys/net/ipv4/conf/default/send_redirects
cat       /proc/sys/net/ipv4/conf/default/send_redirects
echo "1" >/proc/sys/net/ipv4/conf/eno16777736/send_redirects
cat       /proc/sys/net/ipv4/conf/eno16777736/send_redirects#setup DIP
/sbin/ifconfig eno16777736 192.168.1.101 broadcast 192.168.1.255 netmask 255.255.255.0#add ethernet device and routing for VIP 192.168.1.11
/sbin/ifconfig tunl0 192.168.1.11 broadcast 192.168.1.11 netmask 255.255.255.255
/sbin/route add -host 192.168.1.11 dev tunl0
#listing ifconfig info for VIP 192.168.1.11
/sbin/ifconfig tunl0#check VIP 192.168.1.11 is reachable from self (director)
/bin/ping -c 1 192.168.1.11
#listing routing info for VIP 192.168.1.11
/bin/netstat -rn#setup_ipvsadm_table
#clear ipvsadm table
/sbin/ipvsadm -C
#installing LVS services with ipvsadm
#add telnet to VIP with round robin scheduling
/sbin/ipvsadm -A -t 192.168.1.11:80 -s rr#forward telnet to realserver using direct routing with weight 1
/sbin/ipvsadm -a -t 192.168.1.11:80 -r 192.168.1.102:80 -i -w 1
#check realserver reachable from director
ping -c 1 192.168.1.102#forward telnet to realserver using direct routing with weight 1
/sbin/ipvsadm -a -t 192.168.1.11:80 -r 192.168.1.103:80 -i -w 1
#check realserver reachable from director
ping -c 1 192.168.1.103#displaying ipvsadm settings
/sbin/ipvsadm#not installing a default gw for LVS_TYPE vs-dr
#---------------mini-rc.lvs_dr-director------------------------

在node2中创建脚本:lvs-tun-node2.sh

#!/bin/bash
#----------mini-rc.lvs_dr-realserver------------------
#setup IP
/sbin/ifconfig eno16777736 192.168.1.103 broadcast 192.168.1.255 netmask 255.255.255.0
#installing default gw 192.168.1.1 for vs-dr
/sbin/route add default gw 192.168.1.1
#showing routing table
/bin/netstat -rn
#checking if DEFAULT_GW 192.168.1.1 is reachable
ping -c 1 192.168.1.1#set_realserver_ip_forwarding to OFF (1 on, 0 off).
echo "0" >/proc/sys/net/ipv4/ip_forward
cat       /proc/sys/net/ipv4/ip_forward#looking for DIP 192.168.1.101
ping -c 1 192.168.1.101#looking for VIP (will be on director)
ping -c 1 192.168.1.11#install_realserver_vip
/sbin/ifconfig tunl0 192.168.1.11 broadcast 192.168.1.11 netmask 0xffffffff up
#ifconfig output
/sbin/ifconfig tunl0
#installing route for VIP 192.168.1.11 on device tunl0
/sbin/route add -host 192.168.1.11 dev tunl0
#listing routing info for VIP 192.168.1.11
/bin/netstat -rn#hiding interface tunl0, will not arp
echo "1" >/proc/sys/net/ipv4/conf/all/hidden
cat       /proc/sys/net/ipv4/conf/all/hidden
echo "1" >/proc/sys/net/ipv4/conf/tunl0/hidden
cat       /proc/sys/net/ipv4/conf/tunl0/hidden#----------mini-rc.lvs_dr-realserver------------------

在node3中创建脚本:lvs-tun-node3.sh

#!/bin/bash
#----------mini-rc.lvs_dr-realserver------------------
#setup IP
/sbin/ifconfig eno16777736 192.168.1.102 broadcast 192.168.1.255 netmask 255.255.255.0
#installing default gw 192.168.1.1 for vs-dr
/sbin/route add default gw 192.168.1.1
#showing routing table
/bin/netstat -rn
#checking if DEFAULT_GW 192.168.1.1 is reachable
ping -c 1 192.168.1.1#set_realserver_ip_forwarding to OFF (1 on, 0 off).
echo "0" >/proc/sys/net/ipv4/ip_forward
cat       /proc/sys/net/ipv4/ip_forward#looking for DIP 192.168.1.101
ping -c 1 192.168.1.101#looking for VIP (will be on director)
ping -c 1 192.168.1.11#install_realserver_vip
/sbin/ifconfig tunl0 192.168.1.11 broadcast 192.168.1.11 netmask 0xffffffff up
#ifconfig output
/sbin/ifconfig tunl0
#installing route for VIP 192.168.1.11 on device tunl0
/sbin/route add -host 192.168.1.11 dev tunl0
#listing routing info for VIP 192.168.1.11
/bin/netstat -rn#hiding interface tunl0, will not arp
echo "1" >/proc/sys/net/ipv4/conf/all/hidden
cat       /proc/sys/net/ipv4/conf/all/hidden
echo "1" >/proc/sys/net/ipv4/conf/tunl0/hidden
cat       /proc/sys/net/ipv4/conf/tunl0/hidden#----------mini-rc.lvs_dr-realserver------------------

分别给脚本附加执行权限

chmod -x lvs-tun-xxxxx.sh

分别运行3个脚本

./lvs-tun-xxxx.sh

TUN测试结果
TUN结果图

第三种负载均衡:DR
DR搭建图

在node1中创建脚本:lvs-dr-node1.sh

#!/bin/bash
#---------------mini-rc.lvs_dr-director------------------------
#set ip_forward OFF for lvs-dr director (1 on, 0 off)
#(there is no forwarding in the conventional sense for LVS-DR)
cat       /proc/sys/net/ipv4/ip_forward
echo "0" >/proc/sys/net/ipv4/ip_forward#director is not gw for realservers: leave icmp redirects on
echo 'setting icmp redirects (1 on, 0 off) '
echo "1" >/proc/sys/net/ipv4/conf/all/send_redirects
cat       /proc/sys/net/ipv4/conf/all/send_redirects
echo "1" >/proc/sys/net/ipv4/conf/default/send_redirects
cat       /proc/sys/net/ipv4/conf/default/send_redirects
echo "1" >/proc/sys/net/ipv4/conf/eno16777736/send_redirects
cat       /proc/sys/net/ipv4/conf/eno16777736/send_redirects#setup DIP
/sbin/ifconfig eno16777736 192.168.1.101 broadcast 192.168.1.255 netmask 255.255.255.0#add ethernet device and routing for VIP 192.168.1.11
/sbin/ifconfig eno16777736:0 192.168.1.11 broadcast 192.168.1.11 netmask 255.255.255.255
/sbin/route add -host 192.168.1.11 dev eno16777736:0
#listing ifconfig info for VIP 192.168.1.11
/sbin/ifconfig eno16777736:0#check VIP 192.168.1.11 is reachable from self (director)
/bin/ping -c 1 192.168.1.11
#listing routing info for VIP 192.168.1.11
/bin/netstat -rn#setup_ipvsadm_table
#clear ipvsadm table
/sbin/ipvsadm -C
#installing LVS services with ipvsadm
#add telnet to VIP with round robin scheduling
/sbin/ipvsadm -A -t 192.168.1.11:80 -s rr#forward telnet to realserver using direct routing with weight 1
/sbin/ipvsadm -a -t 192.168.1.11:80 -r 192.168.1.102:80 -g -w 1
#check realserver reachable from director
ping -c 1 192.168.1.102#forward telnet to realserver using direct routing with weight 1
/sbin/ipvsadm -a -t 192.168.1.11:80 -r 192.168.1.103:80 -g -w 1
#check realserver reachable from director
ping -c 1 192.168.1.103#displaying ipvsadm settings
/sbin/ipvsadm#not installing a default gw for LVS_TYPE vs-dr
#---------------mini-rc.lvs_dr-director------------------------

在node2中创建脚本:lvs-dr-node2.sh

#!/bin/bash
#----------mini-rc.lvs_dr-realserver------------------
#setup IP
/sbin/ifconfig eno16777736 192.168.1.102 broadcast 192.168.1.255 netmask 255.255.255.0
#installing default gw 192.168.1.1 for vs-dr
/sbin/route add default gw 192.168.1.1
#showing routing table
/bin/netstat -rn
#checking if DEFAULT_GW 192.168.1.1 is reachable
ping -c 1 192.168.1.1#set_realserver_ip_forwarding to OFF (1 on, 0 off).
echo "0" >/proc/sys/net/ipv4/ip_forward
cat       /proc/sys/net/ipv4/ip_forward#looking for DIP 192.168.1.101
ping -c 1 192.168.1.101#looking for VIP (will be on director)
ping -c 1 192.168.1.11#install_realserver_vip
/sbin/ifconfig lo:0 192.168.1.11 broadcast 192.168.1.11 netmask 0xffffffff up
#ifconfig output
/sbin/ifconfig lo:0
#installing route for VIP 192.168.1.11 on device lo:0
/sbin/route add -host 192.168.1.11 dev lo:0
#listing routing info for VIP 192.168.1.11
/bin/netstat -rn#hiding interface lo:0, will not arp
echo "1" >/proc/sys/net/ipv4/conf/all/hidden
cat       /proc/sys/net/ipv4/conf/all/hidden
echo "1" >/proc/sys/net/ipv4/conf/lo/hidden
cat       /proc/sys/net/ipv4/conf/lo/hidden#----------mini-rc.lvs_dr-realserver------------------

在node3中创建脚本:lvs-dr-node3.sh

#!/bin/bash
#----------mini-rc.lvs_dr-realserver------------------
#setup IP
/sbin/ifconfig eno16777736 192.168.1.103 broadcast 192.168.1.255 netmask 255.255.255.0
#installing default gw 192.168.1.1 for vs-dr
/sbin/route add default gw 192.168.1.1
#showing routing table
/bin/netstat -rn
#checking if DEFAULT_GW 192.168.1.1 is reachable
ping -c 1 192.168.1.1#set_realserver_ip_forwarding to OFF (1 on, 0 off).
echo "0" >/proc/sys/net/ipv4/ip_forward
cat       /proc/sys/net/ipv4/ip_forward#looking for DIP 192.168.1.101
ping -c 1 192.168.1.101#looking for VIP (will be on director)
ping -c 1 192.168.1.11#install_realserver_vip
/sbin/ifconfig lo:0 192.168.1.11 broadcast 192.168.1.11 netmask 0xffffffff up
#ifconfig output
/sbin/ifconfig lo:0
#installing route for VIP 192.168.1.11 on device lo:0
/sbin/route add -host 192.168.1.11 dev lo:0
#listing routing info for VIP 192.168.1.11
/bin/netstat -rn#hiding interface lo:0, will not arp
echo "1" >/proc/sys/net/ipv4/conf/all/hidden
cat       /proc/sys/net/ipv4/conf/all/hidden
echo "1" >/proc/sys/net/ipv4/conf/lo/hidden
cat       /proc/sys/net/ipv4/conf/lo/hidden#----------mini-rc.lvs_dr-realserver------------------

分别给脚本附加执行权限

chmod -x lvs-dr-xxxxx.sh

分别运行3个脚本

./lvs-dr-xxxx.sh

DR测试结果

DR结果图

本文脚本文件:
LVS3中负载均衡技术脚本

个人博客原文:
LVS的3种负载均衡技术的测试

参考文章链接:
LVS配置教程

这篇关于LVS的3种负载均衡技术的测试的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1059610

相关文章

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

Java利用JSONPath操作JSON数据的技术指南

《Java利用JSONPath操作JSON数据的技术指南》JSONPath是一种强大的工具,用于查询和操作JSON数据,类似于SQL的语法,它为处理复杂的JSON数据结构提供了简单且高效... 目录1、简述2、什么是 jsONPath?3、Java 示例3.1 基本查询3.2 过滤查询3.3 递归搜索3.4

Python中随机休眠技术原理与应用详解

《Python中随机休眠技术原理与应用详解》在编程中,让程序暂停执行特定时间是常见需求,当需要引入不确定性时,随机休眠就成为关键技巧,下面我们就来看看Python中随机休眠技术的具体实现与应用吧... 目录引言一、实现原理与基础方法1.1 核心函数解析1.2 基础实现模板1.3 整数版实现二、典型应用场景2

SpringCloud之LoadBalancer负载均衡服务调用过程

《SpringCloud之LoadBalancer负载均衡服务调用过程》:本文主要介绍SpringCloud之LoadBalancer负载均衡服务调用过程,具有很好的参考价值,希望对大家有所帮助,... 目录前言一、LoadBalancer是什么?二、使用步骤1、启动consul2、客户端加入依赖3、以服务

SpringCloud负载均衡spring-cloud-starter-loadbalancer解读

《SpringCloud负载均衡spring-cloud-starter-loadbalancer解读》:本文主要介绍SpringCloud负载均衡spring-cloud-starter-loa... 目录简述主要特点使用负载均衡算法1. 轮询负载均衡策略(Round Robin)2. 随机负载均衡策略(

SpringBoot中整合RabbitMQ(测试+部署上线最新完整)的过程

《SpringBoot中整合RabbitMQ(测试+部署上线最新完整)的过程》本文详细介绍了如何在虚拟机和宝塔面板中安装RabbitMQ,并使用Java代码实现消息的发送和接收,通过异步通讯,可以优化... 目录一、RabbitMQ安装二、启动RabbitMQ三、javascript编写Java代码1、引入

Nginx设置连接超时并进行测试的方法步骤

《Nginx设置连接超时并进行测试的方法步骤》在高并发场景下,如果客户端与服务器的连接长时间未响应,会占用大量的系统资源,影响其他正常请求的处理效率,为了解决这个问题,可以通过设置Nginx的连接... 目录设置连接超时目的操作步骤测试连接超时测试方法:总结:设置连接超时目的设置客户端与服务器之间的连接

Spring Cloud LoadBalancer 负载均衡详解

《SpringCloudLoadBalancer负载均衡详解》本文介绍了如何在SpringCloud中使用SpringCloudLoadBalancer实现客户端负载均衡,并详细讲解了轮询策略和... 目录1. 在 idea 上运行多个服务2. 问题引入3. 负载均衡4. Spring Cloud Load

如何测试计算机的内存是否存在问题? 判断电脑内存故障的多种方法

《如何测试计算机的内存是否存在问题?判断电脑内存故障的多种方法》内存是电脑中非常重要的组件之一,如果内存出现故障,可能会导致电脑出现各种问题,如蓝屏、死机、程序崩溃等,如何判断内存是否出现故障呢?下... 如果你的电脑是崩溃、冻结还是不稳定,那么它的内存可能有问题。要进行检查,你可以使用Windows 11

性能测试介绍

性能测试是一种测试方法,旨在评估系统、应用程序或组件在现实场景中的性能表现和可靠性。它通常用于衡量系统在不同负载条件下的响应时间、吞吐量、资源利用率、稳定性和可扩展性等关键指标。 为什么要进行性能测试 通过性能测试,可以确定系统是否能够满足预期的性能要求,找出性能瓶颈和潜在的问题,并进行优化和调整。 发现性能瓶颈:性能测试可以帮助发现系统的性能瓶颈,即系统在高负载或高并发情况下可能出现的问题