13-Nginx负载均衡基本概述

Nginx负载均衡基本概述

1、为什么要使用负载均衡

当我们的Web服务器直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台Web服务器组成集群,前端使用Nginx负载均衡,将请求分散的打到我们的后端服务器集群中,实现负载的分发,那么会大大提升系统的吞吐率,请求性能,高容灾

往往我们接触的最多的是SLB(Server Load Balance)负载均衡,实现最多的也是SLB、那么SLB它的调度节点和服务节点通常是在一个地域里面,那么它在这个小的逻辑地域里面决定了他对部分服务的实时性、响应性是非常好的。

所以说当海量请求过来以后,它同样是请求调度节点,调度节点将用户的请求转发给后端对应的服务节点,服务节点处理完请求后在转发给调度节点,调度节点最后响应给用户节点,这样也能实现一个均衡的作用,那么NGINX则是一个典型的SLB

负载均衡的叫法有很多

负载均衡
负载
Load Balance
LB

公有云中的叫法

SLB阿里云负载均衡
QLB青云负载均衡
CLB腾讯云负载均衡
ULB ucloud负载均衡

常见的负载均衡软件

Nginx
Haproxy
LVS

2、四层负载和七层负载的区别

四层负载均衡数据包在底层就进行了分发,而七层负载均衡数据包则是在最顶层进行分发,由此可以看出,七层负载均衡效率没有四层负载均衡高

但七层负载均衡更贴近于服务,如:http协议就是七层协议,我们可以用Nginx可以作为会话保持,URL路径规则匹配、head头改写等等,这些是四层负载均衡无法实现的。

注意:四层负载均衡不识别域名,七层负载均衡识别域名

负载均衡能实现的应用场景一:四层负载均衡

所谓四层负载均衡指的是OSI七层模型中的传输层,那么传输层Nginx已经能支持TCP/IP的控制,所以只需要对客户端的请求进行TCP/IP协议的包转发就可以实现负载均衡,那么它的好处是性能非常快,只需要底层进行应用处理,而不需要进行一些复杂的逻辑。

负载均衡能实现的应用场景二:七层负载均衡

七层负载均衡它是在应用层,那么它可以完成很多应用方面的协议请求,比如我们说的http应用的负载均衡,他可以实现http信息的改写,头信息的改写,安全应用规则控制,URL匹配规则控制,以及转发、rewrite等等的规则,所以在应用层的服务里面,我们可以做的内容就更多,那么Nginx则是一个典型的七层负载均衡SLB

3、Nginx负载均衡配置场景

Nginx要实现负载均衡需要用到proxy_pass代理模块配置

Nginx负载均衡与Nginx代理不同地方在于,Nginx的一个location仅能代理一台服务器,而Nginx负载均衡则是将客户端请求代理转发至一组upstream虚拟服务池。

Nginx upstream虚拟配置语法

Syntax: upstream name
Default: -
Context: http

#upstream例
upstream backend {
  server backend1.example.com weight=5;
  server backend2.example.com:8080;
  server unix:/tmp/backend3;
  server backup1.example.com:8080 bakcup;
}

server {
  location / {
   proxy_pass http://backend;  #地址池的名字
  }
}

