国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

Nginx四層負載均衡的配置指南

發(fā)布時(shí)間:2021-08-15 18:37 來(lái)源: 閱讀:0 作者:徐中祥 欄目: 服務(wù)器 歡迎投稿:712375056

一、四層負載均衡介紹

什么是四層負載均衡

所謂四層負載均衡,也就是主要通過(guò)報文中的目標地址和端口,再加上負載均衡設備設置的服務(wù)器選擇方式,決定最終選擇的內部服務(wù)器。

以常見(jiàn)的TCP為例,負載均衡設備在接收到第一個(gè)來(lái)自客戶(hù)端的SYN 請求時(shí),選擇一個(gè)最佳的服務(wù)器,并對報文中目標IP地址進(jìn)行修改(改為后端服務(wù)器IP),直接轉發(fā)給該服務(wù)器。TCP的連接建立,即三次握手是客戶(hù)端和服務(wù)器直接建立的,負載均衡設備只是起到一個(gè)類(lèi)似路由器的轉發(fā)動(dòng)作。在某些部署情況下,為保證服務(wù)器回包可以正確返回給負載均衡設備,在轉發(fā)報文的同時(shí)可能還會(huì )對報文原來(lái)的源地址進(jìn)行修改。

應用場(chǎng)景

1.四層+七層來(lái)做負載均衡,四層可以保證七層的負載均衡的高可用性;

2.負載均衡可以做端口轉發(fā)

3.數據讀寫(xiě)分離

四層負載均衡特點(diǎn)

1.四層負載均衡僅能轉發(fā)TCP/IP協(xié)議、UDP協(xié)議、通常用來(lái)轉發(fā)端口,如:tcp/22、udp/53;

2.四層負載均衡可以用來(lái)解決七層負載均衡端口限制問(wèn)題;(七層負載均衡最大使用65535個(gè)端口號)

3.四層負載均衡可以解決七層負載均衡高可用問(wèn)題;(多臺后端七層負載均衡能同時(shí)的使用)

4.四層的轉發(fā)效率比七層的高得多,但僅支持tcp/ip協(xié)議,不支持http和https協(xié)議;

5.通常大并發(fā)場(chǎng)景通常會(huì )選擇使用在七層負載前面增加四層負載均衡。

二、四層負載均衡環(huán)境搭建

環(huán)境準備

lb4和lb02搭建Nginx

# 配置yum源
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

# 安裝Nginx
[root@lb02 ~]# yum install nginx -y
[root@lb4 ~]# yum install nginx -y

# 創(chuàng  )建用戶(hù)
[root@lb02 ~]# groupadd www -g 666 && useradd www -u 666 -g 666 -s /sbin/nologin -M
[root@lb4 ~]# groupadd www -g 666 && useradd www -u 666 -g 666 -s /sbin/nologin -M

# 配置nginx
[root@lb02 ~]# vim /etc/nginx/nginx.conf 
user  www;
[root@lb4 ~]# vim /etc/nginx/nginx.conf 
user  www;

# 啟動(dòng)Nginx
[root@lb4 ~]# systemctl start nginx && systemctl enable nginx && systemctl status nginx
[root@lb02 ~]# systemctl start nginx && systemctl enable nginx && systemctl status nginx

將lb01配置同步到lb02

[root@lb01 ~]# scp /etc/nginx/conf.d/* 172.16.1.5:/etc/nginx/conf.d/
[root@lb01 ~]# scp /etc/nginx/proxy_params 172.16.1.5:/etc/nginx/

測試lb02的負載均衡

[root@lb02 ~]# nginx -t && systemctl restart nginx

#配置hosts測試
10.0.0.5 linux.wp.com

三、配置四層負載均衡

四層負載均衡語(yǔ)法

Syntax:	stream { ... }
Default:	—
Context:	main

#示例:四層負載均衡stream模塊跟http模塊在同一級別,不能配置在http里面
stream {
    upstream backend {
        server backend1.example.com:12345 weight=5;
        server 127.0.0.1:12345            max_fails=3 fail_timeout=30s;
    }

    server {
        listen 12345;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass backend;
    }
}

配置nginx主配置文件

[root@lb4 ~]# vim /etc/nginx/nginx.conf
#注釋http層所有內容
user  www;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
#添加一個(gè)包含文件
include /etc/nginx/conf.c/*.conf;
#http {
#    include       /etc/nginx/mime.types;
#    default_type  application/octet-stream;
#    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
#                      '$status $body_bytes_sent "$http_referer" '
#                      '"$http_user_agent" "$http_x_forwarded_for"';
#    access_log  /var/log/nginx/access.log  main;
#    sendfile        on;
#    #tcp_nopush     on;
#    keepalive_timeout  65;
#    #gzip  on;
#    include /etc/nginx/conf.d/*.conf;
#}

配置四層負載均衡

#創(chuàng  )建目錄
[root@lb4 ~]# mkdir /etc/nginx/conf.c

#配置
[root@lb4 ~]# vim /etc/nginx/conf.c/linux.lb4.com.conf
stream {
    upstream lbserver {
        server 10.0.0.4:80;
        server 10.0.0.5:80;
    }

    server {
        listen 80;
        proxy_pass lbserver;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
    }
}

# 啟動(dòng)Nginx
[root@lb4 ~]# nginx -t && systemctl start nginx

# 配置hosts訪(fǎng)問(wèn)
10.0.0.6 linux.lb4.com

四層負載均衡配置日志

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

#如果需要日志則需要配置在stream下面
[root@lb4 ~]# vim /etc/nginx/conf.c/linux.lb4.com.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 lbserver {
        server 10.0.0.4:80;
        server 10.0.0.5:80;
    }

    server {
        listen 80;
        proxy_pass lbserver;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
    }
}

#查看所有web服務(wù)器日志
[root@web01 ~]# tail -f /var/log/nginx/access.log
[root@web02 ~]# tail -f /var/log/nginx/access.log

四、四層負載端口轉發(fā)

請求負載均衡的5555端口,跳轉到web01的22端口

#簡(jiǎn)單配置
stream {
	server {
        listen 5555;
        proxy_pass 172.16.1.7:22;
	}
}

#一般配置
stream {
    upstream ssh_7 {
        server 10.0.0.7:22;
    }

    server {
        listen 5555;
        proxy_pass ssh_7;
    }
}

# 測試
[D:\~]$ ssh root@10.0.0.6:5555
成功跳轉

請求負載均衡的6666端口,跳轉至172.16.1.51:3306

stream {
    upstream db_51 {
        server 172.16.1.51:3306;
    }
    
    server {
        listen 6666;
        proxy_pass db_51;
    }
}

數據庫從庫的負載均衡

stream {
    upstream dbserver {
        server 172.16.1.51:3306;
        server 172.16.1.52:3306;
        server 172.16.1.53:3306;
        server 172.16.1.54:3306;
        server 172.16.1.55:3306;
        server 172.16.1.56:3306;
    }
    
    server {
        listen 5555;
        proxy_pass dbserver;
    }
}

總結

到此這篇關(guān)于Nginx四層負載均衡配置的文章就介紹到這了,更多相關(guān)Nginx四層負載均衡內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

N
国产精品亚洲а∨天堂免| 午夜无码人妻AV大片色欲| 亚洲日韩欧美制服二区DVD| 99精品免视看| 人妻丰满AV无码中文字幕| 婷婷四虎东京热无码群交双飞视频|