一.负载均衡简介
Nginx 提供了基于 TCP 的 4 层(网络)和 7 层(应用)负载均衡,nginx的4层负载均衡其实就是端口转发。
应用型负载均衡(ALB)提供面向HTTP/HTTPS/QUIC等7层(应用)负载均衡。
4 层负载均衡配置示例:Nginx 监听在 12345 端口上的 TCP 连接,并将请求代理到名为 backend 的上游组。
7 层负载均衡配置示例:Nginx 监听在 80 端口上的 HTTP 连接,并将请求代理到名为 backend 的上游组。它还设置了一些常用的 HTTP 头部,以确保上游服务器能获取到正确的客户端信息。
二.环境准备
准备3台ECS主机,操作系统CENTOS7.4
ECS proxy | 10.0.0.7 EIP |
ECS web01 | 10.0.08 无EIP |
ECS web02 | 10.0.0.9 无EIP |
三台ECS安装nginx并配置web站点
三.配置7层nginx负载均衡
ECS proxy
四.配置4层nginx负载均衡
五.阿里云ALB负载均衡
简单说明:基于以上的环境ECS proxy的nginx停掉(systemctl stop nginx),只剩下2台web服务器,分别为web01和web02,然后使用创建阿里云ALB,ALB实例要与两台web服务器在一个地域。
1.创建实例
2.实例网络类型选公网,可用区要2个,一个在创建ECS实例时创建过(用这个公网IP验证试验),再随便创建一个。
3.ALB实例公网IP 实例---实例详情中可以看到内网web服务器对应的公网IP
4.创建监听 即监听ALB公网IP的应用和端口,这里是http 80端口
5.创建服务器组,即ALB后端的web01-02服务器,放到一个组里面,就是对组里面的服务器进行负载均衡的,web服务器端口也是这个过程填写。
发现还要再来一遍,哈哈哈。
最后提交。
6.浏览器使用内网服务器所在ALB实例的交换机的公网IP访问验证
六.阿里云NLB负载均衡 只展示与ALB不一样的部分
做ssh转发的话,后端服务器就一对一,不要组了,不然会2台服务器每次连接可能不一样,可以做2个不同的端口映射。
如NLB的公网IP+5555对应web01内网IP的22,NLB的公网IP+6666对应web02内网IP的22。