负载均衡 -02
  uM1yvXrce2fo 2023年11月02日 37 0


一、负载均衡实践

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

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

#语法
Syntax: upstream name { ... }
Default: —
Context: http

#例子
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com:8080;
server backup1.example.com:8080 backup;
server backup2.example.com:8080 backup;
}

server {
... ...
location / {
proxy_pass http://backend;
}
}
2.环境准备
 主机           IP                 主机角色              

web01 172.16.1.7 web服务端

web02 172.16.1.8 web服务端

lb01 10.0.0.5,172.16.1.5 负载均衡
3.web01准备站点
1、配置nginx
[root@web01 ~]# vim /etc/nginx/conf.d/linux12.lb.com.conf
server {
listen 80;
server_name linux12.lb.com;
charset utf8;

location / {
root /mm/lb;
index index.html;
}
}

2.准备网站
[root@web01 ~]# mkdir /mm/lb
[root@web01 ~]# echo "我是web01负载均衡lblb" > /mm/lb/index.html
[root@web01 ~]# chown -R www.www /mm/

3、配置hosts访问
[root@web01 ~]# systemctl restart nginx

10.0.0.7 linux12.lb.com

3.web01准备站点
1、配置nginx
[root@web02 ~]# vim /etc/nginx/conf.d/linux12.lb.com.conf
server {
listen 80;
server_name linux12.lb.com;
charset utf8;

location / {
root /mm/lb;
index index.html;
}
}

2、准备网站
[root@web02 ~]# mkdir /mm/lb
[root@web02 ~]# echo "我是web02负载均衡lblb"> /mm/lb/index.html
[root@web02 ~]# chown -R www.www /mm/

3、配置hosts访问
[root@web02 ~]# systemctl restart nginx

10.0.0.8 linux12 .lb.com
4.配置负载均衡配置文件
[root@lb01 ~]# vim /etc/nginx/conf.d/linux12.lb.com.conf
upstream web_group {
server 172.16.1.7:80;
server 172.16.1.8;
}

server {
listen 80;
server_name linux12.lb.com;

location / {
proxy_pass http://web_group;
include proxy_params;
}
}
5.准备代理优化文件
[root@lb01 ~]# vim /etc/nginx/proxy_params 

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 20s;
proxy_read_timeout 20s;
proxy_send_timeout 20s;
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 8 8k;
6.重启访问测试
1.重启
[root@lb01 ~]# 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 ~]# systemctl restart nginx

2.配置hosts
10.0.0.5 linux12.lb.com

负载均衡 -02_nginx

二、负载均衡结合项目

1.配置wordpress负载均衡
[root@lb01 conf.d]# vi linux12.wp.com.conf
upstream blog {
server 172.16.1.7;
server 172.16.1.8;
}

