什么是集群
•一组通过高速网络互联的计算组,并以单一系统的模式加以管理
•将很多服务器集中起来一起,提供同一种服务,在客户端看来就象是只有一个服务器
•可以在付出较低成本的情况下获得在性能、可靠性、灵活性方面的相对较高的收益
•任务调度是集群系统中的核心技术
集群目的
•提高性能
— 如计算密集型应用,如:天气预报、核试验模拟
•降低成本
— 相对百万美元级的超级计算机,价格便宜
•提高可扩展性
— 只要增加集群节点即可
•增强可靠性
— 多个节点完成相同功能,避免单点失败
集群分类
•高性能计算集群HPC
— 通过以集群开发的并行应用程序,解决复杂的科学问题
•负载均衡(LB)集群
— 客户端负载在计算机集群中尽可能平均分摊
•高可用(HA)集群
— 避免单点故障,当一个系统发生故障时,可以快速迁移
LVS集群组成
•前端:负载均衡层
— 由一台或多台负载调度器构成
•中间:服务器群组层
— 由一组实际运行应用服务的服务器组成
•底端:数据共享存储层
— 提供共享存储空间的存储区域
LVS术语
• Director Server:调度服务器
— 将负载分发到Real Server的服务器
• VIP:虚拟IP地址
— 公布给用户访问的虚拟IP地址
• DIP:调度器连接节点服务器的IP地址
• Real Server:真实服务器
— 真正提供应用服务的服务器
• RIP:真实IP地址
— 集群节点上使用的IP地址
LVS工作模式
①NAT模式
地址转换
•Network Address Translation,简称NAT模式
•类似于防火墙的私有网络结构,负载调度器作为所有服务器节点的网关,即作为客户 机的访问入口,也是各节点回应客户机的访问出口
•服务器节点使用私有IP地址,与负载调度器位于同一个物理网络,安全性要优于其他两 种方式
②TUN模式
IP隧道
•IP Tunnel,简称TUN模式
•采用开放式的网络结构,负载调度器仅作为客户机的访问入口,各节点通过各自的Internet连接直接回应客户机,而不再经过负载调度器
•服务器节点分散在互联网中的不同位置,具有独立的公网IP地址,通过专用IP隧道与负载调度器相互通信
③DR模式
直接路由
•Direct Routing,简称DR模式
•采用半开放式的网络结构,与TUN模式的结构类似,但各节点并不是分散在各地,而是与调度器位于同一个物理网络
•负载调度器与各节点服务器通过本地网络连接,不需要建立专用的IP隧道
LVS的负载调度算法
•轮询(Round Robin)
—将收到的访问请求按照顺序轮流分配给群集中的各节点 (真实服务器)
—均等地对待每台服务器,而不管服务器实际的连接数和系统负载。
•加权轮询(Weighted Round Robin)
—根据调度器设置的权重值来分发请求
—权重值高的节点优先获得任务并且分配的请求越多
—这样可以保证性能高的节点承担更 多请求。
•最少连接(Least Connections)
—根据真实服务器已建立的连接数进行分配
—将收到的访问请求优先分配给连接数最少的节点
—如果所有的服务器节点性能相近,采用这种方式可以更好的均衡负载
•加权最少连接(Weighted Least Connections)
—在服务器节点的性能差异较大的 情况下,调度器可以根据节点服务器负载自动调整 权重
—权重较高的节点将承担更 大比例的活动连接负载。
•IP_Hash
—根据请求来源的IP地址进行Hash计算,得到后端服务器
—这样来自同一个IP的请求总是会落到同一台服务器上处理,以致于可以将请求上下文信息存储在这个服务器上
•url_hash
—按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器
—后端服务器为缓存时比较有效
•fair
—采用的不是内建负载均衡使用的轮换的均衡算法
—而是可以根据页面大小、加载时间长短智能的进行负载均衡
—也就是根据后端服务器时间来分配用户请求,响应时间短的优先分配
ipvsadm 工具
| 选项 | 解释 |
|---|---|
| -A | 添加虚拟服务器 |
| -D | 删除整个虚拟服务器 |
| -s | 指定负载调度算法(轮询:rr、加权轮询:wrr、最少连接:lc、加权最少连接: wlc) |
| -a | 表示添加真实服务器(节点服务器) |
| -d | 删除某一个节点 |
| -t | 指定VIP地址及TCP端口 |
| -r | 指定RIP地址及TCP端口 |
| -m | 表示使用NAT群集模式 |
| -g | 表示使用DR模式 |
| -i | 表示使用TUN模式 |
| -w | 设置权重(权重为0时表示暂停节点) |
| -p 60 | 表示保持长连接60秒 |
| -l | 列表查看LVS虚拟服务器〔默认为查看所有) |
| -n | 以数字形式显示地址、端口等信息,常与"-"选项组合使用。ipvsadm -ln |
NAT 模式 LVS负载均衡部署
负载调度器:配置双网卡 内网:192.168.64.10(ens33) 外网卡:12.0.0.1(ens36)
二台WEB服务器集群池:192.168.64.20、192.168.64.30
一台NFS共享服务器:192.168.64.40
客户端:12.0.0.100
NFS共享服务器:192.168.64.40 #关闭防火墙 [root@localhost ~]# systemctl stop firewalld [root@localhost ~]# setenforce 0 #安装nfs服务 [root@localhost ~]# yum install -y nfs-utils rpcbind #新建目录,并创建共享文件 [root@localhost ~]# cd /opt/ [root@localhost opt]# mkdir wmc lxy [root@localhost opt]# echo 'this is wmc' >wmc/index.html [root@localhost opt]# echo 'this is lxy' >lxy/index.html #开启服务 [root@localhost opt]# systemctl start nfs [root@localhost opt]# systemctl start rpcbind #授权 [root@localhost opt]# chmod 777 wmc/ lxy/ #设置共享策略并重启服务 [root@localhost opt]# vim /etc/exports /opt/wmc 192.168.64.0/24(rw,sync) /opt/lxy 192.168.64.0/24(rw,sync) [root@localhost opt]# systemctl restart nfs [root@localhost opt]# exportfs -rv exporting 192.168.64.0/24:/opt/lxy exporting 192.168.64.0/24:/opt/wmc
节点web服务器一:192.168.64.20 #关闭防火墙 [root@localhost ~]# systemctl stop firewalld.service [root@localhost ~]# setenforce 0 #安装httpd [root@localhost ~]# yum install -y httpd #查看nfs服务 [root@localhost ~]# showmount -e 192.168.64.40 Export list for 192.168.64.40: /opt/lxy 192.168.64.0/24 /opt/wmc 192.168.64.0/24 #挂载 [root@localhost ~]# mount 192.168.64.40:/opt/wmc /var/www/html [root@localhost ~]# cat /var/www/html/index.html this is wmc #开启httpd服务 [root@localhost ~]# systemctl start httpd #指定网关并重启服务 [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 GATEWAY=192.168.64.10 #DNS1=8.8.8.8 [root@localhost ~]# systemctl restart network
节点web服务器二:192.168.64.30 #关闭防火墙 [root@localhost ~]# systemctl stop firewalld.service [root@localhost ~]# setenforce 0 #安装httpd [root@localhost ~]# yum install -y httpd #查看nfs服务 [root@localhost ~]# showmount -e 192.168.64.40 Export list for 192.168.64.40: /opt/lxy 192.168.64.0/24 /opt/wmc 192.168.64.0/24 #挂载 [root@localhost ~]# mount 192.168.64.40:/opt/lxy /var/www/html [root@localhost ~]# cat /var/www/html/index.html this is lxy #开启httpd服务 [root@localhost ~]# systemctl start httpd #指定网关并重启服务 [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 GATEWAY=192.168.64.10 #DNS1=8.8.8.8 [root@localhost ~]# systemctl restart network
负载调度器:内网:192.168.64.10(ens33) 外网:12.0.0.1(ens36) #关闭防火墙 [root@localhost ~]# systemctl stop firewalld.service [root@localhost ~]# setenforce 0 #安装ipvsadm [root@localhost ~]# yum install -y ipvsadm #添加网卡ens36,配置网卡,重启网络 [root@localhost ~]# cd /etc/sysconfig/network-scripts/ [root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens36 [root@localhost network-scripts]# vim ifcfg-ens36 去掉网关、删除UUID、修改IP为12.0.0.1 [root@localhost network-scripts]# vim ifcfg-ens33 去掉网关 [root@localhost network-scripts]# systemctl restart network [root@localhost network-scripts]# ifconfig #打开路由转发功能 [root@localhost ~]# vim /etc/sysctl.conf net.ipv4.ip_forward = 1 [root@localhost ~]# sysctl -p #防火墙策略 [root@localhost ~]# iptables -nL -t net #查看策略 [root@localhost ~]# iptables -F #清空策略 [root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.64.0/24 -o ens36 -j SNAT --to 12.0.0.1 #添加策略 [root@localhost ~]# iptables -nL -t nat #查看策略 #加载LVS内核模块 [root@localhost ~]# modprobe ip_vs [root@localhost ~]# cat /proc/net/ip_vs #开启ipvsadm服务 [root@localhost ~]# systemctl start ipvsadm.service #######制定调度任务 [root@localhost ~]# ipvsadm -C#清空策略 #指定IP地址 外网的入口 -s rr 轮询 [root@localhost ~]# ipvsadm -A -t 12.0.0.1:80 -s rr #先指定虚拟服务器再添加真实服务器地址,-r:真实服务器地址 -m指定nat模式 [root@localhost ~]# ipvsadm -a -t 12.0.0.1:80 -r 192.168.64.20:80 -m [root@localhost ~]# ipvsadm -a -t 12.0.0.1:80 -r 192.168.64.30:80 -m [root@localhost ~]# ipvsadm #开启服务 [root@localhost ~]# ipvsadm -ln #查看策略 [root@localhost ~]# ipvsadm-save -n >/etc/sysconfig/ipvsadm #永久保存规则



