栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

使用 NGINX 实现 负载均衡

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用 NGINX 实现 负载均衡

【NGINX 负载均衡配置和原理】
  • 一、什么是负载均衡?
  • 二、为什么需要负载均衡?
    • 1.编译安装nginx
    • 2.解决软件的依赖关系,需要安装的软件包
    • 3.新建luogan用户和组
    • 4.下载nginx软件
    • 5.解压软件
    • 6.编译前的配置
    • 7.如果上面的编译前的配置失败,直接退出脚本
    • 8.开机启动
    • 9.运行安装脚本
    • 10.配置nginx里的负载均衡功能
  • 三、负载均衡的算法(方法)
    • 1.轮询
    • 2.ip_hash 基于客户端的ip地址
    • 3.least-connected 最小连接数


一、什么是负载均衡?

负载均衡: 将用户的访问请求均衡的分散到后端的真正提供服务的机器上
负载均衡器: 实现负载均衡功能的一个机器
load balancer --》LB
load balancing

二、为什么需要负载均衡?

1.能够将大量的请求比较均匀的分散到后端,不会导致某台访问量过大,某个服务又没有访问量

2.高可用(对后端的服务器进行健康检测,如果后端那台服务器出现问题,就不会再将请求转发给它,从而避免用户访问不了服务器,启动一个容错的功能)

[root@mysql-server ~]# hostnamectl  set-hostname load-balancer
[root@mysql-server ~]# su
[root@load-balancer ~]#

nginx 来实现负载均衡功能
nginx是一个web服务器,可用实现http功能,但是它也可以对http访问进行负载均衡

1.编译安装nginx
[root@load-balancer ~]# cat onekey_install_shediao_nginx_v10.sh
#!/bin/bash
2.解决软件的依赖关系,需要安装的软件包
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ autoconf automake make psmisc net-tools lsof vim wget
3.新建luogan用户和组
id  sanchuang || useradd sanchuang -s /sbin/nologin
4.下载nginx软件
mkdir  /sanchuang99 -p
cd /sanchuang99
wget  http://nginx.org/download/nginx-1.21.1.tar.gz
5.解压软件
tar xf nginx-1.21.1.tar.gz
#进入解压后的文件夹
cd nginx-1.21.1
6.编译前的配置
./configure --prefix=/usr/local/scsanchuang99  --user=sanchuang --group=sanchuang  --with-http_ssl_module   --with-threads  --with-http_v2_module  --with-http_stub_status_module  --with-stream
7.如果上面的编译前的配置失败,直接退出脚本
if (( $? != 0));then
  exit
fi
#编译
make -j 2
#编译安装
make  install

#修改PATH变量
echo  "PATH=$PATH:/usr/local/scsanchuang99/sbin" >>/root/.bashrc
#执行修改了环境变量的脚本
source /root/.bashrc


#firewalld and selinux

#stop firewall和设置下次开机不启动firewalld
service firewalld stop
systemctl disable firewalld

#临时停止selinux和永久停止selinux
setenforce 0
sed  -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config
8.开机启动
chmod +x /etc/rc.d/rc.local
echo  "/usr/local/scsanchuang99/sbin/nginx" >>/etc/rc.local
9.运行安装脚本
[root@load-balancer ~]# bash onekey_install_shediao_nginx_v10.sh
[root@load-balancer ~]# su - root  切换用户,加载修改了的PATH变量
上一次登录:二 8月 24 11:26:16 CST 2021pts/2 上
[root@load-balancer ~]# which nginx  查看nginx的命令
/usr/local/scsanchuang99/sbin/nginx

[root@load-balancer ~]# nginx  启动nginx
[root@load-balancer ~]# nginx -s stop  关闭nginx
[root@load-balancer ~]# nginx  启动nginx

[root@load-balancer ~]# ps aux|grep nginx  查看nginx的进程
root       7569  0.0  0.0  46220  1160 ?        Ss   11:28   0:00 nginx: master process nginx
sanchua+   7570  0.0  0.1  46680  1912 ?        S    11:28   0:00 nginx: worker process
root       7572  0.0  0.0 112824   980 pts/2    S+   11:29   0:00 grep --color=auto nginx
[root@load-balancer ~]# ss -anplut|grep nginx  查看nginx的端口
tcp    LISTEN     0      128       *:80                    *:*                   users:(("nginx",pid=7570,fd=6),("nginx",pid=7569,fd=6))
[root@load-balancer ~]#
10.配置nginx里的负载均衡功能
[root@load-balancer ~]# cd /usr/local/scsanchuang99/  进入nginx编译安装指定的目录
[root@load-balancer scsanchuang99]# ls
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
[root@load-balancer scsanchuang99]# cd conf/  进入配置文件的命令
[root@load-balancer conf]# ls
fastcgi.conf          fastcgi_params.default  mime.types          nginx.conf.default   uwsgi_params
fastcgi.conf.default  koi-utf                 mime.types.default  scgi_params          uwsgi_params.default
fastcgi_params        koi-win                 nginx.conf          scgi_params.default  win-utf
[root@load-balancer conf]#
  nginx.conf  是nginx的配置文件
  [root@load-balancer conf]# vim nginx.conf

upstream  上游-->实现负载均衡的指令
 proxy 代理
 pass 通过

⚠️附赠一个学习的网址哈☝️:
nginx.org

三、负载均衡的算法(方法) 1.轮询
roundrobin  --》rr  --》默认,默认情况下所有的服务器的权重值都是1 ,值越大优先级越好
      加权轮询
        server 192.168.0.17  weight=1;
        server 192.168.0.18 weight=2;
        server 192.168.0.19;
        server 192.168.0.7;
2.ip_hash 基于客户端的ip地址

ip_hash 基于客户端的ip地址做负载均衡,相同的ip地址转发到同一个服务器 --》用户的会话信息需要保存的,尽量让这个客户机每次都访问相同的一台
会话信息–》登录信息,购物车里

3.least-connected 最小连接数
http{
  
   upstream  scweb {     #定义一个负载均衡器名字叫scweb
        server 192.168.0.17:8080;
        server 192.168.0.18:8080;
        server 192.168.0.19:8080;
        server 192.168.0.7:8080;

   }
 server {
        listen       80;          #监听80端口
        server_name  www.sc.com;  #为www.sc.com 域名服务
        location / {
                proxy_pass http://scweb ;     #调用负载均衡器
        }
.....省略很多配置
}
[root@load-balancer conf]# nginx -s reload 重新加载配置文件--》相当于重启了nginx服务
[root@load-balancer conf]#

[root@load-balancer conf]# ps axu|grep nginx
root       7569  0.0  0.1  46356  2016 ?        Ss   11:28   0:00 nginx: master process nginx
sanchua+  12158  0.0  0.1  46816  2044 ?        S    11:58   0:00 nginx: worker process
root      12160  0.0  0.0 112824   980 pts/2    S+   11:59   0:00 grep --color=auto nginx
[root@load-balancer conf]#
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/642024.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号