server {
listen 80;
server_name linux12.wp.com;

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

#重启
[root@lb01 ~]# systemctl restart nginx


2.配置hosts查看网站

10.0.0.5 linux12.wp.com
2.配置zh的负载均衡
[root@lb01 conf.d]# vi linux12.zh.com.conf
upstream zh {
server 172.16.1.7;
server 172.16.1.8;
}

server {
listen 80;
server_name linux12.zh.com;

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

2.配置hosts查看网站

10.0.0.5 linux12.zh.com

4.负载均衡常见问题

1、常见问题
如果后端服务器出现问题,负载均衡仍然会将请求发送给相应机器

如果后台服务连接超时,Nginx是本身是有机制的,如果出现一个节点down掉的时候,Nginx会更据你具体负载均衡的设置,将请求转移到其他的节点上,但是,如果后台服务连接没有down掉,但是返回错误异常码了如:504、502、500,这个时候你需要加一个负载均衡的设置,如下:proxy_next_upstream http_500 | http_502 | http_503 | http_504 |http_404;意思是,当其中一台返回错误码404,500...等错误时,可以分配到下一台服务器程序继续处理,提高平台访问成功率。
2、解决问题的模块
Syntax: proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | non_idempotent | off ...;
Default: proxy_next_upstream error timeout;
Context: http, server, location
3、配置
[root@lb01 ~]# vim /etc/nginx/conf.d/linux12.wp.com.conf
upstream blog {
server 172.16.1.7;
server 172.16.1.8;
}

server {
listen 80;
server_name linux12.wp.com;

location / {
proxy_pass http://blog;
include proxy_params;
#可以配置,可以写到include
proxy_next_upstream http_500 | http_502 | http_503 | http_504 | http_403 | http_404;
}
}

#重启
[root@lb01 ~]# systemctl restart nginx

三、负载均衡调度算法

# 调度算法         概述

轮询 按时间顺序逐一分配到不同的后端服务器(默认)
weight 加权轮询,weight值越大,分配到的访问几率越高
ip_hash 每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器
url_hash 按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器
least_conn 最少链接数,哪个机器链接数少就分发给它
1.轮询的配置方法
upstream blog {
server 172.16.1.7;
server 172.16.1.8;
}
2.加权轮询配置
upstream blog {
server 172.16.1.7 weight=8; #7这台机器出现8次
server 172.16.1.8 weight=2; #8这台机器出现2次
}
3.ip_bash配置方法
upstream web_group {
server 172.16.1.7:80;
server 172.16.1.8;
ip_hash;
}
#一般用来做会话保持

四、负载均衡后端状态

## 状态         概述

down 当前的server暂时不参与负载均衡
backup 预留的备份服务器
max_fails 允许请求失败的次数
fail_timeout 经过max_fails失败后, 服务暂停时间
max_conns 限制最大的接收连接数
1.down状态配置
upstream blog {
#一般情况下,维护场景使用
server 172.16.1.7 down;
server 172.16.1.8;
##注释掉也可以
2.backup状态配置
upstream web_group {
server 172.16.1.7 backup;
server 172.16.1.8;
server 172.16.1.9 backup;
}
#当一台机器挂掉后,另一台备份使用,backup是一台接一台接替!
3.访问错误状态配置
upstream web_group {
server 172.16.1.7;
server 172.16.1.8 max_fails=3 fail_timeout=20s;
}
# 最大错误数3次,第四次就去7这台机器上了;20秒后连接
4.最大连接数配置
upstream web_group {
server 172.16.1.7;
server 172.16.1.8 max_conns=200;

五、Nginx负载均衡健康检查(扩展)

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

## 负载均衡机器上安装
1.安装依赖
[root@lb02 ~]# yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch

2.下载第三方模块
[root@lb02 ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@lb02 ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip

3.解压及安装
[root@lb02 ~]# tar xf nginx-1.14.2.tar.gz
[root@lb02 ~]# unzip master.zip

4.进入nginx目录,打补丁(nginx的版本是1.14补丁就选择1.14的,p1代表在nginx目录,p0是不在nginx目录)
[root@lb02 ~]# cd nginx-1.14.2/

[root@lb02 nginx-1.14.2]# patch -p1 <../nginx_upstream_check_module-master/check_1.14.0+.patch

[root@lb02 nginx-1.14.2]# ./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 --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/root/nginx_upstream_check_module-master --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'

[root@lb02 nginx-1.14.2]# make && make install

5.在已有的负载均衡上增加健康检查的功能
[root@lb01 conf.d]# cat proxy_web.conf
upstream web {
server 172.16.1.7:80 max_fails=2 fail_timeout=10s;
server 172.16.1.8:80 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 linux12.lb.com;
location / {
proxy_pass http://web;
include proxy_params;
}

location /upstream_check {
check_status;
}
}
1

6.重启访问页面

#重启
#配置hosts
#访问
http://linux12.lb.com/upstream_check

六、Nginx负载均衡会话保持

1.什么是会话保持

我们在访问网站的时候,进行登陆以后,'web01'服务器上回生成一个'session',然后服务器会携带着'session_id'返回给浏览器记录一个'cookie'值,当第二次访问时,'cookie'会来服务器上与'session'进行对比,如果对比成功,则不需要重新登录,但是负载均衡,会进行跳转到另一台'web02'服务器,'web02'服务器并没'session',然后回再生成一个'session'在'web02',此时负载均衡起作用跳转到'web01','web01'的'session'与浏览器的'cookie'对应不上,回报错。# 在web01与web02服务器,博客还有知乎生成的对应数据库表的文件,在基于负载均衡的情况下也会出错

# 在使用负载均衡的时候会遇到会话保持的问题,可通过如下方式进行解决。
1.使用nginx的ip_hash,根据客户端的IP,将请求分配到对应的IP上
2.基于服务端的session会话共享(NFS,MySQL,memcache,redis,file)
2.session共享的方法
1.‘把多台后端服务器session文件目录挂载到NFS同一目录’
 # 把在同一虚拟服务池的web服务器产生session的文件目录,挂载到nsf的服务器的共享文件上,前提是有nfs的服务器,并且生成共享目录.

## 例:

[root@web01 ~]# mount -t nfs 172.16.1.31:/data /var/lib/php/session/
[root@web02 ~]# mount -t nfs 172.16.1.31:/data /var/lib/php/session/
2.‘通过程序将session存储到redis缓存’
  # 把产生 session 为文件的方式,转为提取到内存,
'珍贵的例子
## 数据库服务器
[root@db01 ~]# yum install -y redis 负载均衡服务器下载 redis
[root@db01 ~]# vim /etc/redis.conf 修改配置文件
bind 172.16.1.51
[root@db01 ~]# systemctl start redis 启动 redis

## web服务器
[root@web01 ~]# vim /etc/php.ini
#原配置
#session.save_handler = files
session.save_handler = redis
#;session.save_path = "/tmp"
session.save_path = "tcp://172.16.1.51:6379"
[root@web01 ~]# vim /etc/php-fpm.d/www.conf
#注释以下两行
;php_value[session.save_handler] = files
;php_value[session.save_path] = /var/lib/php/session
3.通过程序将session存储到mysql数据库 # 开发人员
3.web01上搭建phpmyadmin
1)上传包
[root@web01 ~]# cd /mm/
[root@web01 mm]# rz
[root@web01 mm]# ll
-rw-r--r--. 1 root root 11060845 Oct 18 10:48 phpMyAdmin-4.9.0.1-all-languages.zip

2、解压
并改名[root@web01 mm]# unzip phpMyAdmin-4.9.0.1-all-languages.zip
[root@web01 mm]# mv phpMyAdmin-4.9.0.1-all-languages php

3、修改连接数据库代码
[root@web01 mm]# cp /mm/php/{config.sample.inc.php,config.inc.php}
[root@web01 mm]# vim /mm/php/config.inc.php
$cfg['Servers'][$i]['host'] = '172.16.1.51';

4、授权站点目录
[root@web01 mm]# chown -R www.www /mm/

5、配置nginx
[root@web01 ~]# vim /etc/nginx/conf.d/linux12.php.com.conf
server {
listen 80;
server_name linux12.php.com;
root /mm/php;

location / {
index index.php;
}

location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

[root@web01 ~]# systemctl restart nginx

6、配置hosts访问
#配置hosts
10.0.0.7 linux12.php.com

#访问页面 http://linux12.php.com/

#报错:
Error during session start; please check your PHP and/or webserver log file and configure your PHP installation properly. Also ensure that cookies are enabled in your browser.
session_start(): open(SESSION_FILE, O_RDWR) failed: Permission denied (13)
session_start(): Failed to read session data: files (path: /var/lib/php/session)

#解决:
[root@web01 ~]# chown -R www.www /var/lib/php/session

7、再次登录测试
#登录测试,如果登录用户密码错误
mysqli_real_connect(): (HY000/1045): Access denied for user 'root'@'172.16.1.7' (using password: YES)

#数据库授权

MariaDB [(none)]> grant all on *.* to root@'172.16.1.%' identified by '123';
Query OK, 0 rows affected (0.00 sec)

#web01 测试
mysql -uroot -p123 -h172.16.1.51 成功
4.web02同步到服务器web01
1、同步站点文件
[root@web01 ~]# scp -r /mm/php 172.16.1.8:/mm/

2、同步nginx配置
[root@web01 ~]# scp /etc/nginx/conf.d/linux12.php.com.conf 172.16.1.8:/etc/nginx/conf.d/

3、授权目录
[root@web01 ~]# chown -R www.www /var/lib/php/session
[root@web02 ~]# chown -R www.www /mm/

4、访问测试
#重启
[root@web02 ~]# systemctl restart nginx

#配置hosts
linux12.php.com

#访问
http://linux12.php.com/index.php

5.配置负载均衡
1、配置
[root@lb01 ~]# vim /etc/nginx/conf.d/linux12.php.com.conf
upstream phpmyadmin {
server 172.16.1.7;
server 172.16.1.8;
}

server {
listen 80;
server_name linux12.php.com;

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

[root@lb01 ~]# 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 ~]# systemctl restart nginx

2、访问
#配置hosts
10.0.0.4 linux12.php.com

#访问报错
Failed to set session cookie. Maybe you are using HTTP instead of HTTPS to access phpMyAdmin.

6.解放决方式一:挂载session文件的目录
[root@web01 ~]# mount -t nfs 172.16.1.31:/data/wp /var/lib/php/session/
[root@web02 ~]# mount -t nfs 172.16.1.31:/data/wp /var/lib/php/session/

7.解决方式二:使用redis实现session共享
1)安装redis
[root@db01 ~]# yum install -y redis

2、配置服务
[root@db01 ~]# vim /etc/redis.conf
bind 172.16.1.51

3、启动服务
[root@db01 ~]# systemctl start redis

#检查
[root@db01 ~]# netstat -lntp
tcp 0 0 172.16.1.51:6379 0.0.0.0:* LISTEN 13305/redis-server

4、修改PHP服务将session存储到redis
[root@web01 ~]# vim /etc/php.ini
#原配置
#session.save_handler = files
session.save_handler = redis
#;session.save_path = "/tmp"
session.save_path = "tcp://172.16.1.51:6379"

[root@web01 ~]# vim /etc/php-fpm.d/www.conf
#注释以下两行
;php_value[session.save_handler] = files
;php_value[session.save_path] = /var/lib/php/session

5、重启
[root@web01 ~]# systemctl restart php-fpm

6、再次访问测试
http://linux12.php.com/index.php
#登录没有问题

7、redis验证session
1.连接进入redis
[root@db01 ~]# redis-cli -h 172.16.1.51

2.查看所有key
172.16.1.51:6379> keys *

3.查看session的生存时间
172.16.1.51:6379> TTL PHPREDIS_SESSION:43948cd72f7589982cc3758f9d5c2b8d
(integer) 1265

4.退出
172.16.1.51:6379> quit


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  ehrZuhofWJiC   2024年05月17日   34   0   0 linuxsvn
  ehrZuhofWJiC   2024年05月17日   38   0   0 KVMlinux
  ehrZuhofWJiC   2024年05月17日   36   0   0 服务器linux
uM1yvXrce2fo