平时修改nginx配置文件需要重启,这样会影响到用户体验,Nginx有以下几种方案实现动态配置:
Consul+OpenResty、Consul+upsync
Consul 简介Consul开源的分布式服务注册与发现系统,通过HTTP API可以使得服务注册、发现实现起来非常简单。
Centos7安装Consul下载地址:https://releases.hashicorp.com/consul/
1.命令行下载
wget https://releases.hashicorp.com/consul/1.12.0/consul_1.12.0_linux_amd64.zip
2.解压
unzip consul_1.12.0_linux_amd64.zip
提示错误:unzip 未找到
解决:yum -y install unzip
3.安装验证
./consul
Usage: consul [--version] [--help] <command> [<args>]
Available commands are:
acl Interact with Consul's ACLs
agent Runs a Consul agent
catalog Interact with the catalog
config Interact with Consul's Centralized Configurations
connect Interact with Consul Connect
debug Records a debugging archive for operators
event Fire a new event
exec Executes a command on Consul nodes
force-leave Forces a member of the cluster to enter the "left" state
info Provides debugging information for operators.
intention Interact with Connect service intentions
join Tell Consul agent to join cluster
keygen Generates a new encryption key
keyring Manages gossip layer encryption keys
kv Interact with the key-value store
leave Gracefully leaves the Consul cluster and shuts down
lock Execute a command holding a lock
login Login to Consul using an auth method
logout Destroy a Consul token created with login
maint Controls node or service maintenance mode
members Lists the members of a Consul cluster
monitor Stream logs from a Consul agent
operator Provides cluster-level tools for Consul operators
reload Triggers the agent to reload configuration files
rtt Estimates network round trip time between nodes
services Interact with services
snapshot Saves, restores and inspects snapshots of Consul server state
tls Builtin helpers for creating CAs and certificates
validate Validate config files/directories
version Prints the Consul version
watch Watch for changes in Consul
4.启动
./consul agent -dev -ui -node=consul-dev -client=192.168.126.156
5.浏览器访问 192.168.126.156:8500
nginx-upsync-module 简介Upsync是新浪微博开源的基于Nginx实现动态配置的三方模块。Nginx-Upsync-Module的功能是拉取Consul的后端server的列表,并动态更新Nginx的路由信息。
安装nginx-upsync需要重新编译nginx,以下是安装nginx和安装nginx-upsync详细步骤:
# 1.安装nginx所需依赖
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre pcre-devel
# 2.安装nginx最新稳定版本并解压
wget http://nginx.org/download/nginx-1.20.2.tar.gz && tar -zxvf nginx-1.20.2.tar.gz
# 3.安装nginx upsync
wget https://github.com/weibocom/nginx-upsync-module/archive/master.zip --no-check-certificate
# 4.解压nginx upsync
unzip -zxvf master.gz
# 5.编译nginx 并添加upsync模块
cd nginx-1.20.2 && ./configure \
--prefix=/usr/local/nginx \
--with-pcre --add-module=../nginx-upsync-module-master
# 6.make
make && make install
编辑nginx.conf文件
vim /usr/local/nginx/conf/nginx.conf
动态负载均衡示例
upstream terry{
server 127.0.0.1:11111;
# 指定拉取的consul配置文件位置,拉取超时时间,拉取间隔,指定使用consul服务,是否强制依赖配置服务器(配置on,如果配置文件失败,nginx启动也会失败)
upsync 192.168.126.156:8500/v1/kv/upstreams/terry upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
# 拉取配置文件持久化位置,如果consul服务出现问题,本地有一个备份
upsync_dump_path /usr/local/nginx/conf/servers_upsync.conf;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://terry;
index index.html index.htm;
}
}
启动nginx
/usr/local/nginx/sbin/nginx
注册服务到consul
命令行注册
curl -X PUT http://192.168.126.156:8500/v1/kv/upstreams/terry/192.168.126.1:8081
curl -X PUT http://192.168.126.156:8500/v1/kv/upstreams/terry/192.168.126.1:8082
打开consul控制台可以查看
测试打开浏览器:http://192.168.126.156/
刷新就是轮询访问
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)