LNMP-负载均衡

文章详细介绍了如何配置Nginx作为负载均衡器,将流量平均分发到后端的WEB服务器,减轻服务器压力。同时,通过案例展示了如何设置反向代理以实现内外网的访问。此外,还提到了负载均衡的多种调度算法,如轮询、加权轮询、IP哈希等,并讨论了会话保持和健康检查的重要性,以及如何在Nginx中添加健康检查模块。最后,文章通过PHPmyadmin的例子说明了在负载均衡环境下处理session会话的问题。
摘要由CSDN通过智能技术生成

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


负载均衡案例配置:
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.htm
    
    修改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 通过浏览器访问


负载均衡多个业务:
[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;
    }
}

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

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

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

IP PV UV  并发

会话保持怎么做的

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

LVS几种模式


当后端的其中一台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


测试加权轮询: 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负载均衡健康检查
如何在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
    [root@lb01 ~]# cd nginx-1.22.0
    [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;
    }
    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 测试session会话问题
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.7
    测试访问: 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值