首页 > 系统相关 >nginx负载均衡

nginx负载均衡

时间:2024-02-24 18:34:17浏览次数:16  
标签:负载 v1 -- consul 192.168 upsync nginx 均衡

sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install consul

unzip consul_1.9.4_linux_amd64.zip
mv consul /usr/local/bin/
consul version

consul 常用命令
consul命令 描述
agent 运行一个consul agent
members 列出consul cluster集群中的members
join 将agent加入到consul集群
leave 将节点移除所在的集群


启动consul服务 (neiwang)
consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul -node=ali -bind=192.168.34.170 -ui -client=0.0.0.0
1.
参数介绍:

-server 表示启动的是一个服务
-bootstrap-expect 1 表示等待多少个节点再启动,这里1个,就是自己一个就启动了
-node=ali 就是给consul服务起个别名为 ali
-bind=192.168.34.170 绑定内网ip
-data-dir /opt/data1 数据存储目录为/opt/data1
-ui 启动默认ui界面
-client consul绑定在哪个client地址上,这个地址提供HTTP、DNS、RPC等服务,默认是127.0.0.1,可指定允许客户端使用什么ip去访问


# 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.zip
# 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 负载均衡配置【192.168.34.170】
upstream blogs{
server www.study_nginx.com:8080 weight=1;
upsync 192.168.34.170:8500/v1/kv/upstreams/nginx_test upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
upsync_dump_path /vhost/server_test.conf;
include /vhost/server_test.conf;
}
server{
listen 80;
server_name www.load_balancing.com;
index index.html index.php;

location ~ \.php$ {
proxy_pass http://blogs;
}
}


upsync 模块安装
cd /home
wget https://github.com/weibocom/nginx-upsync-module/archive/master.zip
unzip master.zip
重新编译nginx :

cd /home/nginx-1.18.0

<font color=“red”>注意:</font>在重新编译前要运行/usr/local/nginx/nginx -V。

将上图参数后面的复制下来,在后面加上我们这次安装的upsync模块

./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_gzip_static_module --with-http_stub_status_module --with-file-aio --with-http_realip_module --with-http_ssl_module --with-pcre=/home/pcre-8.44 --with-zlib=/home/zlib-1.2.11 --with-openssl=/home/openssl-1.1.1g --with-pcre --add-module=/home/nginx-upsync-module-master

make -j2
make install

consul + nginx 动态负载均衡
添加upstream 发送PUT请求

curl -X PUT -d '{"weight":1,"max_fails":2,"fail_timeout":10}' http://192.168.34.170:8500/v1/kv/upstreams/nginx_test/192.168.34.165:80


curl http://127.0.0.1:8500/v1/kv/?recurse

curl www.load_balancing.com/api/admin.php

curl -X DELETE -d '{"weight":1,"max_fails":2,"fail_timeout":10}' http://192.168.34.170:8500/v1/kv/upstreams/nginx_test/192.168.34.170:80

 

 

 

---------------------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------

 

1,动态负载均衡

      传统的负载均衡,如果修改了nginx.conf 的配置,必须需要重启nginx 服务,效率不高。动态负载均衡,就是可配置化,动态化的去配置负载均衡。

2,实现方案

    1. Consul+Consul-template   每次发现配置更改需要raload nginx,重启Nginx。

    2. Consul+OpenResty 实现无需raload动态负载均衡

    3. Consul+upsync+Nginx  实现无需raload动态负载均衡

3,Consul 简介

    Consul是一款开源的分布式服务注册与发现系统,通过HTTP API可以使得服务注册、发现实现起来非常简单,它支持如下特性。

    服务注册:服务实现者可以通过HTTP API或DNS方式,将服务注册到Consul。

    服务发现:服务消费者可以通过HTTP API或DNS方式,从Consul获取服务的IP和PORT。

    故障检测:支持如TCP、HTTP等方式的健康检查机制,从而当服务有故障时自动摘除。

    K/V存储:使用K/V存储实现动态配置中心,其使用HTTP长轮询实现变更触发和配置更改。

   多数据中心:支持多数据中心,可以按照数据中心注册和发现服务,即支持只消费本地机房服务,使用多数据中心集群还可以避免单数据中心的单点故障。

   Raft算法:Consul使用Raft算法实现集群数据一致性。

