架构图
构建代码
方式一:手工配置
一、Director-Server配置
1、安装 ipvsadm包
[[email protected] ~]# yum install -y ipvsadm
2、配置LVS虚拟IP(VIP)
#采用子接口配置
[[email protected] ~]# ifconfig ens33:100 192.168.74.100 netmask 255.255.255.0 up
#查看地址
[[email protected] ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.74.153 netmask 255.255.255.0 broadcast 192.168.74.255
ether 00:0c:29:99:83:dd txqueuelen 1000 (Ethernet)
RX packets 1331 bytes 100169 (97.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 523 bytes 55138 (53.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:100: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.74.100 netmask 255.255.255.0 broadcast 192.168.74.255
ether 00:0c:29:99:83:dd txqueuelen 1000 (Ethernet)
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
3、手工添加LVS服务并增加两台Real-Server
#删除之前配置
[[email protected] ~]# ipvsadm -C
#指定Director-Server
[[email protected] ~]# ipvsadm -A -t 192.168.74.100:80 -s rr
#指定Real-Server
[[email protected] ~]# ipvsadm -a -t 192.168.74.100:80 -r 192.168.74.155:80 -g
[[email protected] ~]# ipvsadm -a -t 192.168.74.100:80 -r 192.168.74.156:80 -g
4、查看配置
[[email protected] ~]# 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.74.100:80 rr
-> 192.168.74.155:80 Route 1 0 0
-> 192.168.74.156:80 Route 1 0 0
5、安装httpd包并启动服务
[[email protected] ~]# yum install -y httpd
[[email protected] ~]# systemctl start httpd
二、Web1配置
1、安装httpd包并启动服务写入网页文件
[[email protected] ~]# yum install -y httpd
[[email protected] ~]# systemctl start httpd
[roo[email protected] ~]# echo "web1 test page" > /var/www/html/index.html
2、绑定VIP并添加本机访问VIP的路由
[[email protected] ~]# ifconfig lo:100 192.168.74.100 netmask 255.255.255.255 up
[[email protected] ~]# route add -host 192.168.74.100 dev lo
3、抑制ARP响应
[[email protected] ~]# echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
[[email protected] ~]# echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
[[email protected] ~]# echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
[[email protected] ~]# echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
4、查看IP
[[email protected] ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.74.155 netmask 255.255.255.0 broadcast 192.168.74.255
ether 00:0c:29:47:69:57 txqueuelen 1000 (Ethernet)
RX packets 38691 bytes 50796960 (48.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8853 bytes 799350 (780.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo:100: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 192.168.74.100 netmask 255.255.255.255
loop txqueuelen 1000 (Local Loopback)
三、web2配置
1、安装httpd包并启动服务写入网页文件
[[email protected] ~]# yum install -y httpd
[[email protected] ~]# systemctl start httpd
[roo[email protected] ~]# echo "web2 test page" > /var/www/html/index.html
2、绑定VIP并添加本机访问VIP的路由
[[email protected] ~]# ifconfig lo:100 192.168.74.100 netmask 255.255.255.255 up
[[email protected] ~]# route add -host 192.168.74.100 dev lo
3、抑制ARP响应
[[email protected] ~]# echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
[[email protected] ~]# echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
[[email protected] ~]# echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
[[email protected] ~]# echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
4、查看IP
[[email protected] ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.74.156 netmask 255.255.255.0 broadcast 192.168.74.255
ether 00:0c:29:13:2a:fb txqueuelen 1000 (Ethernet)
RX packets 38058 bytes 50759244 (48.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8299 bytes 767038 (749.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo:100: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 192.168.74.100 netmask 255.255.255.255
loop txqueuelen 1000 (Local Loopback)
四、测试
[[email protected] ~]# for ((i=1;i<=10;i++))
> do
> curl 192.168.74.100
> done
web1 test page
web2 test page
web1 test page
web2 test page
web1 test page
web2 test page
web1 test page
web2 test page
web1 test page
web2 test page
#查看LVS集群配置
[[email protected] ~]# 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.74.100:80 rr
-> 192.168.74.155:80 Route 1 0 5
-> 192.168.74.156:80 Route 1 0 5
方式二:脚本配置
一、Director-Server配置
1、配置DS脚本
[[email protected] ~]# 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
VIP=192.168.74.100
RIP1=192.168.74.155
RIP2=192.168.74.156
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}:100 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev ${DipName}:100
#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}:100
/sbin/ifconfig ${DipName}:100 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
2、添加脚本到系统服务
#给脚本一个执行权限
[[email protected] ~]# chmod +x /etc/init.d/lvs_dr
#将脚本添加至系统服务
[[email protected] ~]# chkconfig --add lvs_dr
#并开机自启动服务
[[email protected] ~]# chkconfig lvs_dr on
#启动服务
[[email protected] ~]# systemctl start lvs_dr
3、查看配置
[[email protected] ~]# 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.74.100:80 rr
-> 192.168.74.155:80 Route 1 0 0
-> 192.168.74.156:80 Route 1 0 0
二、web配置(web1与web2配置相同,此处以web1为例)
1、配置RS脚本
[[email protected] ~]# 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.74.100
. /etc/rc.d/init.d/functions
start() {
PID=`ifconfig | grep lo:100 | wc -l`
if [ $PID -ne 0 ];
then
echo "The LVS-DR-RIP Server is already running !"
else
/sbin/ifconfig lo:100 $VIP netmask 255.255.255.255 broadcast $VIP up
/sbin/route add -host $VIP dev lo:100
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:100
/sbin/ifconfig lo:100 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
2、添加脚本到系统服务
#给脚本添加执行权限
[[email protected] ~]# chmod +x /etc/init.d/lvs_rs
#将脚本添加至系统服务
[[email protected] ~]# chkconfig --add lvs_rs
#设置开机自启动服务
[[email protected] ~]# chkconfig lvs_rs on
#启动服务
[[email protected] ~]# systemctl start lvs_rs
3、查看IP
[[email protected] ~]# 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 192.168.74.100/32 scope global lo:100
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:47:69:57 brd ff:ff:ff:ff:ff:ff
inet 192.168.74.155/24 brd 192.168.74.255 scope global dynamic ens33
valid_lft 1374sec preferred_lft 1374sec
4、查看路由
[[email protected] ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.74.2 0.0.0.0 UG 0 0 0 ens33
192.168.74.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33
192.168.74.100 0.0.0.0 255.255.255.255 UH 0 0 0 lo
三、测试
[[email protected] ~]# for ((i=1;i<=10;i++))
> do
> curl 192.168.74.100
> done
web2 test page
web1 test page
web2 test page
web1 test page
web2 test page
web1 test page
web2 test page
web1 test page
web2 test page
web1 test page