RuijieUniversity——第六章LVS负载均衡群集技能展示了解群集的结构与工作模式学会配置NFS共享服务学会构建LVS负载均衡群集本章结构LVS负载均衡群集案例1:LVS-NAT群集案例2:LVS-DR群集集群技术概述LVS虚拟服务器LVS群集应用基础构建负载均衡群集NFS共享存储服务集群技术概述3-1群集的含义Cluster,集群、群集由多台主机构成,但对外只表现为一个整体群集的类型负载均衡(LoadBalancer)群集高可用(HighAvailable)群集高性能运算(HighPerformanceComputer)群集集群技术概述3-2负载均衡的结构第一层,负载调度器(LoadBalancer或Director)第二层,服务器池(ServerPool)第三层,共享存储(ShareStorage)InternetNAS存储设备备用调度器主调度器HA服务器池共享存储负载调度器群集IP地址(VIP)集群技术概述3-3负载均衡的工作模式地址转换(NAT)模式IP隧道(TUN)模式直接路由(DR)模式InternetInternetInternetNAT模式TUN模式DR模式关于LVS虚拟服务器2-1LinuxVirtualServer针对Linux内核的负载均衡解决方案1998年5月,由我国的章文嵩博士创建官方网站:[root@localhost~]#modprobeip_vs[root@localhost~]#cat/proc/net/ip_vsIPVirtualServerversion1.2.1(size=4096)ProtLocalAddress:PortSchedulerFlags-RemoteAddress:PortForwardWeightActiveConnInActConn确认内核对LVS的支持关于LVS虚拟服务器2-2LVS的负载调度算法轮询(RoundRobin)加权轮询(WeightedRoundRobin)最少连接(LeastConnections)加权最少连接(WeightedLeastConnections)……[root@localhost~]#cd/misc/cd/Packages/[root@localhostPackages]#rpm-ivhipvsadm-1.25-9.el6.i686.rpm[root@localhostPackages]#ipvsadm-vipvsadmv1.252008/5/15(compiledwithpoptandIPVSv1.2.1)安装ipvsadm管理工具使用ipvsadm工具4-1创建虚拟服务器主要操作选项-A添加虚拟服务器-t设置群集地址(VIP,VirtualIP)-s指定负载调度算法[root@localhost~]#ipvsadm-A-t172.16.16.172:80-srr使用ipvsadm工具4-2添加、删除服务器节点主要操作选项-a添加真实服务器-d删除真实服务器-r指定真实服务器(RealServer)的地址-m使用NAT模式;-g、-i分别对应DR、TUN模式-w为节点服务器设置权重,默认为1[root@localhost~]#ipvsadm-a-t172.16.16.172:80-r192.168.7.21:80-m[root@localhost~]#ipvsadm-a-t172.16.16.172:80-r192.168.7.22:80-m[root@localhost~]#ipvsadm-a-t172.16.16.172:80-r192.168.7.23:80-m[root@localhost~]#ipvsadm-a-t172.16.16.172:80-r192.168.7.24:80-m……[root@localhost~]#ipvsadm-d-r192.168.7.24:80-t172.16.16.172:80使用ipvsadm工具4-3查看群集及节点情况主要操作选项-L查看LVS群集方案-n以数字形式显示地址信息-c查看各节点的负载连接情况[root@localhost~]#ipvsadm-L-n……TCP172.16.16.172:80rr-192.168.7.21:80Masq127-192.168.7.22:80Masq139-192.168.7.23:80Masq128-192.168.7.24:80Masq146[root@localhost~]#ipvsadm-LncIPVSconnectionentriesproexpirestatesourcevirtualdestinationTCP01:51FIN_WAIT172.16.16.110:49712172.16.16.172:80192.168.7.21:80TCP01:52FIN_WAIT172.16.16.110:49720172.16.16.172:80192.168.7.22:80……使用ipvsadm工具4-4保存负载分配策略导出工具:ipvsadm-save导入工具:ipvsadm-restore系统服务:/etc/init.d/ipvsadm默认策略文件:/etc/sysconfig/ipvsadm[root@localhost~]#ipvsadm-save/etc/sysconfig/ipvsadm[root@localhost~]#cat/etc/sysconfig/ipvsadm-A-t172.16.16.172:http-srr-a-t172.16.16.172:http-r192.168.7.21:http-m-w1-a-t172.16.16.172:http-r192.168.7.22:http-m-w1-a-t172.16.16.172:http-r192.168.7.23:http-m-w1[root@localhost~]#serviceipvsadmstop[root@localhost~]#serviceipvsadmstartLVS策略的默认存放位置NFS共享存储服务2-1NetworkFileSystem,网络文件系统依赖于RPC(远端过程调用)需安装nfs-utils、portmap软件包系统服务:nfs、portmap共享配置文件:/etc/exports[root@localhost~]#yum-yinstallnfs-utilsportmap[root@localhost~]#chkconfignfson[root@localhost~]#chkconfigrpcbindonNFS共享存储服务2-2配置NFS共享修改/etc/exports文件依次启动rpcbind、nfs服务确认已发布的共享挂载NFS共享目录mount[-tnfs]服务器:/共享路径挂载点[root@localhost~]#mkdir-p/opt/[root@localhost~]#vi/etc/exports/opt/(rw,sync,no_root_squash)/var/ftp/pub192.168.4.11(ro)192.168.4.110(rw)[root@localhost~]#servicerpcbindstart[root@localhost~]#servicenfsstart[root@localhost~]#showmount-eExportlistforlocalhost.localdomain:/var/ftp/pub192.168.4.11/opt/共享路径客户机地址(权限选项)[root@localhost~]#yum-yinstallportmap[root@localhost~]#chkconfigportmapon[root@localhost~]#serviceportmapstart[root@localhost~]#showmount-e172.16.16.172Exportlistfor172.16.16.172:/var/ftp/pub192.168.4.11/opt/[root@localhost~]#mount172.16.16.172:/opt/[root@localhost~]#tail-1/etc/mtab172.16.16.172:/opt/=4,addr=172.16.16.172,clientaddr=172.16.16.17700客户机也必须支持RPC挂载NFS共享目录小结请思考:常见的服务器群集有哪几种类型?使用ipvsadm建立LVS群集的基本过程是什么?如何配置、使用NFS共享目录?构建LVS-NAT群集2-1案例环境LVS调度器作为Web服务器池的网关使用轮询(rr)调度算法Web服务器池:节点1-节点4192.168.7.21-24/24InternetNFS共享:/var/负载调度器woeth0:172.16.16.172/24eth1:192.168.7.254/24群集地址:172.16.16.172构建LVS-NAT群集2-2配置过程LVS调度器的配置:SNAT策略、LVS-NAT策略Web节点服务器的配置:httpd服务访问,并验证群集分配情况[root@localhost~]#vi/etc/sysctl.conf……net.ipv4.ip_forward=1[root@localhost~]#sysctl-p[root@localhost~]#iptables-tnat-APOSTROUTING-s192.168.7.0/24-oeth0-jSNAT--to-source172.16.16.172[root@localhost~]#serviceipvsadmstop[root@localhost~]#ipvsadm-A-t172.16.16.172:80-srr[root@localhost~]#ipvsadm-a-t172.16.16.172:80-r192.168.7.21:80-m[root@localhost~]#ipvsadm-a-t172.16.16.172:80-r192.168.7.22:80-m[root@localhost~]#ipvsadm-a-t172.16.16.172:80-r192.168.7.23:80-m[root@localhost~]#ipvsadm-a-t172.16.16.172:80-r192.168.7.24:80-m[root@localhost~]#serviceipvsadmsave[root@localhost~]#chkconfigipvsadmonSNAT地址转换策略LVS-NAT群集模式[root@localhost~]#yum-yinstallhttpd[root@localhost~]#mount192.168.7.250:/opt/[root@localhost~]#vi/var/负载均衡群集——测试网页/h1[root@localhost~]#servicehttpdstart[root@localhost~]#chkconfighttpdon各节点的网页相同[root@localhost~]#ipvsadm-LnIP