4,Consul+upsync+Nginx 实现原理

       Upsync是新浪微博开源的基于Nginx实现动态配置的三方模块。Nginx-Upsync-Module的功能是拉取Consul的后端server的列表,并动态更新Nginx的路由信息。此模块不依赖于任何第三方模块。Consul作为Nginx的DB,利用Consul的KV服务,每个Nginx Work进程独立的去拉取各个upstream的配置,并更新各自的路由。

     ConsoleWeb--------k/v-------->  ConsoleServer   <-------upsync(间隔读取)-------- nginx

       ConsoleServer 用来专门存储负载均衡的配置信息,nginx.conf

       ConsoleWeb是一个可视化的界面展示ConsoleServer的界面

       nginx 通过upsync (新浪开发的基于nginx 的一个相当于插件) 间隔读取ConsoleServer的配置信息,同时会在本地存放一份信息,如果宕机,也可以读取到当时配置的配置文件信息(留存)

5,安装consul

在/usr/local 目录下(自定义)

1.下载consul_0.7.5_linux_amd64.zip
wget https://releases.hashicorp.com/consul/0.7.5/consul_0.7.5_linux_amd64.zip

2.解压consul_0.7.5_linux_amd64.zip
unzip consul_0.7.5_linux_amd64.zip
如果解压出现该错误
-bash: unzip: 未找到命令
解决办法
yum -y install unzip

3. 执行以下 ./consul 有命令列表说明安装成功

4.启动consul
我的linux Ip地址192.168.170.110
./consul agent -dev -ui -node=consul-dev -client=192.168.170.110

5.临时关闭防火墙systemctl stop firewalld

6.浏览器访问192.168.170.110:8500,web界面如下:

 

       

7,可以通过界面注册服务,Chrome 浏览器会有缓存,建议使用Firefox, consul 也提供了接口服务:

 
/v1/agent/checks : 返回本地agent注册的所有检查(包括配置文件和HTTP接口)
/v1/agent/services : 返回本地agent注册的所有 服务
/v1/agent/members : 返回agent在集群的gossip pool中看到的成员
/v1/agent/self : 返回本地agent的配置和成员信息
/v1/agent/join/<address> : 触发本地agent加入node
/v1/agent/force-leave/<node>>: 强制删除node
/v1/agent/check/register : 在本地agent增加一个检查项,使用PUT方法传输一个json格式的数据
/v1/agent/check/deregister/<checkID> : 注销一个本地agent的检查项
/v1/agent/check/pass/<checkID> : 设置一个本地检查项的状态为passing
/v1/agent/check/warn/<checkID> : 设置一个本地检查项的状态为warning
/v1/agent/check/fail/<checkID> : 设置一个本地检查项的状态为critical
/v1/agent/service/register : 在本地agent增加一个新的服务项,使用PUT方法传输一个json格式的数据
/v1/agent/service/deregister/<serviceID> : 注销一个本地agent的服务项

/v1/catalog/register : 注册一个新节点、服务或检查
/v1/catalog/deregister : 取消注册节点、服务或检查
/v1/catalog/datacenters : 列出已知数据中心
/v1/catalog/nodes : 列出给定DC中的节点
/v1/catalog/services : 列出给定DC中的服务
/v1/catalog/service/<service> : 列出给定服务中的节点
/v1/catalog/node/<node> : 列出节点提供的服务
 

例如:使用postman post 请求,调用如下的接口http://192.168.178.110:8500/v1/catalog/register 来注册服务,192.168.178.110 是consul 服务ip:

{"Datacenter": "dc1","Node":"tomcat", "Address":"192.168.43.27","Service": {"Id" :"192.168.43.27:8080", "Service": "aiyuesheng","tags": ["dev"], "Port": 8080}}

 

 

当返回true 的时候,这个服务就被注册到consul 服务里了

Datacenter指定数据中心,Address指定服务IP,Service.Id指定服务唯一标识,Service.Service指定服务分组,Service.tags指定服务标签(如测试环境、预发环境等),Service.Port指定服务端口。

 

6,搭建 nginx + consul + upsync 

     这套服务要求nginx 的版本最低1.9

step1:

