文章目录

  • 负载均衡及其相关配置
  • 1.什么是负载均衡
  • 2.负载均衡的配置(采用的轮询算法,一人一下)
  • 3.负载均衡策略(权重模式)
  • 4.其他负载均衡策略(基本不会使用)


负载均衡及其相关配置

1.什么是负载均衡

nginx负载均衡后端服务器挂了 nginx负载均衡挂掉一台_服务器

背景:在用户通过Nginx访问一台服务器时,由于用户访问请求很多,可能导致服务器宕机,或者一些不确定因素导致服务器宕机。

解决办法:通过多台服务器一起处理用户的请求。而且多台服务器内容必须是一样的,多台服务器组成集群

原理:当1号服务器宕机后,nginx会访问2号服务器,2号宕机了,就访问3号服务器。但是为了避免把服务器累死,于是所有的服务器一起处理请求,这就是负载均衡

步骤

  1. 当一台服务器宕机后,Nginx会指向另外的几台服务器,去请求资源
  2. Nginx具体请求那个服务器是通过算法来决定
  3. 拿最简单的轮询算法来说,当用户发出请求后,会均匀的打到服务器上(第一次请求,打到1号服务器,第二次请求打到2号服务器,第三次请求打到3号服务器,第四次请求,打到1号服务器)

举个例子:皇帝今晚睡觉,三个王妃一人一下,第一个王妃不行,你就下线,皇帝就**其余俩个王妃

2.负载均衡的配置(采用的轮询算法,一人一下)

首先克隆俩台虚拟机。

这里主机是centos7(IP:192.168.56.137),

俩个克隆的分别是Nginx2(IP:192.168.56.136)和Nginx3(ip:192.168.56.138)

其中把Nginx2当做负载均衡器使用

nginx负载均衡后端服务器挂了 nginx负载均衡挂掉一台_负载均衡_02

工作过程,通过在网址上访问192.168.56.137(当做负载均衡器使用),他会轮询,一次跳转到192.168.56.136页面和192.168.56.138页面

**Nginx2配置:(192.168.56.136)**用作负载均衡器

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;
                                   # 这里修改了  注意upstream与server是同一个级别的
    upstream httpds{
        server 192.168.56.137:80;  #也可以设置为应用服务器类似Tomcat,写它的ip就行
        server 192.168.56.138:80;
    }

    server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass http://httpds;                # 这里修改了
            #root   html;
            #index  index.html index.htm;
        }

   
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

}

CentOS7配置(192.168.56.137):

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;


    sendfile        on;


    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;



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


        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

}

CentOS7的html页面设置

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body>
<body style="background-color: pink">
<h1>192.168.56.137</h1>
</body>
</html>

Nginx3配置(192.168.56.138)

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;


    sendfile        on;


    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;



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


        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

}

Nginx3的html页面设置

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body>
<body style="background-color: yellow">
<h1>192.168.56.138</h1>
</body>
</html>

配置html页面的原因就是为了显示有区别

运行结果:

刷新第一下跳转到192.168.56.137

nginx负载均衡后端服务器挂了 nginx负载均衡挂掉一台_nginx负载均衡后端服务器挂了_03

刷新第二下跳转到192.168.56.138

nginx负载均衡后端服务器挂了 nginx负载均衡挂掉一台_运维_04

3.负载均衡策略(权重模式)

这里由于电脑问题,只能用三台机器测试(还是上述三台机器,配置只有Nginx2(192.168.56.137)这台机器变了)

nginx负载均衡后端服务器挂了 nginx负载均衡挂掉一台_服务器_05

Nginx2配置 剩下的俩个服务器配置和上述轮询一样都不变

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    upstream httpds{
        server 192.168.56.137:80 weight=5;
        server 192.168.56.138:80 weight=1;
    }

    server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass http://httpds;             
            #root   html;
            #index  index.html index.htm;
        }

   
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

}

运行结果:

页面刷新6次,其中粉色页面出现5次,黄色页面出现1次。

nginx负载均衡后端服务器挂了 nginx负载均衡挂掉一台_负载均衡_06


nginx负载均衡后端服务器挂了 nginx负载均衡挂掉一台_服务器_07

还可以通过配置down,l来确定让谁下线,backup来设置备用服务器,当所有的服务器都宕机后,会启动备用服务器

upstream httpds{
        server 192.168.56.137:80 weight=5 down;
        server 192.168.56.138:80 weight=1;
        server 192.168.56.139:80 weight=1 backup;
    }

4.其他负载均衡策略(基本不会使用)

  • ip_hash
    根据客户端的ip地址转发同一台服务器,可以保持会话,但是很少用这种方式去保持会话,例如我们当前正在使用wifi访问,当坐火车去另一地方,此时会话就不保持了。
  • least_conn
    最少连接访问,优先访问连接最少的那一台服务器,因为连接少,可能是由于该服务器配置较低,刚开始赋予的权重较低。
  • url_hash(需要第三方插件)
    根据用户访问的url定向转发请求,不同的url转发到不同的服务器进行处理(定向流量转发)。
  • fair(需要第三方插件)
    根据后端服务器响应时间转发请求,这种方式也很少使用,因为容易造成流量倾斜,给某一台服务器压垮。