负载均衡: 流量的平均分发 用户访问负载均衡 负载将请求 平均的分发到后端WEB服务器 减轻WEB的压力
反向代理: 代理用户请求后端服务器(外网访问内网) 没有负载的功能 代理内部用户请求外网(内网访问外网

4、负载均衡案例配置:

1.WEB01配置

[root@web01 conf.d]# cat node.conf 
server {
		listen 80;
		server_name node.oldboy.com;

		location / {
		root /node/;
		index index.html;
		}
}

[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@web01 conf.d]# systemctl restart nginx
	
[root@web01 conf.d]# mkdir /node
[root@web01 conf.d]# echo web01......... > /node/index.html

修改hosts文件 浏览器访问测试:

2.WEB02配置

[root@web02 conf.d]# cat node.conf 
server {
		listen 80;
		server_name node.oldboy.com;

		location / {
		root /node/;
		index index.html;
		}
	}


	[root@web02 conf.d]# nginx -t
	nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
	nginx: configuration file /etc/nginx/nginx.conf test is successful
	[root@web02 conf.d]# systemctl restart nginx

	[root@web02 conf.d]# mkdir /node
	[root@web02 conf.d]# echo web02........ > /node/index.html


	修改hosts文件 浏览器访问测试

3.配置负载均衡 10.0.0.5

	[root@lb01 conf.d]# cat node.conf 
	upstream node {
		server  172.16.1.7;
		server  172.16.1.8:80;
	}
	server {
		listen 80;
		server_name node.oldboy.com;
		
		location / {
		proxy_pass http://node;
		include proxy_params;
		}
	}

	[root@lb01 conf.d]# nginx -t
	nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
	nginx: configuration file /etc/nginx/nginx.conf test is successful
	[root@lb01 conf.d]# systemctl restart nginx


	修改hosts文件解析node.oldboy.com 到10.0.0.5 通过浏览器访问

负载均衡常见典型故障

如果后台服务连接超时,Nginx是本身是有机制的,如果出现一个节点down掉的时候,Nginx会根据你具体负载均衡的设置,将请求转移到其他的节点上,但是如果后台服务连接没有down掉,但是返回错误异常码了如:504、502、500、这个时候你需要加一个负载均衡的设置,如下:proxy_next_upstream http_500 | http_502 | http_503 | http_504 | http_404;意思是,当其中一台返回错误码404,500等错误时,可以分配到下一台服务器程序继续处理,提高平台访问成功率

server {
    listen 80;
    server_name www.oldboyedu.com;
    
    location / {
        proxy_pass http://node;
        proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
    }
}

负载均衡多个业务:

[root@lb01 conf.d]# cat *.conf
upstream phpshe {
	server  172.16.1.7;
	server  172.16.1.8:80;
}
server {
	listen 80;
	server_name phpshe.oldboy.com;
	
	location / {
	proxy_pass http://phpshe;
	include proxy_params;
	proxy_next_upstream error timeout http_500 http_502 http_503;
	}
}
upstream node {
	server  172.16.1.7;
	server  172.16.1.8:80;
}
server {
	listen 80;
	server_name wordpress.oldboy.com;
	
	location / {
	proxy_pass http://node;
	include proxy_params;
	proxy_next_upstream error timeout http_500 http_502 http_503;
	}
}
upstream zh {
	server  172.16.1.7;
	server  172.16.1.8:80;
}
server {
	listen 80;
	server_name zh.oldboy.com;
	
	location / {
	proxy_pass http://zh;
	include proxy_params;
	proxy_next_upstream error timeout http_500 http_502 http_503;
	}
}

当后端的其中一台WEB出现错误代码: 500 501 502 503 504 等错误 则让请求访问下一台WEB

[root@lb01 conf.d]# cat node.conf 
upstream node {
	server  172.16.1.7;
	server  172.16.1.8:80;
}
server {
	listen 80;
	server_name wordpress.oldboy.com;
	
	location / {
	proxy_pass http://node;
	include proxy_params;
	proxy_next_upstream error timeout http_500 http_502 http_503;
	}
}

负载均衡调度算法:

负载均衡调度算法:
rr轮询:    在后端WEB服务器配置相同情况下使用默认的rr轮询
weight:     加权轮询 权重越大 访问到的几率次数越多
ip_hash:    每个请求按照来源IP进行计算 第一次访问的是WEB01 则后面这个IP不管访问多少次 都是WEB01响应
url_hash:   第一个url由WEB01响应 则后面在请求URL时 还是由WEB01进行响应
least_conn: 最少链接数 哪个WEB的连接请求数量少 就倾向于分发到哪个WEB
调度算法 概述
轮询 按时间顺序逐一分配到不同的后端服务器(默认)
weight 加权轮询,weight值越大,分配到的访问几率越高
ip_hash 每个请求按访问IP 的hash结果分配,这样来自同一IP的固定访问一个后端服务器
url_hash 按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器
least_conn 最少链接数,那个机器链接数少就分发

测试加权轮询: WEB02比WEB01多处理6个请求

[root@lb01 conf.d]# cat node.conf 
upstream node {
	server  172.16.1.7;
	server  172.16.1.8:80 weight=6;
}
server {
	listen 80;
	server_name node.oldboy.com;
	
	location / {
	proxy_pass http://node;
	include proxy_params;
	proxy_next_upstream error timeout http_500 http_502 http_503;
	}
}

ip_hash 每次请求WEB01都是请求的同一个WEB服务器 往后都是WEB01负责响应

		  如果WEB01挂掉 则会被hash到WEB02
		  如果WEB01恢复 则继续被WEB02进行响应
		  ip_hash 会造成负载不均衡
[root@lb01 conf.d]# cat node.conf 
upstream node {
	ip_hash;
	server  172.16.1.7;
	server  172.16.1.8:80;
}
server {
	listen 80;
	server_name node.oldboy.com;
	
	location / {
	proxy_pass http://node;
	include proxy_params;
	proxy_next_upstream error timeout http_500 http_502 http_503;
	}
}

Nginx负载均衡后端状态

后端Web服务器在前端Nginx负载均衡调度中的状态

状态 概述
down 当前的server暂时不参与负载均衡
backup 预留的备份服务器
max_fails 允许请求失败的次数
fail_timeout 经过max_fails失败后,服务暂停时间
max_conns 限制最大的接受连接数

测试down状态测试该Server不参与负载均衡的调度

upstream load_pass {
    #不参与任何调度,一般用于停机维护
    server 10.0.0.7:80 down;
}

测试backup以及down状态

upstream load_pass {
    server 10.0.0.7:80 down;
    server 10.0.0.8:80 backup;
    server 10.0.0.9:80 max_fils=1 fail_timeout=10s;
}

location / {
    proxy_pass http://load_pass;
    include proxy_params;
}

检测max_fails失败次数和fail_timeout多少时间内失败多少次则标记down

upstream load_pass {
    server 10.0.0.7:80;
    server 10.0.0.8:80 max_faills=2 fail_timeout=10s;
}

检测max_conns最大TCP连接数

upstream load_pass {
    server 10.0.0.7:80;
    server 10.0.0.8:80 max_conns=1
}

Nginx负载均衡健康检查

在Nginx官方模块提供的模块中,没有对负载均衡后端节点的健康检查模块,但可以使用第三方模块。

nginx_upsyteam_check_module来检测后端服务的健康状态。

如何在Nginx中增加新的功能模块:

1. 查看当前Nginx的默认的模块

Nginx -V

2.安装依赖

[root@lb01 conf.d]# yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch

3.下载nginx源码包 必须和当前安装的nginx版本相同

[root@lb01 ~]# wget http://nginx.org/download/nginx-1.22.0.tar.gz

解压:
[root@lb01 ~]# tar xf  nginx-1.22.0.tar.gz

4.下载需要安装的功能模块

[root@lb01 ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
解压:
[root@lb01 ~]# unzip master.zip

5. 编译Nginx

企业找nginx相关信息的时候会可以用nginx -V来看源码

	将模块增加到Nginx 
[root@lb01 ~]# cd nginx-1.22.0  
# patch是解压安装的命令,-p1代表当前路径
[root@lb01 nginx-1.22.0]# patch -p1 < ../nginx_upstream_check_module-master/check_1.20.1+.patch 
[root@lb01 nginx-1.22.0]# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --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  --add-module=/root/nginx_upstream_check_module-master --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'

	# 最后执行make 编译安装
[root@lb01 nginx-1.22.0]# make && make install

6.使用新增加的健康状态检查模块

	[root@lb01 conf.d]# cat node.conf 
	upstream node {
		server  172.16.1.7 max_fails=2 fail_timeout=10s;
		server  172.16.1.8 max_fails=2 fail_timeout=10s;
		check interval=3000 rise=2 fall=3 timeout=1000 type=tcp;
    # interval 检测间隔时间,单位为毫秒
    # rise 表示请求2次正常,标记此后端的状态为up
    # fall 表示请求3次失败,标记此后端的状态为down
    # type 类型为tcp
    # timeout 超时时间,单位为毫秒
	}
	server {
		listen 80;
		server_name node.oldboy.com;
		
		location / {
		proxy_pass http://node;
		include proxy_params;
		}
		
		location /check {
		check_status;
		}
	}

企业编译安装:

没有用YUM安装 直接使用编译安装:
1. 下载源码包
2. 解压进入源码包
3. ./configure --路径... -模块--
4. make && make install	

增加模块: 之前可能YUM也可能是源码安装

1. 下载和YUM安装对应的源码包
2. 解压进入源码包
3. 将功能模块添加到nginx
4. ./configure 增加模块的位置 --add-module=模块的位置
5. make && make install	

在WEB服务器安装PHPmyadmin 测试seesion会话问题

在WEB服务器安装PHPmyadmin 测试seesion会话问题
WEB01配置:
	[root@web01 conf.d]# cat phpmyadmin.conf 
	server {
		listen 80;
		server_name phpmyadmin.oldboy.com;

		location / {
		root /code/phpmyadmin/;
		index index.php;
		}
		location ~ \.php$ {
		root /code/phpmyadmin/;
		fastcgi_pass 127.0.0.1:9000;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		include fastcgi_params;
		}


	}
	[root@web01 conf.d]# mkdir /code/phpmyadmin
	[root@web01 conf.d]# cd /code/phpmyadmin
	
	下载phpmyadmin代码 软件官网:https://www.phpmyadmin.net/
	[root@web01 phpmyadmin]# wget https://files.phpmyadmin.net/phpMyAdmin/4.8.4/phpMyAdmin-4.8.4-all-languages.zip --no-check-certificate


	解压代码:
	[root@web01 phpmyadmin]# unzip phpMyAdmin-4.8.4-all-languages.zip 
	[root@web01 phpmyadmin]# mv phpMyAdmin-4.8.4-all-languages/* .


	编辑连接数据的配置文件:
	[root@web01 phpmyadmin]# cp config.sample.inc.php config.inc.php
	[root@web01 phpmyadmin]# vim config.inc.php 
	[root@web01 phpmyadmin]# grep host config.inc.php
	$cfg['Servers'][$i]['host'] = '172.16.1.51';			# 管理后端的51数据库
	
	hosts解析到10.0.0.5
	测试访问: phpmyadmin.oldboy.com 

WEB02配置:
	[root@web02 conf.d]# cat phpmyadmin.conf 
	server {
		listen 80;
		server_name phpmyadmin.oldboy.com;

		location / {
		root /code/phpmyadmin/;
		index index.php;
		}
		location ~ \.php$ {
		root /code/phpmyadmin/;
		fastcgi_pass 127.0.0.1:9000;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		include fastcgi_params;
		}


	}
	[root@web02 phpmyadmin]# nginx -t
	nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
	nginx: configuration file /etc/nginx/nginx.conf test is successful
	[root@web02 phpmyadmin]# systemctl restart nginx

	[root@web02 conf.d]# mkdir /code/phpmyadmin

	打包WEB01的代码:
	[root@web01 phpmyadmin]# tar zcvf all.tar.gz ./*
	[root@web01 phpmyadmin]# scp all.tar.gz 10.0.0.8:/code/phpmyadmin/
	
	WEB02直接解压:
	[root@web02 phpmyadmin]# tar xf all.tar.gz
	
	修改hosts phpmyadmin 对应IP 10.0.0.8

加入负载均衡 LB01
[root@lb01 conf.d]# cat phpmyadmin.conf 
upstream admin {
	server  172.16.1.7;
	server  172.16.1.8:80;
}
server {
	listen 80;
	server_name phpmyadmin.oldboy.com;
	
	location / {
	proxy_pass http://admin;
	include proxy_params;
	proxy_next_upstream error timeout http_500 http_502 http_503;
	}
}

修改hosts文件指定10.0.0.5
浏览器测试: phpmyadmin.oldboy.com  无法正常登陆 session存储到WEB本地

解决方法: 将session做会话共享 存储到redis

作业:

作业:
1. 负载均衡 将wordpress zh phpshe 通过负载访问后端
2. 负载均衡调度算法 面试点
3. 后端状态 了解
4. 编译安装
5. 部署phpmyadmin项目
6. 扩展将会话写入到redis

面试点:

HTTP请求:
访问域名的流程
1.DNS解析
2.TCP三次握手
3.HTTP请求
4.HTTP请求静态 Nginx直接处理  动态转发给PHP 请求后端数据库
5.TCP四次挥手

负载均衡用的什么方式转发的
用的upstream模块

负载均衡用的调度算法
rr轮询

IP PV UV  并发

会话保持怎么做的

Nginx优化内容
Nginx配置相关: Nginx进程数量 内核数量一致

LVS几种模式
posted @ 2023-03-31 15:46  猛踢瘸子nei条好腿  阅读(60)  评论(0)    收藏  举报