四層負載均衡


一、四層負載均衡

1.什么是四層負載均衡

所謂四層負載均衡,也就是主要通過報文中的目標地址和端口,再加上負載均衡設備設置的服務器選擇方式,決定最終選擇的內部服務器。
以常見的TCP為例,負載均衡設備在接收到第一個來自客戶端的SYN 請求時,選擇一個最佳的服務器,並對報文中目標IP地址進行修改(改為后端服務器IP),直接轉發給該服務器。TCP的連接建立,即三次握手是客戶端和服務器直接建立的,負載均衡設備只是起到一個類似路由器的轉發動作。在某些部署情況下,為保證服務器回包可以正確返回給負載均衡設備,在轉發報文的同時可能還會對報文原來的源地址進行修改。

2.應用場景

1.四層+七層來做負載均衡,四層可以保證七層的負載均衡的高可用性;
2.負載均衡可以做端口轉發
3.數據庫讀寫分離

4+7的應用場景

image

3.四層負載均衡特點

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

image

二、四層負載均衡實踐

1.環境准備

主機 ip 身份
lb04 172.16.1.3 四層負載均衡
lb01 172.16.1.5 七層負載均衡
lb02 172.16.1.6 七層負載均衡

把lb01、02上的nginx及配置文件都准備好,並且負載均衡沒問題,而且配置要一樣

2.配置四層負載均衡

1)四層負載均衡語法

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;
    }
}

2)配置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;
}
#添加一個包含文件
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;
#}

3)配置四層負載均衡

#創建目錄
[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;
    }
}

4)啟動服務

[root@lb4 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb4 ~]# systemctl start nginx

5)配置hosts訪問

10.0.0.3 linux.wp.com linux.lb.com
 
#訪問
http://linux.wp.com/

6)四層負載均衡配置日志

#四層負載均衡是沒有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服務器日志
[root@web01 ~]# tail -f /var/log/nginx/access.log
[root@web02 ~]# tail -f /var/log/nginx/access.log

三、四層負載端口轉發

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

#簡單配置
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;
    }
}

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

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

3.數據庫從庫的負載均衡

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;
    }
}


免責聲明!

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



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