七层、四层、三层负载均衡

七层负载均衡(HTTP Load Balancing):基于应用层,nginx默认使用的是七层负载均衡,因为需要看http协议,针对业务来做负载均衡

7层只能给http做负载均衡,4层可以给很多的协议做负载均衡(ftp,MySQL,dns,http等)根据端口来实现,nginx也可以做四层负载均衡

四层负载均衡(TCP and UDP Load Balancing):基于传输层,LVS使用的是四层负载均衡,只需要看端口号,LVS的效率更高,针对业务来做负载均衡

四层负载均衡编译安装的时候需要接--with-stream,模块名叫做ngx_stream_core_module

做一个nginx的四层负载均衡

 

[root@lb conf]# cp nginx.conf nginx.conf.bak
[root@lb conf]# vim nginx.conf
[root@lb conf]# cat nginx.conf|egrep '^$|^#' -v
worker_processes  2;
events {
    worker_connections  2048;
}
stream{
	upstream web_servers{
		hash $remote_addr;
		server 192.168.2.33:80;
		server 192.168.2.34:80;
	}
	server{
		listen 80;
		proxy_pass web_servers;
	}
}
[root@lb conf]# nginx -t
nginx: the configuration file /usr/local/sclilin99/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/sclilin99/conf/nginx.conf test is successful
[root@lb conf]# nginx -s reload
[root@lb conf]# ps aux|grep nginx
root       3888  0.0  0.2  50668  2204 ?        Ss   09:03   0:00 nginx: master process /usr/local/sclilin99/sbin/nginx
lilin      4649  0.0  0.2  51632  2412 ?        S    10:04   0:00 nginx: worker process
lilin      4650  0.0  0.2  51632  2412 ?        S    10:04   0:00 nginx: worker process
root       4652  0.0  0.0 112824   988 pts/1    R+   10:04   0:00 grep --color=auto nginx

nginx官方的四层负载均衡举例:TCP and UDP Load Balancing | NGINX Plus

stream {
    upstream stream_backend {
        least_conn;
        server backend1.example.com:12345 weight=5;
        server backend2.example.com:12345 max_fails=2 fail_timeout=30s;
        server backend3.example.com:12345 max_conns=3;
    }
    
    upstream dns_servers {
        least_conn;
        server 192.168.136.130:53;
        server 192.168.136.131:53;
        server 192.168.136.132:53;
    }
    
    server {
        listen        12345;
        proxy_pass    stream_backend;
        proxy_timeout 3s;
        proxy_connect_timeout 1s;
    }
    
    server {
        listen     53 udp;
        proxy_pass dns_servers;
    }
    
    server {
        listen     12346;
        proxy_pass backend4.example.com:12346;
    }
}

三层负载均衡:基于网络层,在路由器上做负载均衡,例如ospf协议可以做负载均衡,只是在中间链路上做负载均衡,不能针对具体业务做负载均衡

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值