四層負載均衡是基於傳輸層協議包來封裝的(如:TCP/IP),那咱們前面使用到的七層是指的應用層,他的組裝在四層 的基礎之上,不管四層仍是七層都是指的OSI網絡模型。css
一、四層+七層來作負載均衡,四層能夠保證七層的負載均衡的高可用性;如:nginx就沒法保證本身的服務高可用,需 要依賴LVS或者keepalive。 二、如:tcp協議的負載均衡,有些請求是TCP協議的(mysql、ssh),或者說這些請求只須要使用四層進行端口的轉發 就能夠了,因此使用四層負載均衡。
四層+七層構建大規模集羣架構使用場景
mysql
四層負載均衡僅能轉發TCP/IP協議、UDP協議、一般用來轉發端口,如:tcp/2二、udp/53;nginx
四層負載均衡能夠用來解決七層負載均衡端口限制問題;(七層負載均衡最大使用65535個端口號)web
四層負載均衡能夠解決七層負載均衡高可用問題;(多臺後端七層負載均衡能同事的使用)sql
四層的轉發效率比七層的高得多,但僅支持tcp/ip協議,不支持http和https協議;vim
一般大併發場景一般會選擇使用在七層負載前面增長四層負載均衡。後端
Nginx如何配置四層負載均衡
centos
一、經過訪問負載均衡的5555端口,實際是後端的web01的22端口在提供服務; 二、經過訪問負載均衡的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]# systemctl start nginx
①建立存放四層負載均衡配置文件的目錄網絡
[root@lb02 ~]# vim /etc/nginx/nginx.conf events { .... } include /etc/nginx/conf.c/*.conf; http { ..... } [root@lb02 ~]# mkdir /etc/nginx/conf.c
②配置四層負載均衡
[root@web03 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@web03 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@web03 conf.c]# systemctl reload nginx #配置本機hosts解析後瀏覽器訪問並查看nginx日誌
③四層負載均衡開啓日誌
#四層負載均衡是沒有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; } }
①使用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; } }