基础服务篇之Nginx负载均衡(五)

简介

负载均衡分为四层负载均衡七层负载均衡

四层负载均衡是工作在七层协议的第四层-传输层,主要工作是转发。它在接收到客户端的流量以后通过修改数据包的地址信息(目标地址和端口和源地址)将流量转发到应用服务器。

七层负载均衡是工作在七层协议的第七层-应用层,主要工作是代理。它首先会与客户端建立一条完整的连接并将应用层的请求流量解析出来,再按照调度算法选择一个应用服务器,并与应用服务器建立另外一条连接将请求发送过去。

负载均衡的目的:将前端超高并发访问转发至后端多台服务器进行处理,解决单个节点压力过大,造成Web服务响应过慢,严重的情况下导致服务瘫痪,无法正常提供服务的问题。

在这里插入图片描述

七层负载均衡

前端服务器node04:192.168.202.131
后端服务器node05:192.168.202.132
后端服务器node06:192.168.202.133

前端服务器主要配置upstream和proxy_pass
upstream: 主要配置均衡池和调度方法
proxy_pass: 主要配置代理服务器IP或服务器组名字
proxy_set_header: 主要配置转发给后端服务器的host和前端客户端的真实IP

场景一:node04配置:
在这里插入图片描述用循环语句方便查看均衡情况

[root@node04 ~]# while true;do curl 192.168.202.131;sleep 1;done
      this is node06
this is node05
      this is node06
this is node05
      this is node06
this is node05
      this is node06
this is node05
      this is node06
this is node05
      this is node06
this is node05
      this is node06
this is node05
      this is node06
this is node05
^C

可以看到以上为轮询均衡

场景二:轮询加权重配置:
在这里插入图片描述只增加了权重weight参数,此方式可适用于两台后端服务器性能不一的情况,性能好的服务器可以把权重设为2,其访问结果为

[root@node04 ~]# while true;do curl 192.168.202.131;sleep 1;done
this is node05
      this is node06
this is node05
this is node05
      this is node06
this is node05
this is node05
      this is node06
this is node05
this is node05
      this is node06
this is node05
this is node05
      this is node06
this is node05
^C

场景三:为了防止客户端轮询丢失会话连接我们可以采用ip hash,它会根据IP网段进行hash,也就是说某个网段下是会固定访问某一台后端服务器,使客户端不会丢失会话保持

    upstream web {
        ip_hash;
        server 192.168.202.132 weight=2;
        server 192.168.202.133 weight=1;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://web;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            root   html;
            index  index.html index.htm;
        }

访问结果:

[root@node04 ~]# while true;do curl 192.168.202.131;sleep 1;done
this is node05
this is node05
this is node05
this is node05
this is node05
this is node05
this is node05
this is node05
this is node05

四层负载均衡

配置四层负载均衡要用到stream模块,此模块默认没有编译到nginx中,所以要额外编译
在前端服务器中编译stream模块

[root@node04 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-stream
checking for OS
 + Linux 3.10.0-957.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
checking for gcc -pipe switch ... found
....
[root@node04 nginx-1.18.0]# make && make install
make -f objs/Makefile
make[1]: Entering directory `/usr/src/nginx-1.18.0'
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/nginx.o \
	src/core/nginx.c
........

配置负载均衡文件,此时的stream模块不能写在http模块里面,而应与http模块同级,下面是配置在events模块下面

....
events {
    worker_connections  1024;
}

stream {
    upstream web {
        server 192.168.202.132:80;
        server 192.168.202.133:80;
    }
    server {
        listen 8080;##因为此时的80端口被占用所以这里用8080端口
        proxy_pass web;
    }
}
.......

重启nginx服务后查看8080端口起来后客户端访问:

[root@node04 ~]# while true;do curl 192.168.202.131:8080;sleep 1;done
this is node05
      this is node06
this is node05
      this is node06
this is node05
      this is node06
this is node05
      this is node06

端口转发应用场景,用ssh服务实验
node04配置

......
events {
    worker_connections  1024;
}

stream {
    server {
        listen 2022;
        proxy_pass 192.168.202.132:22;
    }
}
......

重启nginx服务,查看2022端口

[root@node04 ~]# /usr/local/nginx/sbin/nginx -s stop
[root@node04 ~]# /usr/local/nginx/sbin/nginx 
[root@node04 ~]# netstat -tnlp | grep 2022
tcp        0      0 0.0.0.0:2022            0.0.0.0:*               LISTEN      15931/nginx: master

此时在node06节点上ssh访问node04节点上的2022端口就会跳到node05这台机上

[root@node06 ~]# ssh root@192.168.202.131 -p 2022
The authenticity of host '[192.168.202.131]:2022 ([192.168.202.131]:2022)' can't be established.
ECDSA key fingerprint is SHA256:EJRDnytlOPXImNxWEFzlTWUzFINTYZ8DNLT2C/ci++M.
ECDSA key fingerprint is MD5:5c:96:76:b3:cb:6a:f5:bd:de:d8:65:e0:b5:a5:0b:e9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.202.131]:2022' (ECDSA) to the list of known hosts.
root@192.168.202.131's password: 
Last login: Sun May  1 18:08:36 2022 from 192.168.202.1
[root@node05 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:29:d2:b5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.202.132/24 brd 192.168.202.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::d8aa:1f62:21f3:fbf3/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever
    inet6 fe80::7c25:2724:d542:aab8/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值