LVS基于NAT模式搭建负载均衡群集

LVS的基本架构图

LVS基于NAT模式搭建负载均衡群集

负载均衡群集中,包括三个层次的组件:

1、第一层,负载调度器(BL)

前段至少有一个负载调度器(Load Balancer 或称为Director)负责响应并分发来自客户端的访问请求。

这是访问整个集群系统的唯一入口,对外使用所有服务器共有的VIP(Virtual IP,虚拟IP)地址,也称为群集IP地址,通常会配置主、备两台调度器实现热备份,当主机调度器失效以后平滑替换至备用调度器,确保高可用性。

2、第二层,服务器池(Real Server)

后端由大量真实服务器(Real Server)构成服务器池(Server Pool),提供实际的应用服务,整个群集的伸缩性通过增加、删除服务器节点来完成,而这些过程对客户机是透明的。

群集所提供的应用服务(如HTTP、FTP)由服务器池承担,其中的每个节点具有独立的RIP(Real IP,真实IP)地址,只处理调度器分发过来的客户机请求。当某个节点暂时失效时,负载调度器的容错机制会将其隔离,等待错误排除以后再重新纳入服务器池。

3、第三层,共享储存(NFS)

为了保持服务的一致性,所有节点使用共享储存设备。

为了服务器池中的所有节点提供稳定、一致的文件存取服务,确保整个集群的统一性。在Linux/UNIX环境中,共享储存可以使用NAS设备,或者提供NFS(Network File System,网络文件系统)共享服务的专用服务器。

实验环境:

LVS调度器(BL)作为Web1、Web2两台Web服务器池的网关

BL两块网卡,分别连接内外网   (本次实验是单节点调度的,若要高可用请添加多台备用调度器+keepalive实现,本次实验只是做负载均衡)

BL主机(负载调度器):

外网地址:192.168.10.20/24,同时也作为整个群集的VIP

内网地址:192.168.1.11/24

Web1主机:192.168.1.22/24

Web2主机:192.168.1.33/24

NFS主机:192.168.1.44/24

使用轮询(rr)调度算法

主机名称 内网IP地址 外网IP地址 部署软件
LVS-master(BL主机) 192.168.1.11 192.168.10.20 ipvsadm
NFS 192.168.1.22 nfs、rpcbind
WEB1 192.168.1.33 httpd、nfs-utils
WEB2 192.168.1.44 httpd、nfs-utils

LVS基于NAT模式搭建负载均衡群集

LVS的NAT负载均衡模式原理:

1、PC机直接访问BL服务器的VIP:192.168.10.20

2、BL服务器接收到请求后,通过自身调度算法选择一台下面的WEB服务器,并将目标地址为某一WEB服务器的IP,源地址不变。

3、WEB服务器接受到请求后,将数据包解封并返回数据给BL,返回的数据包中,源IP为WEB自身IP,目标IP为PC机IP。

4、BL服务器接收到WEB响应的数据包后,将数据包的源IP改为自身IP,目标IP不变,发给PC机

整个过程PC机并不知道WEB服务器的存在。

 

开始搭建

1、配置NAS(nfs)共享储存服务器

 [root@nfs ~]# yum -y install nfs-utils rpcbind    #下载NFS rpcbind
[root@nfs ~]# mkdir /opt/www
[root@nfs ~]# vim /etc/exports #设置共享目录,将/opt/www作为共享目录,要求192.168.2.0/24网段只读权限访问网页数据(若增加读写速度和安全性,可结合raid5+LVM)
/opt/www 192.168.1.0/(ro,sync,no_root_squash)
[root@nfs ~]# /etc/init.d/rpcbind start
正在启动 rpcbind: [确定]
[root@nfs ~]# /etc/init.d/nfs start
启动 NFS 服务: [确定]
启动 NFS mountd: [确定]
启动 NFS 守护进程: [确定]
正在启动 RPC idmapd: [确定]
[root@nfs ~]# showmount -e 192.168.1.22 #查看nfs挂载情况
Export list for 192.168.1.22:
/opt/www 192.168.1.0/
[root@nfs ~]# chkconfig rpcbind on #设置开机启动
[root@nfs ~]# chkconfig nfs on #设置开机启动
[root@nfs ~]# echo "<h1>it works</h1>">/opt/www/index.html #编辑共享文件内容

2、配置WEB1、WEB2(操作相同)

 [root@WEB1 ~]#yum -y install httpd nfs-utils                   #下载Apache、nfs服务
[root@WEB1 ~]#sed -i '/#S/ s/#//' /etc/httpd/conf/httpd.conf #启用apache的域名服务
[root@WEB1 ~]#sed -n '/com:80/p' /etc/httpd/conf/httpd.conf
ServerName www.example.com:
[root@WEB1 ~]# /etc/init.d/httpd start
正在启动 httpd: [确定] [root@WEB1 ~]# vim /etc/fstab #编辑fstab文件,写入nfs服务器的挂载目录,使其永久生效。
……
192.168.1.22:/opt/www/ /var/www/html/ nfs defaults,_netdev
#挂载点IP:挂载点目录 #挂载到的目录 [root@WEB1 ~]# mount -a #挂载所以可挂载点
[root@WEB1 ~]# df -hT #查看挂载情况
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vg_www-lv_root ext4 37G .3G 34G % /
/dev/sda1 ext4 485M 33M 427M % /boot
tmpfs tmpfs 242M 242M % /dev/shm
192.168.1.22:/opt/www/ nfs 37G 1.3G 34G 4% /var/www/html

