群集的含义
由多台主机构成,但对外表现为一个整体,只提供一个访问入口,相当于一台大型的计算机。
横向发展:放更多的服务器,有调度分配的问题。
垂直发展:升级单机的硬件设备,提高单个服务器自身功能。
集群类型:

分布式系统
把任务分布下去,比如一个程序,分布在不同的电脑上组成,一台服务器不够,多台服务器处理同一任务。

集群与分布式区别

集群:同一个业务系统,部署在多台服务器上。集群中,每一台服务器实现的功能没有差别,数据和代码都是一样的。
分布式:一个业务被拆成多个子业务,或者本身就是不同的业务,部署在多台服务器上。分布式中,每一台服务器实现的功能是有差别的,数据和代码也是不一样的,分布式每台服务器功能加起来,才是完整的业务。
集群设计原则
可扩展性---集群的横向扩展能力
可用性---无故障时间 (SLA service level agreement)
性能---访问响应时间
容量---单位时间内的最大并发吞吐量(C10K 并发问题)
负载均衡集群
按实现方式划分


基于工作的协议层次划分
传输层(通用):DNAT 和 DPORT
传输层(通用):DNAT 和 DPORT
LVS:(中型)
nginx:stream(小型)
haproxy:mode tcp(不常用)
应用层(专用):针对特定协议,常称为 proxy server
http:nginx, httpd, haproxy(mode http), ...
fastcgi:nginx, httpd, ...
mysql:mysql-proxy, mycat... mycat:表示mysql读写分离
负载均衡集群架构

lvs是内核里面的功能,可以用以下命令,在编译lvs时,可以看见开启或者关闭哪些模块的功能
grep -i -C 10 ipvs /boot/config-3.10.0-693.el7.x86_64

并且可以看见调度算法

lvs集群中的术语
VS:代理服务器 Virtual Server,Director Server(DS), Dispatcher(调度器),Load Balancer(lvs服务器)
RS:真实服务器Real Server(lvs), upstream server(nginx), backend server(haproxy)(真实服务器)
CIP:客户机Client IP(客户机IP)
VIP:代理服务器外网ip Virtual serve IP VS外网的IP
DIP:代理服务器内网ipDirector IP VS内网的IP
RIP:真实服务器的ip Real server IP (真实IP)


lvs工作模式

lvs-nat:
文字版流程讲解:

lvs-nat:本质是多目标IP的DNAT,通过将请求报文中的目标地址和目标端口修改为某处的RS的RIP和PORT实现转发
(1)RIP和DIP应在同一个IP网络,且应使用私网地址;RS的网关要指向DIP
(2)请求报文和响应报文都必须经由lvs服务器转发,lvs服务器易于成为系统瓶颈
(3)支持端口映射,可修改请求报文的目标PORT
(4)VS必须是Linux系统,RS可以是任意OS系统
LVS调度算法
分为静态算法和动态算法
静态算法:不管后端服务器状态
1、RR:roundrobin,轮询,较常用
2、WRR:Weighted RR,加权轮询,较常用
3、SH:Source Hashing,实现session sticky,源IP地址hash;将来自于同一个IP地址的请求始终发往第一次挑中的RS,从而实现会话绑定
4、DH:Destination Hashing;目标地址哈希,第一次轮询调度至RS,后续将发往同一个目标地址的请求始终转发至第一次挑中的RS,典型使用场景是正向代理缓存场景中的负载均衡,如: Web缓存
动态算法:根据一个参考值overhead ,来看服务器是否忙
ipvsadm 工具

NAT模式,LVS负载均衡部署


第一步 四台服务器全部关闭防火墙+防护


NFS服务器上配置:
先下载nfs

然后去新建两个共享目录

主配置文件里面的内容:

随后,分别切换到bennt accp文件里面 去写一下文件内容
然后进到两台真实服务器里面centos2 centos3
真实服务器nginx httpd都可以
centos2 centos3相同 步骤:


centos2 挂载acpp到主页面里面

centos 3 将bennt挂载到主页面

可以通过showmout -e 查看共享的目录

代理服务器配置
先下载ipvasdm 服务

随后,因为代理服务器有内网跟外网,所以有两块网卡,在虚拟机里面添加一张网卡

cpens33 到ens36 ens36就是代理服务器的外网(12.0.0.1)

然后修改一下ens33 跟ens36的配置文件,此时代理服务器为网关,ip地址要跟网关一样(因为自己就是网关)
ens33里面的配置:

ens36配置
然后开启路由转发功能,如果不知道,可以用命令去查找

复制此行,然后进入到systcl.conf的 主配置文件里面

然后查看一下 是否开启

如何开启ipvsadm服务。发现有错误

如何查看 journalctl -xe


将文件导到这个目录下面 随后开启


ipvsadm -A -t 12.0.0.1 (表示新建一个代理服务器 -t表示代理服务器外网的IP地址是12.0.0.1)

这边代理服务器的内网网关是192.168.68.3 后面真实的服务器要通的话,必须要将两台网关改成代理服务器的ip地址 centos2 centos3 都要修改网关

随后两台都要重启

然后在代理服务器上curl一下 代理服务器的外网 成功
