Nginx四層負載均衡


Nginx四層負載均衡概述

什么是四層負載均衡

四層負載均衡是基於傳輸層協議包來封裝的(如:TCP/IP),那我們前面使用到的七層是指的應用層,他的組裝在四層的基礎之上,無論四層還是七層都是指的OSI網絡模型。


四層負載均衡應用場景

1、四層+七層來做負載均衡,四層可以保證七層的負載均衡的高可用性;如:nginx就無法保證自己的服務高可用,需要依賴LVS或者keepalive。

2、如:tcp協議的負載均衡,有些請求是TCP協議的(mysql、ssh),或者說這些請求只需要使用四層進行端口的轉發就可以了,所以使用四層負載均衡。


四層 + 七層構建大規模集群架構使用場景

img


四層負載均衡總結

1、四層負載均衡僅能轉發TCP/IP協議、UDP協議、通常用來轉發端口,如:tcp/22、udp/53;
2、四層負載均衡可以用來解決七層負載均衡端口限制問題;(七層負載均衡最大使用65535個端口號)
3、四層負載均衡可以解決七層負載均衡高可用問題;(多台后端七層負載均衡能同事的使用)
4、四層的轉發效率比七層的高得多,但僅支持tcp/ip協議,不支持http和https協議;
5、通常大並發場景通常會選擇使用在七層負載前面增加四層負載均衡。


nginx四層負載均衡場景實踐

Nginx如何配置四層負載均衡

1、通過訪問負載均衡的5555端口,實際是后端的web01的22端口在提供服務;

2、通過訪問負載均衡的6666端口,實際是后端的mysql的3306端口在提供服務。

先配置兩台lb負載均衡

[root@lb02 ~]# cat /etc/yum.repos.d/nginx.repo 
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

#在lb02上安裝nginx
[root@lb02 yum.repos.d]# yum install -y nginx

#在lb02上同步lb01的所有nginx相關配置
[root@lb02 ~]# scp -r root@172.16.1.5:/etc/nginx /etc/


#啟動nginx
[root@lb02 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@lb02 conf.d]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@lb02 conf.d]# nginx

1.創建存放四層負載均衡配置文件的目錄

[root@lb03 ~]# vim /etc/nginx/nginx.conf
events {
        ....
}
include /etc/nginx/conf.c/*.conf;
http {
        .....
}

[root@lb03 ~]# mkdir /etc/nginx/conf.c

2.配置四層負載均衡

[root@lb03 conf.c]# cat lb_domain.conf 
stream {
    upstream lb {
            server 172.16.1.5:80 weight=5 max_fails=3 fail_timeout=30s;
            server 172.16.1.6:80 weight=5 max_fails=3 fail_timeout=30s;
    }

    server {
            listen 80;
            proxy_connect_timeout 3s;
            proxy_timeout 3s;
            proxy_pass lb;
    }
}
[root@lb03 conf.c]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb03 conf.c]# nginx -s reload

#配置本機hosts解析后瀏覽器訪問並查看nginx日志

3.四層負載均衡開啟日志

#四層負載均衡是沒有access的日志的,因為在nginx.conf的配置中,access的日志格式是配置在http下的,而四層復雜均衡配置實在http以外的;

#如果需要日志則需要配置在stream下面
[root@web03 conf.c]# cat lb_domain.conf 
stream {
    log_format  proxy '$remote_addr $remote_port - [$time_local] $status $protocol '
                  '"$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"' ;
    access_log /var/log/nginx/proxy.log proxy;
    upstream lb {
            server 172.16.1.5:80 weight=5 max_fails=3 fail_timeout=30s;
            server 172.16.1.6:80 weight=5 max_fails=3 fail_timeout=30s;
    }

    server {
            listen 80;
            proxy_connect_timeout 3s;
            proxy_timeout 3s;
            proxy_pass lb;
    }
}

域名解析到lb03上,再次登錄

查看日志:

[root@lb03 ~]# tail -f /var/log/nginx/proxy.log

541055 - lb03 - TCP - 10.0.0.1 - 1656 - 80 - 75.856 - 200 - 28/Aug/2019:03:22:50 +0800
344557 - lb03 - TCP - 10.0.0.1 - 1658 - 80 - 75.850 - 200 - 28/Aug/2019:03:22:50 +0800
284545 - lb03 - TCP - 10.0.0.1 - 1654 - 80 - 75.957 - 200 - 28/Aug/2019:03:22:50 +0800
225560 - lb03 - TCP - 10.0.0.1 - 1659 - 80 - 75.858 - 200 - 28/Aug/2019:03:22:50 +0800

使用nginx四層負載均衡實現tcp的轉發

請求負載均衡 5555    --->     172.16.1.7:22;
請求負載均衡 6666    --->     172.16.1.51:3306;

配置nginx四層負載均衡實現tcp的轉發

[root@lb4-01 ~]# cat /etc/nginx/conf.c/lb_domain.conf 
stream {
    log_format  proxy '$remote_addr $remote_port - [$time_local] $status $protocol '
                      '"$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"' ;
    access_log /var/log/nginx/proxy.log proxy;

#定義轉發ssh的22端口
    upstream ssh_7 {
            server 10.0.0.7:22;
    }
#定義轉發mysql的3306端口
    upstream mysql_51 {
            server 10.0.0.51:3306;
    }
    server {
            listen 5555;
            proxy_connect_timeout 3s;
            proxy_timeout 300s;
            proxy_pass ssh_7;
    }

    server {
            listen 6666;
            proxy_connect_timeout 3s;
            proxy_timeout 3s;
            proxy_pass mysql_51;
    }
}

使用本地連接

ssh root@10.0.0.4 5555
會自動跳轉到端口所指定的服務器上

輸入密碼

自動連接到5555端口指定的服務器,跳轉到web01


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM