项目背景:
链路流程: vip —>nginx—>upstream—>server—>ip+port
1.目前还没做路由功能,分组是按服务器分组(不是按用户分组),分组发布过程一个队列形式,这里提供自定义规则切换服upstrem。
2.不管是全量发布,分组发布,快速发布,lb只做一件事:切换upstream(running_upstream与standy_upstream
3.动态切换LB,做法两种,一是直接操作upstream,二是操作upstream里的server.
4.与ci集成方式:超链接web
方案一:模块fly操作upstream,web方式增删查改upsterem里的server
无drop重启nginx
kill -HUP `cat /data/nginx/logs/nginx.pid`
#!/bin/sh
BASE_DIR='/usr/local/'
${BASE_DIR}nginx/sbin/nginx -t -c ${BASE_DIR}nginx/conf/nginx.
conf >& ${BASE_DIR}nginx/logs/nginx.start
conf >& ${BASE_DIR}nginx/logs/nginx.start
info=`cat ${BASE_DIR}nginx/logs/nginx.start`
if [ `echo $info | grep -c "syntax is ok" ` -eq 1 ]; then
if [ `ps aux|grep "nginx"|grep -c "master"` == 1 ]; then
kill -HUP `cat ${BASE_DIR}nginx/logs/nginx.pid`
echo "ok"
else
killall -9 nginx
sleep 1
${BASE_DIR}nginx/sbin/nginx
fi
else
echo "######## error: ########"
cat ${BASE_DIR}nginx/logs/nginx.start
方案二:模块ngx_dynamic_upstream,api+python方式(直接操作upstream里的server)
因为api接口操作不是太直观,在django上以web方式增删查改upstream
HTTP APIs
You can operate upstreams dynamically with HTTP APIs.
list
$ curl "http://127.0.0.1:6000/dynamic?upstream=zone_for_backends" server 127.0.0.1:6001; server 127.0.0.1:6002; server 127.0.0.1:6003; $
verbose
$ curl "http://127.0.0.1:6000/dynamic?upstream=zone_for_backends&verbose=" server 127.0.0.1:6001 weight=1 max_fails=1 fail_timeout=10; server 127.0.0.1:6002 weight=1 max_fails=1 fail_timeout=10; server 127.0.0.1:6003 weight=1 max_fails=1 fail_timeout=10; $
update_parameters
$ curl "http://127.0.0.1:6000/dynamic?upstream=zone_for_backends&server=127.0.0.1:6003&weight=10&max_fails=5&fail_timeout=5" server 127.0.0.1:6001 weight=1 max_fails=1 fail_timeout=10; server 127.0.0.1:6002 weight=1 max_fails=1 fail_timeout=10; server 127.0.0.1:6003 weight=10 max_fails=5 fail_timeout=5; $
The supported parameters are blow.
- weight
- max_fails
- fail_timeout
down
$ curl "http://127.0.0.1:6000/dynamic?upstream=zone_for_backends&server=127.0.0.1:6003&down=" server 127.0.0.1:6001 weight=1 max_fails=1 fail_timeout=10; server 127.0.0.1:6002 weight=1 max_fails=1 fail_timeout=10; server 127.0.0.1:6003 weight=1 max_fails=1 fail_timeout=10 down; $
up
$ curl "http://127.0.0.1:6000/dynamic?upstream=zone_for_backends&server=127.0.0.1:6003&up=" server 127.0.0.1:6001 weight=1 max_fails=1 fail_timeout=10; server 127.0.0.1:6002 weight=1 max_fails=1 fail_timeout=10; server 127.0.0.1:6003 weight=1 max_fails=1 fail_timeout=10; $
add
$ curl "http://127.0.0.1:6000/dynamic?upstream=zone_for_backends&add=&server=127.0.0.1:6004" server 127.0.0.1:6001; server 127.0.0.1:6002; server 127.0.0.1:6003; server 127.0.0.1:6004; $
remove
$ curl "http://127.0.0.1:6000/dynamic?upstream=zone_for_backends&remove=&server=127.0.0.1:6003"
server 127.0.0.1:6001;
server 127.0.0.1:6002;
server 127.0.0.1:6004;
server 127.0.0.1:6001;
server 127.0.0.1:6002;
server 127.0.0.1:6004;
reference:
方案三:lua(lua_code_cache)热装载+upstream ,直接操作upstream
简说热装载(热加载) :lua代码产生变更,利用lua_code_cache on ,实现自动加载,不用reload nginx。
亮点: 通过lua处理,不用重启nginx,利用api进行增删查改
不足:操作还是基于命令方式,后期还得加上web操作
restful interface
GET
-
/detail
get all upstreams and their servers -
/list
get the list of upstreams -
/upstream/name
find the upstream by it's name
POST
-
/upstream/name
update one upstream -
body
commands; -
body
server ip:port;
DELETE
-
/upstream/name
delete one upstream
Call the interface, when you get the return code is HTTP_INTERNAL_SERVER_ERROR 500
, you need to reload nginx to make the Nginx work at a good state.
If you got HTTP_CONFLICT 409
, you need resend the same commands again latter.
The /list and /detail interface will return HTTP_NO_CONTENT 204
when there is no upstream.
Other code means you should modify your commands and call the interface again.
ATTENEION
: You also need a third-party
to generate the new config and dump it to Nginx'conf directory.
Sample
» curl -H "host: dyhost" 127.0.0.1:8080 <html> <head><title>502 Bad Gateway</title></head> <body bgcolor="white"> <center><h1>502 Bad Gateway</h1></center> <hr><center>nginx/1.3.13</center> </body> </html> » curl -d "server 127.0.0.1:8089;server 127.0.0.1:8088;" 127.0.0.1:8081/upstream/dyhost success » curl -H "host: dyhost" 127.0.0.1:8080 8089 » curl -H "host: dyhost" 127.0.0.1:8080 8088 » curl 127.0.0.1:8081/detail host1 server 127.0.0.1:8088 host2 server 127.0.0.1:8089 dyhost server 127.0.0.1:8089 server 127.0.0.1:8088 » curl -i -X DELETE 127.0.0.1:8081/upstream/dyhost success » curl 127.0.0.1:8081/detail host1 server 127.0.0.1:8088 host2 server 127.0.0.1:8089
API
extern ngx_flag_t ngx_http_dyups_api_enable; ngx_int_t ngx_dyups_update_upstream(ngx_str_t *name, ngx_buf_t *buf, ngx_str_t *rv); ngx_int_t ngx_dyups_delete_upstream(ngx_str_t *name, ngx_str_t *rv); extern ngx_dyups_add_upstream_filter_pt ngx_dyups_add_upstream_top_filter; extern ngx_dyups_del_upstream_filter_pt ngx_dyups_del_upstream_top_filter;
Lua API Example
NOTICE: you should add the directive dyups_interface
into your config file to active this feature
content_by_lua '
local dyups = require "ngx.dyups"
local status, rv = dyups.update("test", [[server 127.0.0.1:8088;]]);
ngx.print(status, rv)
if status ~= ngx.HTTP_OK then
ngx.print(status, rv)
return
end
ngx.print("update success")
status, rv = dyups.delete("test")
if status ~= ngx.HTTP_OK then
ngx.print(status, rv)
return
end
ngx.print("delete success")
local dyups = require "ngx.dyups"
local status, rv = dyups.update("test", [[server 127.0.0.1:8088;]]);
ngx.print(status, rv)
if status ~= ngx.HTTP_OK then
ngx.print(status, rv)
return
end
ngx.print("update success")
status, rv = dyups.delete("test")
if status ~= ngx.HTTP_OK then
ngx.print(status, rv)
return
end
ngx.print("delete success")
';
reference:
方案四:自研
自已写lua+redis,再封装一层api,供业务插入标签,再进行增删查改
相关推荐
官方nginx 镜像不带主动健康,本镜像将 nginx_upstream_check健康检查 打包到了镜像中。
此资源有两个文件,含 nginx-upstream-jvm-route 和 nginx 对应版本,都是tar.gz文件。 安装方法网上很多就不写了,亲测可用。 不用担心版本不匹配造成安装失败,再浪费积分去到处下载尝试的烦恼。 此资源有两个文件...
基于lua的nginx自定义负载均衡基于lua的nginx自定义负载均衡
nginx经常用到的负载均衡的5种策略,注意细节,部分属性不能一起使用。
nginx配置upstream的资源文件文件,下载下来直接上传至服务解压即可
nginx-upstream-jvm-route 支持nginx版本1.15 解决nginx: [emerg] invalid parameter "srun_id=tomcat1" 问题
nginx1.8 负载均衡
本文将分析Nginx服务器负载均衡的体系架构,研究默认的加权轮询算法,并提出一种改进后的动态负载均衡算法,实时收集负载信息,重新计算并分配权值。通过实验测试,对比不同算法下的负载均衡性能,改进后的算法能有效提高...
Nginx的负载均衡入门 Nginx的负载均衡入门 Nginx的负载均衡入门
linux nginx nginx_upstream_jvm_route
NGINX第三方模块:nginx_upstream_hash-0.3.1.tar.gz。
CentOS6.8 安装 Nginx 配置负载CentOS6.8 安装 Nginx 配置负载
nginx配置 +负载均衡+https协议 完整的配置以及nginx 启动 停止 重新加载
HAProxy+Nginx实现负载均衡
nginx软件负载均衡,实现各种集群服务器应用的负载均衡!
nginx优化之request_time 和upstream_response_time差别笔者在根据nginx的accesslog中$request_time
NGINX第三方模块:nginx_upstream_hash-0.3.2版本。。。
Nginx+Tomcat负载均衡解决方案,当前信息系统的应用范围越来越广泛,我们的信息系统从开始的只需向少量用户提供服务,到现在越来越频繁的需要应对大量用户同时在线,系统高并发的情况也越来越常见,并且对系统的稳定...
nginx1.16版本 后端节点健康检查、自动隔离+nginx_upstream_check_module-master+nginx-upload-module
Nginx均衡负载-fair(第三方)模块,要实现这种均衡负载操作,必须给Nginx添加这个模块,否则,无法进行!