3、PC机访问WEB服务器测试

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZoAAACtCAYAAACJDrnCAAAY9ElEQVR4nO3dDXQU5b0G8IdL1GgNH0ogCZEgRkEQiF7TBBNFFDW2IQbugaCxpfV6Ragt1baCUm45h0bBj6u9p0TTU0/lXnMlYkmMVLAqGAmagHwLWoiQQBICRAKESpRt9+5/ktnMzs7uzibzxiQ+P8/KMjM772yAffb9nD7Nzc1uEBERKfIv3/QFEBFR7xbxTV8AETnL7WYjBXUvHQ6ac+fO4cSJEzh27Bi++uor7ffNzc3avqioKJx33nm44IILMHjwYFxyySXa74nIeeZgYdBQd9Mn3D4aCZMDBw7g6NGjiImJQWxsLC666CKcf/756Nevn3bM6dOn8fXXX+PLL7/EkSNH0NDQgCFDhmDEiBFaCBGRM/RQMf9K1J3YDpqzZ8+iqqoKx48fx9ixY7WAkXCxQ0JHAmf37t2Ijo5GYmIiLrzwwk5dONG3nYSKPOSL3eeff661LBB1R7aCRmove/bs0WokEhJ2A8ZMAkfCSmpESUlJWpMaEYXPWIPZvn07xo8fz9YC6rZC9tEcOnRIC4e0tDQMHDiwU4VJQI0ePVqrDW3atEkLrWHDhnXqnETfVnqNRmoyDBnqzoIOb5aQqa+vx+23397pkDGSc912223auaUMIuoY9slQTxAwaKS5TK/JdLSpLBgZkZaSkqKVIaPXiCg8DBnqKSyDRjr+pU8mPT1dCwRVvvOd72hl7NixQyuTiELTm8z050TdnWXQSC3jiiuuwIABA5RfgJQhZUmZRETU+/gFjcyTkSHM0lHfVaQsKVOf8Ekdc/LkSa2pUx7ynIioO/AbdSbj8WWeTDgz+eVD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FoCEiIqUYNEREpBSDhoiIlGLQEBGRUgwaIiJSikFDRERKMWiIiEip/wdktpV/60poMgAAAABJRU5ErkJggg==" alt="" />

LVS基于NAT模式搭建负载均衡群集

 4、配置LVS服务器(BL)

 ()加载ip_vs模块,安装ipvsadm工具
[root@bl ~]# modprobe ip_vs
[root@bl ~]# cat /proc/net/ip_vs
IP Virtual Server version 1.2. (size=)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@bl ~]# yum -y install ipvsadm ()配置负载调度器SNAT转发规则
[root@bl ~]# /etc/init.d/ipvsadm stop
ipvsadm: Clearing the current IPVS table: [确定]
ipvsadm: Unloading modules: [确定]
[root@bl ~]# ipvsadm -A -t 192.168.10.20: -s rr
[root@bl ~]# ipvsadm -a -t 192.168.10.20: -r 192.168.1.33: -m -w
[root@bl ~]# ipvsadm -a -t 192.168.10.20: -r 192.168.1.44: -m -w
[root@bl ~]# ipvsadm -L -n
IP Virtual Server version 1.2. (size=)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.10.20: rr
-> 192.168.1.33: Masq
-> 192.168.1.44: Masq
[root@bl ~]#ipvsadm -S >> /etc/sysconfig/ipvsadm
ipvsadm: Saving IPVS table to /etc/sysconfig/ipvsadm: [确定]
[root@bl ~]# chkconfig ipvsadm on ()开启路由转发功能
[root@bl ~]#echo "net.ipv4.ip_forward=1
net.ipv4.conf.all.rp_filter=
net.ipv4.conf.default.rp_filter= " > /etc/sysctl.conf [root@bl ~]# sysctl -p
net.ipv4.ip_forward =
net.ipv4.conf.all.rp_filter =
net.ipv4.conf.default.rp_filter =

5、PC机访问VIP测试

LVS基于NAT模式搭建负载均衡群集

(若不成功,检查各个服务器的防火墙与selinux机制是否关闭)多刷新几次

多次CTRL+f5刷新后查看

  [root@bl ~]#ipvsadm -L -n -c
IPVS connection entries
pro expire state source virtual destination
TCP : TIME_WAIT 192.168.10.10: 192.168.10.20: 192.168.1.33:
TCP : TIME_WAIT 192.168.10.10: 192.168.10.20: 192.168.1.33:
TCP : TIME_WAIT 192.168.10.10: 192.168.10.20: 192.168.1.44:
TCP : TIME_WAIT 192.168.10.10: 192.168.10.20: 192.168.1.44:
TCP : TIME_WAIT 192.168.10.10: 192.168.10.20: 192.168.1.44:
TCP : TIME_WAIT 192.168.10.10: 192.168.10.20: 192.168.1.33:
TCP : TIME_WAIT 192.168.10.10: 192.168.10.20: 192.168.1.33:
TCP : TIME_WAIT 192.168.10.10: 192.168.10.20: 192.168.1.33:

之所以看到的结果并不是轮询的一次是192.168.1.33下一次是192.168.1.44,是因为负载调度器要判断一次完整TCP连接结束后才会进行轮询,也就是完成完整的三次握手与四次断开,因此若在网页上下载电影时及时刷新了网页,也不会轮询到下一台服务器导致传输中断。

Have fun!!!

上一篇:LVS DR模式搭建 keepalived lvs


下一篇:排序(5)---------高速排序(C语言实现)