安装Nginx ,实现反向代理、负载负载库

wget http://nginx.org/download/nginx-1.9.10.tar.gz

安装consul:动态负载均衡均配置实现注册

wget https://releases.hashicorp.com/consul/0.7.1/consul_0.7.1_linux_amd64.zip

安装nginx-upsync-module,间隔的去读取最新的nginx的配置信息

wget https://github.com/weibocom/nginx-upsync-module/archive/master.zip

step2:

unzip master.zip
unzip consul_0.7.1_linux_amd64.zip

 如果解压出现该错误
-bash: unzip: 未找到命令
解决办法:
yum -y install unzip

     

step3:安装Nginx

  解压: tar -zxvf nginx-1.9.10.tar.gz

  配置Nginx:

groupadd nginx
useradd -g nginx -s /sbin/nologin nginx
mkdir -p /var/tmp/nginx/client/
mkdir -p /usr/local/nginx
编译Nginx

cd nginx-1.9.10

./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/tmp/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-master
make && make install
 
编译的是报错
./configure: error: SSL modules require the OpenSSL library.
解决办法
yum -y install openssl openssl-devel


step4:配置upsync
mkdir /usr/local/nginx/conf/servers/

upsync_dump_path指定从consul拉取的上游服务器后持久化到的位置,这样即使consul服务器出问题了,本地还有一个备份。

 step5:配置nginx

 
