wordpress 上传mp4优化网站搜索
文章目录
- 知识点回顾
- 1. 四层负载均衡概述
- 2. Nginx四层负载均衡实践
- 3. nginx四层负载实现tcp转发
- 4. 知识小总结
知识点回顾
七层负载均衡作用:(OSI 应用层)流量分发后端服务高可用调度策略轮询加权轮询ip_hash (会话登录) redis将session_id进行共享url_hash健康检查(官方)backup 备机down 注释max_fails 失败的次数fail_timeout 多长时间内失败max_fails次,则视为down健康检查 check_upstream 第三方upstream
proxy_pass
1. 四层负载均衡概述
四层负载均衡:
版本协议:(OSI传输层 ip:port)nginx1.9 版本
硬件:F5
软件:LVS、Haproxy、Nginx
负载均衡的作用:
1.四层+七层来作负载均衡,4层可以保证7层的负载均衡的高可用性。如:nginx就无法保证自己的服务高可用,需要依赖lvs或者keepalive来作。
2.如:tcp协议的负载均衡,有些请求是TCP协议的(mysql、ssh),或者说这些请求只需要使用4层进行端口的转发就可以了,所以使用4层负载均衡。
比如做:mysql读的负载均衡(轮询)
比如做:端口映射、端口转发 tcp/udp
四层负载均衡总结:
1.四层负载均衡仅能转发TCP/IP协议、UDP协议,通常用来转发端口,如: tcp/3306,tcp/22,udp/53。
2.四层负载均衡可以用来解决七层负载均衡的端口限制问题。(七层负载均衡最大使用65535个端口号)
3.可以用来解决七层负载均衡的高可用问题。(多台后端七层负载均衡能同时的使用)
4.四层的转发效率比七层的高的多,但仅支持tcp/ip协议,不支持http或者https协议
自我总结:
(1)4层可以保证7层的负载均衡的高可用性,如果没有四层的话,多个七层组成的集群
这样做的话没有流量分发的,如果用keeplive,只有一台坏掉的话,其他的才顶上来
造成了浪费
(2)四层只能ip+端口的匹配,只拆到四层,然后抛到后面,七层接着报数据包拆完,需要做host域名匹配的,四层之所以块,直接把请求给扔出去了 ,支持的连接
(3)lvs用的不多,因为必须跑在硬件的机器上keeplive也是跑在硬件,在云环境下没有办法使用的
(4)mysql监听在 tcp的3306端口跟http没有任何关系 ,七层负载均衡只能做http协议相关的,四层负载均衡只能做tcp的转发,ip+端口的形式,四层不需要做任何域名的匹配。面试会问: 高可用解决方案,四层,七层
(5)图解知识点
四层+七层构建大规模集群架构使用场景
2. Nginx四层负载均衡实践
lb02上的配置:
(1)在七层负载均衡lb02上面安装Nginx (这是新添加的虚拟机)
[root@lb02 ~]# cat /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
[root@lb02 ~]# yum install nginx -y
(2)在lb02上面拷贝lb01的所有nginx相关配置即可。
[root@lb02 ~]# scp -rp root@172.16.1.5:/etc/nginx /etc/
(3)启动nginx
[root@lb02 conf.d]# nginx -t
[root@lb02 conf.d]# systemctl start nginx
[root@lb02 conf.d]# systemctl enable nginx
lb-4四层负载均衡的配置:
(1)配置nginx四层负载均衡
[root@lb4-01 ~]# cat /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
[root@lb4-01 ~]# yum install nginx -y [root@lb4-01 ~]# vim /etc/nginx/nginx.conf
events {....
}include /etc/nginx/conf.c/*.conf; # 添加这个为了方便四层配置文件,都集中在一起http {.....
}
(2)创建存放四层负载均衡配置的目录
[root@lb4-01 conf.c]# rm -f /etc/nginx/conf.d/default.conf #删除http的80端口
[root@lb4-01 ~]# mkdir /etc/nginx/conf.c
[root@lb4-01 ~]# cd /etc/nginx/conf.c
[root@lb4-01 conf.c]# cat lb_domain.conf
stream {upstream lb {server 172.16.1.5:80 weight=5 max_fails=3 fail_timeout=30s; # 这个时间测试的时候要不要都行,因为blog.oldboy.com需要的时间比较长,要是加了反而连不上了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;}
}
(3)重载服务
[root@lb4-01 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@lb4-01 conf.c]# systemctl restart nginx [root@lb4-01 conf.c]# systemctl enable nginx
==自我总结: ==
(1)如果有一台七层负载挂掉,页面不会报502,因为四层负载0不是http协议,类似于端口转发的操作
(2)在上一次的php-myadmin实验中我门修改了session.auto_start = 1 ,但是做四层负载实验的时候,要不他改成0,知乎的项目起不来。 在web01和web02都修改。
3. nginx四层负载实现tcp转发
使用nginx四层负载均衡实现tcp的转发
请求负载均衡 5555 ---> 172.16.1.7:22;
请求负载均衡 6666 ---> 172.16.1.51:3306;
(1)在/etc/nginx/conf.c/书写配置文件
[root@lb4-01 ~]# cat /etc/nginx/conf.c/lb_port.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;}
}
(2)检查nginx语法并重启
[root@lb4-01 ~]# nginx -t
[root@lb4-01 ~]# systemctl restart nginx
nginx四层负载均衡记录日志:
日志定义在http层,跟tcp层没有关系,需要重新定
义
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;
}
日志展示效果:
10.0.0.1 59129 - [23/Jan/2019:12:11:40 +0800] 200 TCP "172.16.1.5:80" "0" "0.000"
10.0.0.1 59124 - [23/Jan/2019:12:11:41 +0800] 200 TCP "172.16.1.6:80" "1298" "0.000"
10.0.0.1 59145 - [23/Jan/2019:12:11:44 +0800] 200 TCP "172.16.1.6:80" "1316" "0.001"
10.0.0.1 59598 - [23/Jan/2019:12:22:03 +0800] 200 TCP "10.0.0.7:22" "2213" "0.001"
10.0.0.1 59672 - [23/Jan/2019:12:23:51 +0800] 200 TCP "10.0.0.7:22" "2893" "0.001"
10.0.0.1 59698 - [23/Jan/2019:12:26:43 +0800] 200 TCP "10.0.0.7:22" "3309" "0.000"
4. 知识小总结
(1)nginx四层负载实现tcp转发是代理转发,用户通过代理连内网主机,实际是代理和目标机建立连接。在阿里云中买的主机做的四层负载均衡是直接转发,相当于用户和目标机建立连接(类似于lvs)
四层负载均衡:转发TCP/ip协议,端口转发解决七层负载均衡高可用,解决网站并发或者链接的瓶颈。
场景:4+7 大规模集群使用场景通过四层负载均衡的2222端口,转发到后端的某一台主机的22端口四层负载均衡硬件:F5软件:LVS、Haproxy、Nginx(1.90)诞生 stream(tcp) 不能配置在http层四层负载均衡场景演示:实现动静分离:单台实现方式多台实现方式(通过负载均衡来实现动态静态的拆分)
根据不同的设备调度到不同的服务器上: pc iphonerewrite跳转:
https:暂时未解决的问题:安全 httpsnginx四层负载均衡高可用,keepalived 主和备的方式nginx优化nginx常见问题php优化扩展uwsgi