LVS(Linux Virtual Server)是一个用于构建可扩展和高可用性的负载均衡集群的软件。它基于Linux操作系统,并提供了一种将网络流量分发到多个后端服务器的机制。
总结:LVS通过将客户端请求分发到多个后端服务器,实现了负载均衡和高可用性。它能够提高系统的性能和可靠性,确保用户请求得到有效处理,并在后端服务器故障时自动切换到其他可用服务器。
环境准备四台主机,一台负载均衡机lvs,两台web集群,一台server做测试
[root@node1 ~]# yum install ipvsadm
[root@node1 ~]# ifconfig ens33:100 192.168.136.100/24
[root@node1 ~]# ipvsadm -A -t 192.168.136.100:80 -s rr
[root@node1 ~]# ipvsadm -a -t 192.168.136.100:80 -r 192.168.136.162:80 -g
[root@node1 ~]# ipvsadm -a -t 192.168.136.100:80 -r 192.168.136.163:80 -g
# -A:表示添加一个新的虚拟服务器。
# -t 192.168.136.100:80:指定虚拟服务器的监听地址和端
# rr --调度算法即使用轮询(Round Robin)算法
# -g:表示使用直接路由模式(gatewaying mode)
[root@node1 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.136.100:80 rr
-> 192.168.136.162:80 Route 1 0 0
-> 192.168.136.163:80 Route 1 0 0
[root@node2 ~]# yum install httpd
[root@node2 ~]# systemctl start httpd 启动http服务
[root@node2 ~]# echo "web test page ,ip is `hostname -I`" >/var/www/html/index.html
[root@node2 ~]# ifconfig lo:100 192.168.136.100 netmask 255.255.255.255
[root@node2 ~]# route add -host 192.168.136.100 dev lo
[root@node2 ~]# echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@node2 ~]# echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@node2 ~]# echo "1" > /proc/sys/net/ipv4/conf/ens33/arp_ignore
[root@node2 ~]# echo "2" > /proc/sys/net/ipv4/conf/ens33/arp_announce
[root@server ~]# for ((i=1;i<=6;i++)); do curl 192.168.136.100; done
web test page ,ip is 192.168.136.163
web test page ,ip is 192.168.136.162
web test page ,ip is 192.168.136.163
web test page ,ip is 192.168.136.162
web test page ,ip is 192.168.136.163
web test page ,ip is 192.168.136.162
lvs director 上的脚本
#!/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.95.10
RIP1=192.168.95.11
RIP2=192.168.95.12
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 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
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.95.10
. /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/eth0/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/eth0/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore