1.HTTP动态负载均衡对不同服务器的状态监控,量化不同服务器的性能差异,周期性调整服务器的比例,实现权重的动态调整。在nginx传统的负载均衡中,如果upstream参数发生变化,每次都需要重新加载nginx.conf文件,所以可扩展性不是很高,所以我们可以使用动态负载均衡实现upstream的可配置动态,无需手动重新加载nginx.conf。这类似于分布式配置中心。动态负载均衡实现方案Consul+Consul-template每次发现配置变化,需要raloadnginx,重启Nginx。Consul+OpenResty不用raload实现动态负载均衡。Consul+upsync+Nginx实现动态负载均衡无需raload。常见的服务器注册和发现框架1.常见的服务发现框架Consul、Eureka、ZooKeeper、Etcd。ZooKeeper是此类最古老的项目之一,起源于Hadoop。它非常成熟、可靠,并被许多大公司(YouTube、eBay、Yahoo等)使用。2、Consul是HashiCorp推出的开源工具,用于实现分布式系统的服务发现和配置。Consul是用Go语言编写的,所以它天然具有便携性(支持Linux、windows和MacOSX)。Consul采用主从模式的设计,使得集群数量可以大规模扩展,集群间通过RPC(HTTP和DNS)调用。Consul内置了服务注册和发现框架、分布式一致性协议实现、健康检查、Key/Value存储和多数据中心解决方案。不再需要依赖其他工具(如ZooKeeper等),使用起来也比较简单。3、etcd是一个使用HTTP协议的key/value对存储系统。它是一个分布式的、功能性的分层配置系统,可用于构建服务发现系统。易于部署、安装和使用,并提供可靠的数据持久化。它是安全的并且有据可查。nginx下的动态负载均衡,目前有三种选择:1.Tengine的dyups模块。2、微博的Upsync+Consul实现动态负载均衡。3.OpenResty的balancer_by_lua(也是排云用的是它开源的slardar(Consulbalancer_by_lua))。下面这篇文章介绍如何使用upsync模块和consul来实现nginx的动态负载均衡。2.CONSUL快速入门Consul是一个开源的分布式服务注册和发现系统。它可以通过HTTPAPI使服务注册和发现变得非常简单。它支持以下功能。服务注册:服务实现者可以通过HTTPAPI或DNS向Consul注册服务。服务发现:服务消费者可以通过HTTPAPI或DNS从Consul获取服务IP和PORT。故障检测:支持TCP、HTTP等健康检查机制,当服务出现故障时自动移除。K/V存储:使用K/V存储实现动态配置中心,使用HTTP长轮询实现变更触发和配置变更。多数据中心:支持多数据中心,可以按照数据中心注册和发现服务,即只能消费本地机房服务,使用多数据中心集群也可以避免单个数据单点故障中心。Raft算法:Consul使用Raft算法实现集群数据一致性。服务注册和发现可以通过Consul进行管理。接下来需要一个与Nginx部署在同一台机器上的Agent,实现Nginx配置更改和Nginx重启功能。我们有两种选择,Confd或者Consul-template,而Consul-template是Consul官方提供的,所以我们选择它。它使用HTTP长轮询来进行更改触发器和配置更改(使用Consul的watch命令实现)。也就是说,我们使用Consul-template来实现配置模板,然后拉取Consul配置渲染模板,生成实际的Nginx配置。CONSUL环境搭建1.下载consul_1.13.1_linux_amd64.zipwgethttps://releases.hashicorp.com/consul/1.13.1/consul_1.13.1_linux_amd64.zip2.解压consul_0.7.5_linux_amd64.zipunzipconsul_0.7.5_linux_amd64.zip#如果解压这个错误-bash:unzip:#commandnotfound#centos解决办法yum-yinstallunzip#ubuntu解决办法apt-get-yinstallunzip3.执行./consul出现如下信息说明安装成功4.启动领事,我的linuxip地址是192.168.199.104./consulagent-dev-ui-node=consul-dev-client=192.168.199.1045。暂时关闭防火墙systemctlstopfirewalld6。浏览器访问192.168.199.104:8500,会出现如下页面7.使用PostMan注册HTTP服务//这里的ip地址是ConsulServer所在服务器的地址//http://192.168.199.104:8500/v1/catalog/register///参数1{"Datacenter":"dc1","Node":"go-http","Address":"192.168.199.104",//本地地址"Service":{"Id":"192.168.199.104:8080",#8080"Service":"helloword","tags":["dev"],"Port":8080}}//参数2{"Datacenter":"dc1","Node":"go-http","Address":"192.168.199.104","Service":{"Id":"192.168.199.104:8081",//8081"Service":"helloword","tags":["dev"],"Port":8081}}Datacenter指定数据中心,Address指定服务IP,Service.Id指定服务唯一标识,Service.Service指定服务分组,Service.tags指定服务标签(如测试环境、预发布环境等),Service.Port指定服务端口8。发现HTTP服务http://192.168.199.104:8500/v1/catalog/service/helloword3.NGINX-UPSYNC-MODULE注意:清空之前的Nginx环境,重新安装。NGINX-UPSYNC-MODULE简介Upsync是新浪微博开源的基于Nginx动态配置的三方模块。Nginx-Upsync-Module的作用是拉取Consul的后端服务器列表,动态更新Nginx的路由信息??。该模块不依赖于任何第三方模块。Consul作为Nginx的DB,使用Consul的KV服务,每个NginxWork进程独立拉取各个upstream的配置,更新各自的路由。NGINX-UPSYNC-MODULE安装配置全过程安装Nginxwgethttp://nginx.org/download/nginx-1.23.1.tar.gz#功能:实现反向代理,load加载库安装consulwgethttps://releases。hashicorp.com/consul/1.13.1/consul_1.13.1_linux_amd64.zip#功能:配置实现动态负载均衡注册安装nginx-upsync-modulewgethttps://github.com/weibocom/nginx-upsync-module/archive/master.zip#作用:nginx动态获取上游最新信息#解压安装unzipmaster.zipunzipconsul_1.13.1_linux_amd64.zip#如果解压时出现这个错误-bash:unzip:commandnotfound#centos解决方法yum-y安装解压#ubuntu解决方案apt-get-yinstallunzip#installNginx#unzipNginxtar-zxvfnginx-1.23.1.tar.gz#configureNginxgroupaddnginxuseradd-gnginx-s/sbin/nologinnginxmkdir-p/var/tmp/nginx/client/mkdir-p/usr/local/nginxcompileNginxcdnginx-1.23.1./configure--prefix=/usr/local/nginx--user=nginx--group=nginx--with-http_ssl_module--with-http_flv_module--with-http_stub_status_module--with-http_gzip_static_module--with-http_realip_module--http-client-body-temp-path=/var/tmp/nginx/client/--http-proxy-temp-path=/var/临时文件/nginx/proxy/--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi--http-scgi-temp-path=/var/tmp/nginx/scgi--with-pcre--add-module=../nginx-upsync-module-mastermake&&makeinstall编译报错./configure:error:SSLmodulesrequiretheOpenSSLlibrary。centos解决方案yum-yinstallopensslopenssl-develubuntu解决方案apt-get-yinstallopensslopenssl-develcreateupsync_dump_pathmkdir/usr/local/nginx/conf/servers/#upsync_dump_path指定从consul拉取的上游服务器并持久化到该位置,这样即使consul服务器出现问题,本地还是有备份的上游动态配置http{upstreamhelloworld{server127.0.0.1:1111;#连接ConsulServer动态获取配置负载均衡信息,每隔0.5s获取信息upsync192.168.199.104:8500/v1/kv/upstreams/helloworldupsync_timeout=6mupsync_interval=500msupsync_type=consulstrong_dependency=off;#动态拉取ConsulServer相关负载均衡信息并持久化到硬盘upsync_dump_path/usr/local/nginx/conf/servers/servers_test.conf;}服务器{听80;服务器名称本地主机;位置/{proxy_passhttp://helloword;indexindex.htmlindex.htm;}}}1.upsync命令指定从consul拉取upstream服务器配置的路径;upsync_timeout配置从consul拉取上游服务器配置的超时时间;upsync_interval配置consul从upstream服务器拉取配置的间隔时间;upsync_type指定使用consul配置服务器;strong_dependency配置nginx启动时是否强制依赖配置服务器。如果配置为on,拉取配置失败时nginx启动也会失败。upsync_dump_path指定从consul拉取的上游服务器持久化的位置,这样即使consul服务器出现问题,仍然有本地备份。2.注意:更换consul注册中心地址,启动consul,暂时关闭防火墙,systemctl停止firewalld,我的linuxIp地址192.168.199.104./consulagent-dev-ui-node=consul-dev-client=192.168.199.104,添加nginx上游服务1.使用linux命令发送put请求curl-XPUThttp://192.168.199.104:8500/v1/kv/upstreams/helloworld/192.168.199.104:8080curl-XPUThttp://192.168.199.104:8500/v1/kv/upstreams/helloworld/192.168.199.104:80812。使用postmen发送put请求http://192.168.199.104:8500/v1/kv/upstreams/helloworld/192.168.199.104:8080http://192.168.199.104:8500v1/kv/upstreams/helloworld/192.168.199.104:8081负载均衡信息参数{"weight":1,"max_fails":2,"fail_timeout":10,"down":0}启动Nginx参考文档nginx-proxy_pass官网完整示例配置nginx-upsync-module【NGINX】基于CONSUL+UPSYNC+NGINX的动态负载均衡
