[toc]
nginx的七层负载均衡概述 负载均衡简介 为什么使用负载均衡
解决web服务器的单点故障
让web服务器构成一个集群
将请求平均下发给后端的web服务器
负载均衡的叫法
load balance(LB)
server load balance(SLB)
公有云负载均衡
阿里云 SLB 阿里云负载均衡(Server Load Balancer,简称SLB)
腾讯云 CLB
青云 LB
AWS ELB
负载均衡的软件和硬件
七层和四层的区别
一个七层 传输层,一个是七层,应用层
四层比七层快
四层无法识别域名,七层可以识别域名
nginx七层负载实现场景
nginx要实现负载均衡依赖一个proxy_pass代理模块
Nginx负载均衡和Nginx代理不同的地方:
nginx的代理location层仅只能代理一台服务器
nginx的负载均衡可以代理多台服务器
负载均衡的语法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 Syntax: upstream name { ... } Default: - Context: http upstream backend { 模块名 后端的主机 名字 (根据网站域名来起名) server backend1.example.com weight=5; server backend2.example.com:8080; server unix:/tmp/backend3; server backup1.example.com:8080 backup; } server { location / { proxy_pass http://backend; } } upstream blog.xxx.com { server 172.16.1.7:80; server 172.16.1.8:888; serber 172.16.1.9:999; } server { location { proxy_pass http://blog.xxx.com } }
配置负载均衡 环境准备
主机
WanIP
LanIP
角色
应用
lb01
10.0.0.5
172.16.1.5
负载均衡
nginx
web01
10.0.0.7
172.16.1.7
web网站
nginx php
web02
10.0.0.8
172.16.1.8
web网站
nginx php
web03
10.0.0.9
172.16.1.9
web网站
nginx php
编辑nginx的配置文件(web01 web02 web03) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 vim lb.xxx.conf server { listen 9999; server_name lb.xxx.com; root /code/lb; index index.html; } mkdir -p /code/lbecho 'web01' > /code/lb/index.htmlecho 'web02' > /code/lb/index.htmlecho 'web03' > /code/lb/index.htmlsystemctl start nginx && systemctl enable nginx nginx -t 10.0.0.7 lb.xxx.com
负载均衡配置(lb01) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 vim /etc/nginx/conf.d/lb.conf upstream lb.xxx.com{ server 172.16.1.7:9999; server 172.16.1.8:9999; } server { listen 80; server_name lb.xxx.com; location / { proxy_pass http://lb.xxx.com; include proxy_params; } } vim /etc/nginx/proxy_params proxy_set_header Host $host ; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ; proxy_connect_timeout 60s; proxy_read_timeout 60s; proxy_send_timeout 60s; proxy_buffering on; proxy_buffer_size 32k; proxy_buffers 4 128k;
负载均衡模式 轮循
加权轮循
IP hash
url hash
nginx负载均衡后端状态 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 upstream lb.xxx.com{ server 172.16.1.7:9999 down; ( server 172.16.1.8:9999; server 172.16.1.9:9999; } upstream lb.xxx.com{ server 172.16.1.7:9999 backup; server 172.16.1.8:9999; server 172.16.1.9:9999; } upstream lb.xxx.com{ server 172.16.1.7:9999 backup; server 172.16.1.8:9999 max_fails=3; server 172.16.1.9:9999 ; } upstream lb.xxx.com{ server 172.16.1.7:9999 backup; server 172.16.1.8:9999 max_fails=3 fail_timeout=60s; server 172.16.1.9:9999 ; } upstream lb.xxx.com{ server 172.16.1.7:9999 backup; server 172.16.1.8:9999 max_fails=3 fail_timeout=60s; server 172.16.1.9:9999 max_conns=1; }
nginx负载均衡健康检查 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 yum install -y pcre-devel openssl-devel https://github.com/yaoweibin/nginx_upstream_check_module systemctl stop nginx wget https://nginx.org/download/nginx-1.22.1.tar.gz wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip tar xf nginx-1.22.1.tar.gz && unzip master.zip cd ~/nginx-1.22.1/yum install -y patch patch -p1 < /root/nginx_upstream_check_module-master/check_1.20.1+.patch mkdir /app./configure --prefix=/app/nginx-1.22.0 --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/root/nginx_upstream_check_module-master make && make install vim /app/nginx-1.22.0/conf/nginx.conf user nobody; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; include /app/nginx-1.22.0/conf/conf.d/*.conf; mkdir conf.dvim /app/nginx-1.22.0/conf/conf.d/lb.xxx.com.conf upstream lb.xxx.com{ server 172.16.1.7:9999; server 172.16.1.8:9999; server 172.16.1.9:9999; check interval=3000 rise=2 fall=3 timeout =1000 type =tcp; } server { listen 80; server_name lb.xxx.com; location / { proxy_pass http://lb.xxx.com; include proxy_params; } location /check_health { check_status; } } vim /app/nginx-1.22.0/conf/proxy_params proxy_set_header Host $host ; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ; proxy_connect_timeout 60s; proxy_read_timeout 60s; proxy_send_timeout 60s; proxy_buffering on; proxy_buffer_size 32k; proxy_buffers 4 128k; /app/nginx-1.22.0/sbin/nginx 10.0.0.5 lb.xxx.com lb.xxx.com/check_health
SLB负载均衡 概述 随着互联网和数据中心的快速发展,大量的数据和信息需要在网络中传输。在这样的情况下,负载均衡(SLB)变得越来越重要。负载均衡可以提高系统的可用性、性能和可伸缩性,保证系统的稳定运行。在本篇文章中,我们将探讨负载均衡SLB的原理以及工作场景。
负载均衡SLB的概念
负载均衡(Load Balancing)是指对多台服务器或者网络设备进行分布式处理,实现请求分发和流量管理的技术。其核心思想是将请求分发到多个服务器上,以实现服务器的负载均衡,提高系统的性能和可用性。
负载均衡6(SLB)是一种特殊的负载均衡技术,它可以对网络流量进行更加细粒度的控制。具体来说,SLB可以根据客户端访问请求的特征,将请求转发到不同的服务器处理。SLB可以在内容层、网络层或者传输层进行操作,从而实现灵活且高效的负载均衡功能。
负载均衡SLB的工作原理 负载均衡SLB可以通过三种不同的方式进行实现:内容路由、硬件负载均衡器和软件负载均衡器。此外,也可以使用DNS负载均衡实现负载均衡的功能。
阿里云负载均衡SLB支持以下类型的负载均衡:
应用型负载均衡ALB(Application Load Balancer):
专门面向七层,提供超强的业务处理性能,例如HTTPS卸载能力。
单实例每秒查询数QPS(Query Per Second)可达100万次。
同时ALB提供基于内容的高级路由特性,例如基于HTTP报头、Cookie和查询字符串进行转发、重定向和重写等,是阿里云官方云原生Ingress网关。
网络型负载均衡NLB(Network Load Balancer):
面向万物互联时代推出的新一代四层负载均衡,支持超高性能和自动弹性能力,单实例可以达到1亿并发连接,用于应对高并发业务。
NLB有TCPSSL卸载、新建连接限速、多端口监听等高级特性,可用于物联网MQTTS加密卸载、抗洪峰上联等场景。
传统型负载均衡CLB(Classic Load Balancer):
支持TCP、UDP、HTTP和HTTPS协议,具备良好的四层处理能力,以及基础的七层处理能力。