基于Consul,Nginx-UpSync动态负载均衡
1 . nginx reload的问题
问题描述
nginx reload是有一定损耗的,如果你使用的是长连接的话,那么当reload nginx时长连接所有的worker进程会进行优雅退出,并当该worker进程上的所有连接都释放时,进程才真正退出。
对于社区版nginx目前有三个选择方式:
- Tengine 的Dyups模块。
- 微博的Upsync+Consul 实现动态负载均衡。
- OpenResty的balancer_by_lua(又拍云使用其开源的slardar(Consul balancer_by_lua))。
本文使用upsync模块来解决配置文件修改后,reload nginx进程造成性能下降的问题。
它的功能是拉取 consul 的后端 server 的列表,并更新 Nginx 的路由信息。此模块不依赖于任何第三方模块。consul 作为 Nginx 的 db,利用 consul 的 KV 服务,每个 Nginx work 进程独立的去拉取各个 upstream 的配置,并更新各自的路由。
2 . nginx安装nginx-upsync-module操作实例
注: 在nginx容器篇中,已先安装了nginx的docker容器
2 . 1 下载nginx-upsync-module-master.zip包
https://github.com/weibocom/nginx-upsync-module
2 . 2 下载新的nginx
http://nginx.org/download/
这里选用 nginx-1.21.0.tar.gz 的版本
2 . 3 把当前两个文件上传到nginx的容器中
#先解压两个资源包,并且上传到nginx的容器中
docker cp nginx-1.21.0 nginx:/package/
docker cp nginx-upsync-module-master nginx:/package/
2 . 4 进入nginx的docker容器中,进行环境编译
2 . 4 . 1 更新apt镜像源为阿里源
sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
sed -i s@/security.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
apt clean
apt-get update -y
2 . 4 . 2 加载编译需要的gcc环境
apt -y install gcc autoconf automake make libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev
2 . 4 . 3 执行 nginx -V 获取nginx的配置信息
nginx -V
2 . 4 . 4 进入上传上去的 nginx-1.21.0 目录
复制配置文件的信息,在配置信息的后面添加 nginx-upsync-module-master 模块的文件路径
添加配置如下:
--with-pcre --add-module=/package/nginx-upsync-module-master
2 . 4 . 5 重新编译
make && make install
到此,nginx-upsync-module模块就安装完毕,接下来是进行nginx与consul的动态配置
3 . consul动态配置nginx
3 . 1 在nginx的映射路径中,创建用于加载consul的nginx配置
本地宿主机创建配置文件
vim /docker/nginx/conf.d/http/consul_nginx.conf
vim /docker/nginx/conf.d/http/consul/upsync.conf
upstream UPSYNC {
#当前服务器的地址
server 192.168.48.132:80;
#监听consul的配置中心
upsync 192.168.48.132:8500/v1/kv/upstreams/nginx_test upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
#根据consul把配置文件生成到的upsync.conf文件中(此路径为docker容器中的真实文件路径)
upsync_dump_path /etc/nginx/conf.d/http/consul/upsync.conf;
#引入配置文件
include /etc/nginx/conf.d/http/consul/upsync.conf;
}
server
{
listen 80;
listen [::]:80;
server_name localhost;
root /docker/www;
index index.php index.html;
location /{
proxy_pass http://UPSYNC;
}
}
3 . 2 向consul容器中添加配置信息
curl -X PUT -d '{"weight":1,"max_fails":2,"fail_timeout":10}' http://192.168.48.129:8500/v1/kv/upstreams/nginx_test/192.168.48.129:9504
服务器生成的配置文件
注意:由于upsync.conf文件是由consul触发生成的配置文件,因此不需要重启nginx就可以生效,如果手动修改了 upsync.conf 配置文件,是不会生效的,如果从起nginx,则会重新拉取consul中的配置文件信息,consul中动态修改配置文件会动态的推送到upsync.conf文件中,无需启动,配置文件都生效
3 . 3 测试
3 . 3 . 1 在128,129,134文件中添加测试访问的项目,132是负载均衡服务器
vim order.conf
server {
listen 9504;
listen [::]:9504;
server_name localhost;
root /docker/www;
index index.php index.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /docker/www;
}
location /home {
index index.html index.php;
}
location ~ \.php$ {
root /docker/www/;
fastcgi_pass 172.17.0.3:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
3 . 3 . 2 分别访问128,129,134服务器
3 . 3 . 3 访问 132 负载均衡服务器