解决高并发的“灵丹妙药”--负载均衡

在这里插入图片描述


前言

提示:这里可以添加本文要记录的大概内容:


提示:以下是本篇文章正文内容,下面案例可供参考

一、负载均衡

1.什么是负载均衡?

负载均衡:建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。

看起来还是比较晕,归纳一下,其实负载均衡就是: 将用户的访问请求均衡的分散到后端的真正提供服务的机器上

负载均衡器: 实现负载均衡功能的一个机器

load balancer --》LB
load balancing

2.为什么需要负载均衡?

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

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

3.负载均衡有什么作用?

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

比较经典的而且开源免费的:
1.nginx
2.lvs

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

示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。

二、如何配置nginx里的负载均衡功能

前期准备:

1.编译安装nginx

[root@load-balancer ~]# cat onekey_install_shediao_nginx_v10.sh
#!/bin/bash

#解决软件的依赖关系,需要安装的软件包
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ autoconf automake make psmisc net-tools lsof vim wget

#新建luogan用户和组
id  sanchuang || useradd sanchuang -s /sbin/nologin

代码如下(示例):

2.下载nginx软件

mkdir  /sanchuang99 -p
cd /sanchuang99
wget  http://nginx.org/download/nginx-1.21.1.tar.gz

#解压软件
tar xf nginx-1.21.1.tar.gz
#进入解压后的文件夹
cd nginx-1.21.1

#编译前的配置
./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

3.如果上面的编译前的配置失败,直接退出脚本

if (( $? != 0));then
  exit
fi
#编译
make -j 2
#编译安装
make  install

4.修改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

<font color=#999AAA >代码如下(

5.临时停止selinux和永久停止selinux

setenforce 0
sed  -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config

6.开机启动

chmod +x /etc/rc.d/rc.local
echo  "/usr/local/scsanchuang99/sbin/nginx" >>/etc/rc.local

7.运行安装脚本

[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

8.查看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 ~]#

配置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官网

三.负载均衡的算法:(3种)

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地址转发到同一个服务器 --》用户的会话信息需要保存的,尽量让这个客户机每次都访问相同的一台
会话信息–》登录信息,购物车里。

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]#

host 文件做域名解析

root@manager17 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
#192.168.0.1 www.baidu.com
192.168.0.17 manager17
192.168.0.18 worker-1
192.168.0.19 worker-2
192.168.0.7  worker-3
192.168.0.88  www.sc.com
[root@manager17 ~]# curl  http://www.sc.com
<html>
  <head>
    <title>sanchuang</title>
  </head>
  <body>
    <p> name: cali </p>
    <p>sex :male </p>
    <p>phoneNO:18908495097</p>
    <p>friendship:cali and su wenyang</p>
    <img src=feng.jpg>
    <a href=sc.html>sanchuang</a>
  </body>
</html>
[root@manager17 ~]#

总结

  1. 配置nginx 里面的负载均衡步骤很多,希望可以耐心地按照步骤操作,这个方面的问题在面试中也是经常被问到,所以一定要掌握这方面的原理。

  2. 对于负载均衡的算法,一般默认采用的是第一种,对于第二种的算法也是会用到的,所以说3种算法最好全部掌握。

3.如果你看到这里了,麻烦👍 + 关注哈,感谢支持,码字不易,谢谢理解.

在这里插入图片描述

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

未末0902

你的鼓励与支持是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值