目录
一.群集的类型
二.负载均衡的分层结构
三.负载均衡的工作模式
四.LVS的负载调度算法
五.ipvsadm管理工具
六.构建LVS-NAT
七.构建LVS-DR
[root@mysql1 ~]# modprobe ip_vs //加载ip_vs模块
[root@mysql1 ~]# cat /proc/net/ip_vs //查看ip_vs版本信息
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@mysql1 ~]#
[root@localhost ~]# rpm -q ipvsadm
未安装软件包 ipvsadm
[root@localhost ~]# yum install ipvsadm -y
[root@localhost ~]# ipvsadm -A -t 192.168.10.100:80 -s rr
[root@localhost ~]#
## -A表示添加虚拟服务器,-t用来指定VIP地址以及TCP端口,
-s用来指定负载调度算法——轮询(rr)、加权轮询(wrr)、最少连接(lc)、加权最少连接(wlc)
添加服务器节点服务器,为虚拟服务器192.168.10.100添加四个服务器节点,IP地址为192.168.43.101,对应的ipvsadm命令操作如下。若希望使用保持连接,还添加“-p 60”选项,其中60为保持时间秒
[root@localhost ~]# ipvsadm -a -t 192.168.10.100:80 -r 192.168.43.101:80 -m -w 1
[root@localhost ~]#
##选项-a表示添加真实服务器,-t用来指定VIP(虚拟地址)以及TCP端口,
-r用来指定RIP地址(真实地址)以及TCP端口,
-m表示使用NAT群集模式(-g DR模式和-i TUN模式),
-w用来设置权重(权重为0时,表示暂停节点)
[root@localhost ~]# 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.10.100:80 rr
-> 192.168.43.101:80 Masq 1 0 0
[root@localhost ~]#
##forwording列下的Masq对应Masquerade(地址伪装),
表示采用的群集模式为NAT;
如果是Route,表示采用的群集模式为DR
[root@localhost ~]# ipvsadm -d -r 192.168.43.101:80 -t 192.168.10.100:80
[root@localhost ~]# 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.10.100:80 rr
[root@localhost ~]#
##此外,需要删除整个虚拟服务器时,使用选项-D并且指定虚拟IP地址就可以,无需指定节点。
##例如,执行 "ipvsadm -D -t 192.168.10.100:80",删除此虚拟服务器
保存负载分配策略,使用导出、导入工具ipvsadm-save/ipvsadm-restore可以保存、恢复LVS策略,操作方法类似iptables规则的导出、导入。通过系统服务ipvsadm也可以保存策略,如可执”service ipvsadm save“;也可以快速清除、重建负载分配策略
[root@localhost ~]# ipvsadm -a -t 192.168.10.100:80 -r 192.168.43.101:80 -m -w 1
[root@localhost ~]# ipvsadm-save > /etc/sysconfig/ipvsadm
[root@localhost ~]# cat /etc/sysconfig/ipvsadm
-A -t 192.168.10.100:http -s rr
-a -t 192.168.10.100:http -r localhost.localdomain:http -m -w 1
##使用ipvsadm保存策略
类别 | IP地址 | 系统 | 软件包 |
LVS调度器 | ens33:192.168.43.101/24 esn37:12.0.0.1/24 |
centos7 |
ipvsadm |
web服务器1 | 192.168.43.102/24 | centos7 | httpd |
web服务器2 | 192.168.43.103/24 | centos7 | httpd |
NFS存储服务器 | 192.168.43.104/24 | centos7 | rpcbind nfs-utils |
外部客户机 | 12.0.0.12 | win10 |
1.添加两个硬盘,在现网上一般做磁盘阵列
fdisk /dev/sdb
fdisk /dev/sdc
2.格式化磁盘
[root@localhost ~]# mkfs.xfs /dev/sdb1
[root@localhost ~]# mkfs.xfs /dev/sdc1
3.建立挂载点
[root@localhost ~]# mkdir /opt/data /opt/yun
4.在配置文件里面设置自动挂载
[root@localhost ~]# vim /etc/fstab
/dev/sdb1 /opt/data xfs defaults 0 0
/dev/sdc1 /opt/yun xfs defaults 0 0
5.开启自动挂载
[root@localhost ~]# mount -a
6.查看挂载情况
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda2 20G 4.5G 16G 23% /
devtmpfs 471M 0 471M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 8.6M 478M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda5 10G 37M 10G 1% /home
/dev/sda1 8.0G 164M 7.9G 2% /boot
tmpfs 98M 4.0K 98M 1% /run/user/42
tmpfs 98M 24K 98M 1% /run/user/0
/dev/sdb1 20G 33M 20G 1% /opt/data
/dev/sdc1 20G 33M 20G 1% /opt/yun
6.关闭安全性功能,设置nfs服务
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# vim /etc/exports
/opt/data 192.168.43.0/24(rw,sync,no_root_squash)
/opt/yun 192.168.43.0/24(rw,sync,no_root_squash)
##rw代表可读写,sync代表同步写入,no_root_squash代表不进行降级处理
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl start rpcbind
[root@localhost ~]# showmount -e ##查看共享点的具体情况
Export list for localhost.localdomain:
/opt/yun 192.168.43.0/24
/opt/data 192.168.43.0/24
7.修改网卡为仅主机模式,按拓扑图配置IP地址信息
配置web server 1
1.安装httpd软件包
yum install httpd -y
2.修改网卡信息,关闭安全性功能
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
[root@localhost ~]# service network restart
3.配置主页面信息
cd /var/www/html
echo ""
4.查看NFS服务
[root@localhost ~]# showmount -e 192.168.43.104
Export list for 192.168.43.104:
/opt/yun 192.168.43.0/24
/opt/data 192.168.43.0/24
5.设置网络挂载点,将web服务的主页面挂载到NFS服务器上
[root@localhost ~]# vim /etc/fstab
192.168.43.104:/opt/data /var/www/html nfs defaults,_netdev 0 0
[root@localhost ~]# mount -a
6.开启httpd服务
systemctl start httpd
配置web server 2
1.安装httpd软件包
yum install httpd -y
2.修改网卡信息,关闭安全性功能
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
[root@localhost ~]# service network restart
3.配置主页面信息
cd /var/www/html
echo ""
4.查看NFS服务
[root@localhost ~]# showmount -e 192.168.43.104
Export list for 192.168.43.104:
/opt/yun 192.168.43.0/24
/opt/data 192.168.43.0/24
5.设置网络挂载点,将web服务的主页面挂载到NFS服务器上
[root@localhost ~]# vim /etc/fstab
192.168.43.104:/opt/yun /var/www/html nfs defaults,_netdev 0 0
[root@localhost ~]# mount -a
6.开启httpd服务
systemctl start httpd
1.安装ipvsadm工具
yum insatll ipvsadm -y
2.添加双网卡,修改为仅主机模式,修改IP地址信息,不需要网关
#先使用ifconfig查看网卡名称
3.重启网卡
[root@localhost network-scripts]# service network restart
Restarting network (via systemctl): [ 确定 ]
[root@localhost network-scripts]# ifconfig
ens33: flags=4163 mtu 1500
inet 192.168.43.101 netmask 255.255.255.0 broadcast 192.168.43.255
inet6 fe80::ba30:ef5e:b59f:4f1c prefixlen 64 scopeid 0x20
ether 00:0c:29:ce:5f:24 txqueuelen 1000 (Ethernet)
RX packets 4807 bytes 6504865 (6.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2551 bytes 162985 (159.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens37: flags=4163 mtu 1500
inet 12.0.0.1 netmask 255.255.255.0 broadcast 12.0.0.255
inet6 fe80::ece2:7d1e:9841:aaa4 prefixlen 64 scopeid 0x20
ether 00:0c:29:ce:5f:2e txqueuelen 1000 (Ethernet)
RX packets 31 bytes 3737 (3.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 64 bytes 9975 (9.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
4.开启路由转发功能,添加防火墙规则
##开启路由转发功能
vim /etc/sysctl.conf
#在最后加入
net.ipv4.ip_forward=1
##重新加载
sysctl -p
##清除转发表
iptables -F
#清除nat地址转换表
iptables -t nat -F
#添加防火墙规则帮助转发
iptables -t nat -A POSTROUTING -o ens37 -s 192.168.43.0/24 -j SNAT --to-source 12.0.0.1
5.设置ip_vs模块
##加载LVS内阁模块
modprobe ip_vs
##将命令保存到systemctl列表中
ipvsadm --save > /etc/sysconfig/ipvsadm
6.开启ip_vs模块功能
systemctl start ipvsadm.service
7.编写脚本,设置ip_vs规则
vim /opt/nat.s
#!/bin/bash
ipvsadm -C
ipvsadm -A -t 12.0.0.1:80 -s rr
ipvsadm -a -t 12.0.0.1:80 -r 192.168.100.110:80 -m
ipvsadm -a -t 12.0.0.1:80 -r 192.168.100.111:80 -m
ipvsadm
8.给予权限,执行脚本
chmod +x /opt/nat.sh
##执行脚本
[root@localhost opt]# ./nat.sh
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP localhost.localdomain:http rr
-> 192.168.43.102:http Masq 1 0 0
-> 192.168.43.103:http Masq 1 0 0
[root@localhost opt]#
类型 | IP地址 | 系统 | 软件包 |
客户机 | 12.0.0.12/24 | win10 | |
路由器 | ens33:192.168.100.1/24 ens37:12.0.0.1/24 |
centos7 | |
LVS调度器 | RIP:192.168.100.110/24 VIP:192.168.100.100/24 |
centos7 | ipvsadm |
web1 | RIP:192.168.43.120/24 VIP:192.168.100.100/24 |
centos7 | httpd |
web2 | RIP:192.168.43.130/24 VIP:192.168.100.100/24 |
centos7 | httpd |
NFS | RIP:192.168.43.140/24 | centos7 | rpcbind nfs-utils |
1.添加两块磁盘,创建分区,格式化
[root@localhost ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=1310656 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=5242624, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost ~]# mkfs.xfs /dev/sdc1
meta-data=/dev/sdc1 isize=512 agcount=4, agsize=1310656 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=5242624, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
2.创建挂载目录
[root@localhost ~]# mkdir /opt/data /opt/yun
3.设置自动挂载
[root@localhost ~]# vim /etc/fstab
/dev/sdb1 /opt/data xfs defaults 0 0
/dev/sdc1 /opt/yun xfs defaults 0 0
~
4.开启自动挂载
[root@localhost ~]# mount -a
5.查看挂载情况
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda2 20G 4.5G 16G 23% /
devtmpfs 471M 0 471M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 8.5M 478M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda5 10G 37M 10G 1% /home
/dev/sda1 8.0G 164M 7.9G 2% /boot
tmpfs 98M 24K 98M 1% /run/user/0
/dev/sdb1 20G 33M 20G 1% /opt/data
/dev/sdc1 20G 33M 20G 1% /opt/yun
6.设置nfs服务内容
[root@localhost ~]# vim /etc/exports
/opt/data 192.168.43.0/24(rw,sync,no_root_squash)
/opt/yun 192.168.43.0/24(rw,sync,no_root_squash)
7.关闭安全性功能,开启NFS服务
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl start rpcbind
8.验证NFS服务
[root@localhost ~]# showmount -e
Export list for localhost.localdomain:
/opt/yun 192.168.100.0/24
/opt/data 192.168.100.0/24
[root@localhost ~]#
9。修改网卡模式为仅主机模式
配置web server 1
1.安装httpd的软件包
yum install httpd -y
2.添加web站点信息
[root@localhost ~]# cd /var/www/html
[root@localhost html]# echo "data web server
" > index.html
3.修改真实网卡信息,RIP
[root@localhost html]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
IPADDR=192.168.100.120
GATEWAY=192.168.100.1
4.添加虚拟网卡信息,VIP
[root@localhost network-scripts]# cp -p ifcfg-lo ifcfg-lo:0
[root@localhost network-scripts]# vim ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.100.100
NETMASK=255.255.255.0
ONBOOT=yes
~
5.编辑服务器的LVS与VIP的脚本
[root@localhost network-scripts]# cd /etc/init.d
[root@localhost init.d]# ls
functions netconsole network README
[root@localhost init.d]# vim web.sh
#!/bin/bash
VIP=192.168.100.100
case "$1" in
start)
ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/sbin/route add -host $VIP dev lo:0
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/all/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p > /dev/null 2>&1
echo "real server start ok"
;;
stop)
ifconfig lo:0 down
route del $VIP /dev/null 2>&1
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/all/arp_ignore
echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce
echo "real server stop"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
~
6.关闭安全功能,开启各个服务
[root@localhost init.d]# ifup lo:0
[root@localhost init.d]# chmod +x web.sh
[root@localhost init.d]# service web.sh start
real server start ok
[root@localhost init.d]# systemctl start httpd.service
[root@localhost init.d]# systemctl stop firewalld
[root@localhost init.d]# setenforce 0
配置web server 2
1.安装httpd的软件包
yum install httpd -y
2.添加web站点信息
[root@localhost ~]# cd /var/www/html
[root@localhost html]# echo "yun web server
" > index.html
3.修改真实网卡信息,RIP
[root@localhost html]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
IPADDR=192.168.100.130
GATEWAY=192.168.100.1
4.添加虚拟网卡信息,VIP
[root@localhost network-scripts]# cp -p ifcfg-lo ifcfg-lo:0
[root@localhost network-scripts]# vim ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.100.100
NETMASK=255.255.255.0
ONBOOT=yes
~
5.编辑服务器的LVS与VIP的脚本
[root@localhost network-scripts]# cd /etc/init.d
[root@localhost init.d]# ls
functions netconsole network README
[root@localhost init.d]# vim web.sh
#!/bin/bash
VIP=192.168.100.100
case "$1" in
start)
ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/sbin/route add -host $VIP dev lo:0
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/all/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p > /dev/null 2>&1
echo "real server start ok"
;;
stop)
ifconfig lo:0 down
route del $VIP /dev/null 2>&1
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/all/arp_ignore
echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce
echo "real server stop"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
~
6.关闭安全功能,开启各个服务
[root@localhost init.d]# ifup lo:0
[root@localhost init.d]# chmod +x web.sh
[root@localhost init.d]# service web.sh start
real server start ok
[root@localhost init.d]# systemctl start httpd.service
[root@localhost init.d]# systemctl stop firewalld
[root@localhost init.d]# setenforce 0
1.安装ipvsadm工具
yum install ipvsadm -y
2.修改真实网卡信息,RIP
IPADDR=192.168.100.110
GATEWAY=192.168.100.1
3.添加虚拟VIP
[root@localhost network-scripts]# cp -p ifcfg-ens33 ifcfg-ens33:0
[root@localhost network-scripts]# vim ifcfg-ens33:0
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.100.100
NETMASK=255.255.255.0
4.开启路由转发功能,关闭重定向功能
[root@localhost ~]# vim /etc/sysctl.conf
[root@localhost ~]# sysctl -p
net.ipv4.ip_forward = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
5.编辑LVS脚本
[root@localhost network-scripts]# cd /etc/init.d
[root@localhost init.d]# vim dr.sh
#!/bin/bash
GW=192.168.100.1
VIP=192.168.100.100
RIP1=192.168.100.120
RIP2=192.168.100.130
case "$1" in
start)
/sbin/ipvsadm --save > /etc/sysconfig/ipvsadm
systemctl start ipvsadm
/sbin/ifconfig ens33:0 $VIP broadcast $VIP netmask 255.255.255.255 broadcast $VIP up
/sbin/route add -host $VIP dev ens33:0
/sbin/ipvsadm -A -t $VIP:80 -s rr
/sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g
/sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g
echo "ipvsadm starting ok"
;;
stop)
/sbin/ipvsadm -C
systemctl stop ipvsadm
ifconfig ens33:0 down
route del $VIP
echo "ipvsadm stoped ok"
;;
status)
if [ ! -e /var/lock/subsys/ipvsadm ];then
echo "ipvsadm stoped"
exit 1
else
echo "ipvsadm Runing ok"
fi
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
esac
exit 0
6.开启脚本与服务
[root@localhost init.d]# ifup ens33:0
[root@localhost init.d]# service dr.sh start
ipvsadm starting ok
[root@localhost init.d]# systemctl stop firewalld
[root@localhost init.d]# setenforce 0
1.开启路由转发功能
[root@localhost ~]# vim /etc/sysctl.conf
[root@localhost ~]# sysctl -p
net.ipv4.ip_forward = 1
2.设置双网卡
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp -p ifcfg-ens33 ifcfg-ens37
[root@localhost network-scripts]# vim ifcfg-ens33
IPADDR=192.168.100.1
NETMASK=255.255.255.0
[root@localhost network-scripts]# vim ifcfg-ens37
IPADDR=12.0.0.1
NETMASK=255.255.255.0
[root@localhost network-scripts]# service network restart
Restarting network (via systemctl): [ 确定 ]
3.关闭防火墙和SEliinux
[root@localhost network-scripts]# systemctl stop firewalld
[root@localhost network-scripts]# setenforce 0
注:现网中会使用其他技术,保持服务器之间数据同步