nginx+consul实现动态负载均衡(五)

 对于社区版的nginx做到负载动态负载均衡主要有三种方案:

  1. tengine的dyups模块;
  2. 微博的upsync;
  3. openResty的balancer_by_lua

本篇讲解的是nginx添加nginx-upsync-module模块再结合consul,实现上游服务地址配置的动态切换。其整体实现结构如下:

Consul快速入门

Consul是一款开源的分布式服务注册与发现系统,通过HTTP API可以使得服务注册、发现实现起来非常简单,它支持如下特性:
服务注册:服务实现者可以通过HTTP API或DNS方式,将服务注册到Consul。
服务发现:服务消费者可以通过HTTP API或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_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 出现以下信息就说明安装成功

[root@localhost soft] ./consul

usage: consul [--version] [--help] <command> [<args>]

Available commands are:

    agent          Runs a Consul agent

    configtest     Validate config file

    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

    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

    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

    snapshot       Saves, restores and inspects snapshots of Consul server state

    version        Prints the Consul version 

    watch          Watch for changes in Consul

4.启动consul

我的linux Ip地址192.168.167.128

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

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

6.浏览器访问192.168.167.128:8500

7.使用PostMan 注册Http服务

http://192.168.167.128:8500/v1/catalog/register

参数1:

{"Datacenter": "dc1",

 "Node":"tomcat", "Address":"192.168.5.165","Service": {"Id" :"192.168.5.165:8080", "Service": "order","tags": ["dev"], "Port": 8080}}

 参数2:

{"Datacenter": "dc1", "Node":"tomcat", "Address":"192.168.5.165","Service": {"Id" :"192.168.5.165:8081", "Service": "order","tags": ["dev"], "Port": 8081}}

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

7.发现Http服务

http://192.168.167.128:8500/v1/catalog/service/order

[

    {

        "ID": "",

        "Node": "tomcat",

        "Address": "192.168.5.165",

        "TaggedAddresses": null,

        "NodeMeta": null,

        "ServiceID": "192.168.5.165:8080",

        "ServiceName": "order",

        "ServiceTags": [

            "dev"

        ],

        "ServiceAddress": "",

        "ServicePort": 8080,

        "ServiceEnableTagOverride": false,

        "CreateIndex": 36,

        "ModifyIndex": 36

    },

    {

        "ID": "",

        "Node": "tomcat",

        "Address": "192.168.5.165",

        "TaggedAddresses": null,

        "NodeMeta": null,

        "ServiceID": "192.168.5.165:8081",

        "ServiceName": "order",

        "ServiceTags": [

            "dev"

        ],

        "ServiceAddress": "",

        "ServicePort": 8081,

        "ServiceEnableTagOverride": false,

        "CreateIndex": 37,

        "ModifyIndex": 37

    } 

]

nginx-upsync-module

nginx-upsync-module简介

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

nginx-upsync-module安装

下载文件

1.安装consul

作用:对动态负载均衡均配置实现注册

2.安装nginx-upsync-module

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

作用:nginx动态获取最新upstream信息

3.安装Nginx

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

     作用:实现反向代理、负载负载库

解压安装

unzip master.zip

unzip consul_0.7.1_linux_amd64.zip

如果解压出现该错误

-bash: unzip: 未找到命令

解决办法

yum -y install unzip

解压nignx

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-upsync-module模块

cd nginx-1.9.0

./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

Upstream 动态配置

##动态去consul 获取注册的真实反向代理地址

   upstream order-service{

        server 127.0.0.1:1111;

        upsync 192.168.212.134:8500/v1/kv/upstreams/order 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  localhost;

        location / {

            proxy_pass http:// order-service;

            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服务器出问题了,本地还有一个备份。

注意:替换 consul 注册中心地址

创建upsync_dump_path

mkdir /usr/local/nginx/conf/servers/

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

添加nginx  Upstream服务

1.使用linux命令方式发送put请求

curl -X PUT

http://192.168.167.128:8500/v1/kv/upstreams/order/192.168.212.1:8081

curl -X PUT

http://192.168.167.128:8500/v1/kv/upstreams/192.168.167.128/192.168.212.1:8081

负载均衡信息参数

{"weight":1, "max_fails":2, "fail_timeout":10, "down":0}

启动Nginx

Docker+Consul+Nginx+keepalived是一种常用的云原生架构方案,它结合了容器化、服务发现、负载均衡和高可用等多种技术手段,可用于构建高可用、弹性、可扩展的应用系统。 该方案的主要组件包括: 1. Docker:用于容器化应用程序和服务,提供了高效、轻量、可移植的应用打包和部署方式。 2. Consul:用于服务发现和配置管理,支持多数据中心、跨平台、高度可扩展的分布式系统。 3. Nginx:用于负载均衡和反向代理,支持高并发、高可用的流量分发。 4. keepalived:用于实现高可用的服务和节点,提供了基于 VRRP 协议的故障转移和自动切换功能。 在该方案中,Docker 容器作为应用程序和服务的运行环境,使用 Consul 进行服务注册和发现,并通过 Nginx 进行流量分发和负载均衡。同时,使用 keepalived 实现高可用的服务和节点,确保系统的稳定性和可用性。 项目描述可以按照以下步骤进行撰写: 1. 项目背景和目的:简要介绍本项目的背景和目的,说明为什么选择 Docker+Consul+Nginx+keepalived 方案。 2. 技术架构:详细介绍该方案的技术架构和组件,包括 Docker、ConsulNginx 和 keepalived 的作用和使用方式。 3. 系统功能:描述系统的主要功能和特点,包括服务发现、负载均衡、高可用等方面。 4. 实现方式:介绍系统的具体实现方式和实现步骤,包括 Docker 镜像的构建、应用程序的容器化、Consul 的配置和使用、Nginx 的配置和使用、keepalived 的配置和使用等。 5. 测试和验证:对系统进行测试和验证,验证系统的功能和性能是否符合预期,是否满足高可用和弹性的要求。 6. 总结和展望:对本项目进行总结和展望,分析该方案的优缺点和适用范围,展望未来的发展方向和趋势。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

知始行末

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值