#动态去consul 获取注册的真实反向代理地址
   upstream aiyunsheng{
        server 127.0.0.1:11111;
#超时是6m 间隔是500m
#aiyuesheng 是我已经注册到consul里面的service,参照步骤8 upsync 192.168.170.110:8500/v1/kv/upstreams/aiyunsheng upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off; upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf; } server { listen 80; server_name 192.168.43.27;#你所要拦截的服务的ip location / { proxy_pass http://aiyunsheng; index index.html index.htm; } }
 

upsync指令指定从consul哪个路径拉取上游服务器配置;

upsync_timeout配置从consul拉取上游服务器配置的超时时间;

upsync_interval配置从consul拉取上游服务器配置的间隔时间;

upsync_type指定使用consul配置服务器;

strong_dependency配置nginx在启动时是否强制依赖配置服务器,如果配置为on,则拉取配置失败时nginx启动同样失败。

upsync_dump_path指定从consul拉取的上游服务器后持久化到的位置,这样即使consul服务器出问题了,本地还有一个备份。

 

step6:启动console:

临时关闭防火墙systemctl stop firewalld

   ./consul agent -dev -ui -node=consul-dev -client=192.168.178.110

step7:启动nginx 

/usr/local/nginx/sbin/nginx

ste8: 通过postman 向注册中心里面注册service 和 在service 里面添加upstrean

注册service:

例如:使用postman post 请求,调用如下的接口http://192.168.178.110:8500/v1/catalog/register 来注册服务,192.168.178.110 是consul 服务ip:

{"Datacenter": "dc1","Node":"tomcat", "Address":"192.168.43.27","Service": {"Id" :"192.168.43.27:8080", "Service": "aiyuesheng","tags": ["dev"], "Port": 8080}}

注册upstream:

使用postmen 发送put请求

http://192.168.178.110:8500/v1/kv/upstreams/aiyuesheng/192.168.43.27:8080

http://192.168.212.134:8500/v1/kv/upstreams/itmayiedu/192.168.43.27:8081

也可以用linux 命令:

1.使用linux命令方式发送put请求
curl -X PUT http://192.168.178.110:8500/v1/kv/upstreams/aiyuesheng/192.168.43.27:8080
curl -X PUT http://192.168.178.110:8500/v1/kv/upstreams/aiyuesheng/192.168.43.27:8081
 

 

 

step9:访问 http://192.168.178.110/ 直接访问consul 的服务地址

项目代码: 

 
@RestController
public class Index {
    @Value("${server.port}")
    private String port;
    
    @RequestMapping("/")
    public String index() {
        return port;
    }
}
 

能实现nginx 从consul 读取配置,转发到我的真实服务中.....

step10: 若是载添加upstream,只需要调用接口直接载consul 添加就OK,无需重启nginx 服务。。。。

 

动态负载均衡就配置完成。

 

 

 



 

标签:负载,v1,--,consul,192.168,upsync,nginx,均衡
From: https://www.cnblogs.com/JIKes/p/18031412

相关文章

  • 请求接口时报错nginx 413 Request Entity Too Large
    1.在rancher容器中执行命令行$cd/etc/nginx$catnginx.conf2.vi或vim修改配置文件bash:vim:commandnotfound更新软件包列表$sudoapt-getupdate更新软件包$sudoapt-getupgrade安装vim$sudoapt-getinstallvim3.vim修改conf配置文件http新增client_m......
  • R语言宏观经济学:IS-LM曲线可视化货币市场均衡
    全文链接:http://tecdat.cn/?p=32249原文出处:拓端数据部落公众号凯恩斯相关理论主要是美国20世纪30年代的经济危机而提出的,主张政府干预经济,实行宏观调控。按照希克斯的观点,灵活偏好(L)和货币数量(M)决定着货币市场的均衡,而人们持有的货币数量既决定于利率(i),又决定于收入(y)的......
  • nginx显示指定文件
    nginx提供文件服务需增强安全性,数据集管理中通过配置nginx只显示指定目录下以preview.jpg(png,jpeg等)结尾的目录文件,其路径配置需要使用正则表达式,并且传参,配置如下:#nginxlocation中~开头的路径表示使用正则表达式location~ /data/data/dir/(.+preview\.(?:jpg|jpeg|gif|p......
  • nginx反向代理配置介绍
    近期搭建portal网站,发现了nginx的强大,跟大家分享一下,背景知识,nginx代理过程发现:nginx中也可以加入第三方扩展来增强其功能,通常就是下载该模块然后再重新编译nginx加入该模块,比如:https://github.com/openresty/headers-more-nginx-module此外还有一些打包好的nginx增强包,不用编译......
  • docker启动nginx https自签名证书配置
    启动测试应用时,有时需要自己配置证书签名:1使用系统自带opensslopensslreq\>-x509\>-nodes\>-days365\>-newkeyrsa:2048\>-keyoutexample.key\>-outexample.crt各参数含义:req:处理证书签署请求。-x509:生成自签名证书。-nodes:跳过为证书设置密码......
  • 腾讯云Linux服务器 前端Nginx+后端 项目部署
    一、前端项目部署1.安装nginx服务器:在root目录下创建services文件并下载nginx源文件【nginx-1.21.6.tar.gz】 建议尽量选择稳定版本下载  nginx官网下载地址​​​​cd/rootmkdirservicescdservicescurl-onginx-1.21.6.tar.gzhttp://nginx.org/download/......
  • ubutun部署nginx 静态页面资源
    本教程使用安装包安装安装前你需要安装以下依赖apt-getinstallgccapt-getinstalllibpcre3libpcre3-devapt-getinstallzlib1gzlib1g-devsudoapt-getinstallopensslsudoapt-getinstalllibssl-dev说明:Nginx是高度自由化的Web服务器,它的功能是由许多模块来支持......
  • Docker Swarm部署nginx+api-service实现负载均衡
    背景介绍api服务开发完毕之后如何快速部署实现负载均衡。如果不是重量级的项目,非常建议使用DockerSwarm来实现,非常简单高效且稳定。关于DockerSwarm集群的构建详见我的另一篇博客:DockerSwarm集群搭建相关文件准备(这里以一个ocr服务为例)docker-compose.ymlversion:'......
  • nginx与php pathinfo(TP5框架适用)
    最近在试用fastadmin这一个框架,是由tp5进行二开后的一款框架。fastadmin里面有个代码,要获取pathinfo否则会重定向到登录页,而默认的nginx配置获取的pathinfo一直为空,就导致无法正常进入后台。通过查找资料后,发现pathinfo是php的产物,与nginx无关。如果要让nginx把pathinfo环境变量......
  • nginx日志切割备份
    直接上脚本#!/bin/bash#源日志目录logs_path="/logs/nginx"#备份日志目录back_logs_path="${logs_path}/backup/$(date-d'yesterday'+'%F')"#创建备份目录,以日期命名,注意,每天零点整切割,开始记录新的一天的日志,备份目录应该是昨天mkdir-p${back_logs_path}#重命......