四层负载均衡
课前回顾
代理,代理服务器,使用proxy模块
负载均衡解决了,使用一个域名访问多台web的问题,必须要使用proxy+upstream
	负载均衡和共享存储都是为了web的共享
七层负载均衡:识别域名,作用于http层
四层负载均衡:不识别域名,作用于tcp层,可以做端口转发,做数据库的代理
nginx在1.9版本之前不支持四层负载均衡
nginx在1.9版本之后支持了四层负载均衡
nginx状态异常是因为配置文件有错误,所以nginx无法启动,显示allredey in use
	nginx做四层负载均衡的时候不能使用,别的server使用过的端口,否则无法启动
七层网络模型
四层负载均衡

nginx四层负载均衡配置
需要使用ngx_stream_core_module模块
启动的话,需要注释掉所有的同一个端口的别的nginx服务
可以做7层负载均衡的高可用,四层负载均衡的数据传输比7层快
四层负载均衡可以做7层负载均衡的高可用
#配置主配置文件(驱动层和事件层之间)
[root@lb04 ~]# vim /etc/nginx/nginx.conf
events {
        ....
}
include /etc/nginx/conf.c/*.conf;
http {...}
[root@lb04 ~]# vim /etc/nginx/conf.c/sfz.conf 
stream {
    upstream lb {
            server 172.16.1.7:80 weight=5 max_fails=3 fail_timeout=30s;
            server 172.16.1.8:80 weight=5 max_fails=3 fail_timeout=30s;
    }
	
    server {
            listen 800;
            proxy_connect_timeout 3s;
            proxy_timeout 3s;
            proxy_pass lb;
    }
}
Nginx四层负载均衡端口转发模板
端口转发:
端口映射:
[root@lb4-01 ~]# cat /etc/nginx/conf.c/sfz.conf 
stream {
#定义转发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;
    }
}
1、通过访问负载均衡的5555端口,实际是后端的web01的22端口在提供服务;
2、通过访问负载均衡的6666端口,实际是后端的mysql的3306端口在提供服务。

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号