昆山做网站公司,全国蔬莱网站建设,网上做问卷报酬不错的网站是,网站职位推荐怎么做前言
正常情况下#xff0c;nginx 做反向代理负载均衡的话#xff0c;如果后端节点服务器宕掉的话#xff0c;nginx 默认是不能把这台服务器踢出 upstream 负载集群的#xff0c;所以还会有请求转发到后端的这台服务器上面#xff0c;这样势必造成网站访问故障 注#x…前言
正常情况下nginx 做反向代理负载均衡的话如果后端节点服务器宕掉的话nginx 默认是不能把这台服务器踢出 upstream 负载集群的所以还会有请求转发到后端的这台服务器上面这样势必造成网站访问故障 注实际上不仅是后端节点宕掉需要踢出集群如果说我们发布服务那么节点服务启动和关闭也是需要时间的此时也需要踢出和加入集群操作 请求转发
最简单的做法就是使用 proxy_next_upstream实现请求转发就是在 localtion 中启用 proxy_next_upstream 来解决返回给用户的错误页面示例如下
location /
{
# 如果后端的服务器返回502、504、执行超时等错误
# 自动将请求转发到upstream负载均衡池中的另一台服务器实现故障转移。
proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header;
}虽然这样问题可以解决但是请求还是会转发给这台服务器然后再转发给别的服务器这样以来就浪费了一次转发会损耗网站性能
健康检查
为了避免上述问题我们可以对后端节点进行节点检查目前主要有如下三种方式可以实现对 nginx 负载均衡的后端节点服务器进行健康检查 nginx 自带模块ngx_http_proxy_module 和 ngx_http_upstream_module ngx_http_healthcheck_module 模块这是 nginx 官方早期推出的健康检查的模块但是目前仅支持 nginx 的 1.0.0 版本1.1.0 版本以后均不支持常见的生产环境上基本不会使用该模块 淘宝技术团队开发的 nginx_upstream_check_module 模块更加专业
本次我们使用第三种方法实现节点健康检查
淘宝技术团队开发的 nginx 模快 nginx_upstream_check_module 可以检测后方 realserver 的健康状态如果后端服务器不可用则会将其踢出 upstream所有的请求不转发到这台服务器。当期恢复正常时将其加入 upstream
在淘宝自己的 tengine 上是自带了该模块的大家可以访问淘宝 tengine 来获取安装如果没有使用淘宝的 tengine 的话也可以通过补丁的方式来添加该模块到 nginx 中
本文为了演示简便将会使用 tengine 作为示例当然文末也会附上如何在原生 nginx 集成该模块
安装 Tengine 系统CentOS 7.6 环境准备
yum -y install gcc-c
yum -y install pcre pcre-devel
yum -y install zlib zlib-devel
yum -y install openssl openssl-devel下载解压
cd /usr/local/src/
wget http://tengine.taobao.org/download/tengine-2.3.2.tar.gztar -zxvf tengine-2.3.2.tar.gz
cd /usr/local/src/tengine-2.3.2
编译安装
使用下面命令Tengine 默认将安装在 /usr/local/nginx 目录。你可以用’–prefix’来指定你想要的安装目录
$ ./configure --add-module./modules/ngx_http_upstream_check_module --add-module./modules/ngx_http_upstream_session_sticky_module --add-module./modules/ngx_http_upstream_dynamic_module
$ make sudo make install注淘宝的 Tengine 升级到 2.3.0 或者2.3.1 都不再默认安装健康检查模块 编译安装之后输出如下文件位置 nginx path prefix: /usr/local/nginxnginx binary file: /usr/local/nginx/sbin/nginxnginx modules path: /usr/local/nginx/modulesnginx configuration prefix: /usr/local/nginx/confnginx configuration file: /usr/local/nginx/conf/nginx.confnginx pid file: /usr/local/nginx/logs/nginx.pidnginx error log file: /usr/local/nginx/logs/error.lognginx http access log file: /usr/local/nginx/logs/access.lognginx http client request body temporary files: client_body_tempnginx http proxy temporary files: proxy_tempnginx http fastcgi temporary files: fastcgi_tempnginx http uwsgi temporary files: uwsgi_tempnginx http scgi temporary files: scgi_temp配置后台
vim /usr/lib/systemd/system/nginx.service[Unit]
Descriptionnginx
Afternetwork.target[Service]
Typeforking
ExecStart/usr/local/nginx/sbin/nginx
ExecReload/usr/local/nginx/sbin/nginx -s reload
ExecStop/usr/local/nginx/sbin/nginx -s quit
PrivateTmptrue[Install]
WantedBymulti-user.target之后即可使用 systemctl 设置 nginx 开启关闭开机自启等
nginx 配置文件路径 /usr/local/nginx/conf/nginx.conf
可以配置节点健康检查如下
http {upstream cluster1 {server 172.25.234.148:9001;server 172.25.234.148:9002;check interval3000 rise2 fall3 timeout1000 typehttp;check_http_send HEAD /actuator/health HTTP/1.0\r\n\r\n;check_http_expect_alive http_2xx http_3xx;}server {location /springboot { proxy_pass http://cluster1/; }location /status {check_status;access_log off;#allow IP;#deny all;}}
}我们配置了 server 172.25.234.148:9001 server 172.25.234.148:9002 两个服务开发端口
check interval3000 rise2 fall3 timeout1000 typehttp
interval向后端发送的健康检查包的间隔。fall如果连续失败次数达到指定次数服务器就被认为是 DOWN。rise如果连续成功次数达到指定次数服务器就被认为是 UP。timeout后端健康请求的超时时间。type健康检查包的类型现在支持以下多种类型 tcp、ssl_hello、http、mysql、ajp。
其实还可以配置 port指定后端服务器的检查端口并且最好和实际业务端口不同防止将健康状态通过 nginx 暴露到外网中但是这里为了方便没有指定也就是将业务和健康检查接口配置在同一端口
check_http_send HEAD /actuator/health HTTP/1.0\r\n\r\n; 配置项该指令可以配置 http 健康检查包发送的请求内容。为了减少传输数据量推荐采用 HEAD 方法该接口地址 /actuator/health 后面我们会使用 Spring Actuator 配置
check_http_expect_alive http_2xx http_3xx; 配置项该指令指定 HTTP 回复的成功状态默认认为 2XX 和 3XX 的状态是健康的。注意在 Actuator 提供的 health 端点在返回服务器是 UP 状态时的状态码为 200在返回服务器是 DOWN 状态时的状态码为 503满足该 check_http_expect_alive 配置项
location /springboot 配置项我们创建了一个 Location转发到我们配置的 Upstream。
location /status 配置项我们创建了一个 Location转发到 Tengine 提供的服务器的健康状态页之后可以访问 http://ip/status 就可以看到当前两台 realserver 实时的健康状态
之后重启 nginx 即可生效后面我们配置 actuator 节点健康检查
注意
在生产环境的实施应用中需要注意下面两点
1主要定义好 type。由于默认的 type 是 tcp 类型因此假设服务启动不管是否初始化完毕它的端口都会起来所以此时前段负载均衡器为认为该服务已经可用其实是不可用状态。 2注意 check_http_send 值的设定。由于它的默认值是GET / HTTP/1.0\r\n\r\n。 假设应用是通过 http://ip/name 访问的那么这里 check_http_send 值就需要更改为GET /name HTTP/1.0\r\n\r\n才可以。 针对采用长连接进行检查的这里增加 keep-alive 请求头即HEAD /name HTTP/1.1\r\nConnection: keep-alive\r\n\r\n。 如果后端的 tomcat 是基于域名的多虚拟机此时你需要通过 check_http_send 定义 host不然每次访问都是失败范例
check_http_send GET /mobileapi HTTP/1.0\r\n HOST www.redhat.sx\r\n\r\n ;配置 Actuator
示例仓库地址ReturnTmp/spring-actuator-demo (github.com)
依赖配置 pom.xml !-- actuator --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactIdversion3.1.0/version/dependencyapplication.yml
server: port: 9000
# 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * 可以开放所有端点。
management: endpoints: web: exposure: include: *添加接口可以输出服务端口 GetMapping(/port)public Object port() {return String.format(port%s, env.getProperty(local.server.port));}可以单独配置 actuator 展示端口防止 nginx 暴露但是为了演示简便起见本次不单独设置端口
management:server:port: 8078启动项目即可通过 /actuator/health 接口查看健康状态
然后给项目 maven 打包 package 给对应 jar 包上传服务器分别在两个窗口运行两个服务nohup 后台运行也可以
java -jar spring-actuator-demo-0.0.1-SNAPSHOT.jar --server.port9001
java -jar spring-actuator-demo-0.0.1-SNAPSHOT.jar --server.port9002此时访问 http://ip/springboot/port
通过输出的端口可以发现已经顺利实现负载均衡然后给其中一个服务挂掉可以发现并没有出现部分请求无法访问问题
然后重新启动挂掉的服务访问接口可以发现过了一段时间后节点自动添加到了负载均衡集群中
访问 http://ip/status 可以查看负载均衡集群节点 集成模块
编译安装
[rootlocalhost ~]# cd /usr/local/src
[rootlocalhost src]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
[rootlocalhost src]# unzip nginx_upstream_check_module-master.zip
[rootlocalhost src]# ls
master.zip nginx_upstream_check_module-master[rootlocalhost src]# wget http://nginx.org/download/nginx-1.8.0.tar.gz
[rootlocalhost src]# tar -zxvf nginx-1.8.0.tar.gz
[rootlocalhost src]# cd nginx-1.8.0[rootlocalhost nginx-1.8.0]# patch -p1 ../nginx_upstream_check_module-master/check_1.9.2.patch
[rootlocalhost nginx-1.8.0]# ./configure --prefix/usr/local/nginx --usernginx --groupnginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --add-module../nginx_upstream_check_module-master/
[rootnode1 src]# make make install配置 nginx
[rootmaster-node ~]# vim /usr/local/nginx/conf/vhosts/LB.conf
upstream LB-WWW {server 192.168.1.101:80;server 192.168.1.102:80;check interval3000 rise2 fall5 timeout1000 typehttp;check_keepalive_requests 100;check_http_send HEAD / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n;check_http_expect_alive http_2xx http_3xx;}server {listen 80;server_name www.wangshibo.com;access_log /usr/local/nginx/logs/www-access.log main;error_log /usr/local/nginx/logs/www-error.log;location / {proxy_pass http://LB-WWW;proxy_redirect off ;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header REMOTE-HOST $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_connect_timeout 300;proxy_send_timeout 300;proxy_read_timeout 600;proxy_buffer_size 256k;proxy_buffers 4 256k;proxy_busy_buffers_size 256k;proxy_temp_file_write_size 256k;proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;proxy_max_temp_file_size 128m;proxy_cache mycache;proxy_cache_valid 200 302 60m;proxy_cache_valid 404 1m;}location /nstatus {check_status;access_log off;#allow IP;#deny all;}
}参考链接
Nginx 负载均衡中后端节点服务器健康检查 - 博客园 (cnblogs.com)芋道 Spring Boot 持续交付 Jenkins 入门 | 芋道源码验证码cokeNGINX 负载均衡健康检查和会话保持 - 小丶凡 - 博客园 (cnblogs.com)全网 Tengine 最新版本部署及原理 - 博客园 (cnblogs.com)NGINX笔记之: Tengine编译安装Tengine/2.3.1编译安装踩坑笔记 本文由博客一文多发平台 OpenWrite 发布