Consul+Registrator+Consul-template实现动态修改nginx配置文件

实现需求

用nginx做负载均衡,手动的方式是在upstream中添加或删除后端服务器,比较麻烦.

通过Registrator收集需要注册到Consul作为Nginx后端服务器信息然后注册到Consul key/value.Consul-template去Consul key/value中读取信息,然后自动修改Nginx配置文件并平滑重启Nginx.不需要修改nginx.conf

环境

192.168.0.149 Mesos-master Zookeeper Consul-server Consul-template Nginx
192.168.0.161 Mesos-master Zookeeper Marathon
192.168.0.174 Mesos-master Zookeeper
192.168.0.239 Mesos-salve Registrator

步骤

mesosphere和Nginx搭建过程省略

启动Consul-server

 docker run -d --name=consul --net=host gliderlabs/consul-server -bootstrap -bind=192.168.0.149

启动Registrator

NOTE:这种启动方式是注册到Consul的key/value

 docker run -d  --name=registrator     --net=host   --volume=/var/run/docker.sock:/tmp/docker.sock    gliderlabs/registrator:latest consulkv://localhost:8500/hello

安装Consul-template

wget -c  https://releases.hashicorp.com/consul-template/0.12.0/consul-template_0.12.0_linux_amd64.zip
unzip -d . consul-template_0.12.0_linux_amd64.zip    
cp consul-template    /usr/local/bin/.

配置nginx.conf模板

NOTE:

consul-template和nginx必须装到一台机器,因为consul-template需要动态修改nginx配置文件

只需要把nginx.conf默认配置复制到这里,然后修改upstream段.

#vim /root/nginx_web.ctmpl 
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    
   upstream app {
     {{range $key, $pairs := tree "hello/" | byKey}}{{range $serverid, $pair := $pairs}}
     server {{.Value}}; weight=1 {{end}}{{end}}
   }
   server {
        listen       80;
        server_name  localhost;
        
       location / {
         http://app;
       }

        }

    }

}

启动consul-template

consul-template -consul 192.168.0.149:8500 -template /root/nginx_web.ctmpl:/usr/local/nginx/conf/nginx.conf:"/usr/local/nginx/sbin/nginx -s reload"

测试:

用marathon启动一个nginx容器,看registrator是否注册到consul,然后看consul-template是否自动添加了这个后端服务器到/usr/local/nginx/conf/nginx.conf

时间: 2024-08-25 07:39:53

Consul+Registrator+Consul-template实现动态修改nginx配置文件的相关文章

Consul+Registrator+Docker实现服务发现(nginx反向代理)

环境准备: consul:高可用,分布式的服务发现的工具 master主机-docker01:172.16.1.30 node01主机-docker02:172.16.1.31node02主机-docker03:172.16.1.32 注意:因为是测试环境,所以暂时关闭防火墙,禁用selinux,如果是对外网发布的服务器,是不可以关闭防火墙,可以设置iptables规则. 项目操作: (1)docker01上部署consul:下载或上传consul_1.5.1_linux_amd64软件包: [

新手学分布式-动态修改Nginx配置的一些想法

本人是分布式的新手,在实际工作中遇到了需要动态修改nginx的需求,因此写下实现过程中的想法.Nginx功能强大且灵活,所以这些权当抛砖引玉,希望可以得到大家的讨论和指点.(具体代码在 https://andy-zhangtao.github.io/nginx2svg/ ) 如何动态配置Nginx参数 Nginx参数众多,并且配置是非灵活,因此要达到完美的自动化配置是一件很有挑战性的事情,这个工具并不能十分完美的自动化调整参数.目前支持自动化修改的参数有: server upstream pro

Registrator+Consul+Consul-template+HaProxy实现动态修改Haproty配置文件

实现需求: 用Haproxy做负载均衡,手动方式在配置文件中添加或删除节点服务器信息,比较麻烦. 通过Registrator收集需要注册到Consul作为Haproxy节点服务器的信息,然后注册到Consul key/value. Consul-template去Consul key/value中读取信息,然后自动修改Haproxy配置文件,并重载Haproxy.不需要修改haproxy.cfg. 集群环境: Postil:Mesos集群搭建过程此处省略 关闭selinux和防火墙 setenf

修改nginx配置文件支持Thinkphp pathinfo以及rewrite模式

server { listen 80; server_name localhost; include /etc/nginx/default.d/*.conf; root /usr/share/nginx/thinkercms; location / { index index.php; if (!-e $request_filename) { rewrite ^/(.*)$ /index.php/$1 last; break; } } location ~ .+\.php($|/) { set

使用Ansible的Playbook修改nginx配置文件

目录结构: 文件内容 tasks 目录下的"file.yml"文件,内容如下: tasks目录下的"main.yml" templates目录下的"nginx.conf.j2" {% if nginx_use_proxy %} {% for proxy in nginx_proxies %} upstream {{ proxy.name }} { server {{ ansible_eth0.ipv4.address }}:{{ proxy.po

Docker + Consul + registrator实现服务发现及nginx反向代理

一. 架构设计 在现实中,我们一直渴望着追求提供高质量.高可用的服务架构体系,同时减少不必要的部署和维护代价,减少容错率.面对如此高的要求,可以有两种架构方案:Docker+Etcd+Confd+NginxDocker+Consul+Nginx本文中我们主要来介绍 Docker+Etcd+Confd+Nginx方案,此方案更加高效.快捷,并且维护代价和容错率更低,分布式支持力度更强,如下图所示: 上面示意图的大概流程如下:1.docker01主机上以二进制包的方式部署consul服务并后台运行,

linux下Nginx配置文件(nginx.conf)配置设置详解(windows用phpstudy集成)

linux备份nginx.conf文件举例: cp /usr/local/nginx/nginx.conf /usr/local/nginx/nginx.conf-20171111(日期) 在进程列表里 面找master进程,它的编号就是主进程号. ps -ef | grep nginx 查看进程 cat /usr/local/nginx/nginx.pid 每次修改完nginx文件都要重新加载配置文件linux命令: /usr/local/nginx -t //验证配置文件是否合法 若ngin

nginx之旅第一篇:nginx下载安装、nginx配置文件详解、nginx默认网站

一.nginx下载安装 版本nginx 1.15.5 系统环境centos7.5(本机ip192.168.199.228) 关闭selinux 和防火墙firewall 1.下载 wget http://nginx.org/download/nginx-1.15.5.tar.gz -P /usr/src 2.安装 安装大概过程 配置---编译---安装 配置 1)检查环境 是否 满足安装条件 依赖解决 2)指定安装方式 配置文件 命令文件 各种文件放哪里 开启模块功能[内 置模块 三方模块] 3

基于Consul+Registrator+Nginx实现容器服务自动发现的集群框架

我们先来看一下服务发现常用的框架有哪些: zookeeper eureka etcd consul 这里就不挨个来介绍它们了,本文中主要以consul为主,如果你在大量接触或使用微服务的话,你可能会碰到一个问题?当你创建的服务数量越来越多时,这些服务之间的通信便越难管理,而且维护代价会越来越高.consul可以给你答案,我们一起来了解一下consul: 1.了解consul Consul是一个分布式,高度可用且支持多数据中心的服务发现,配置和编排工具. Consul支持大规模部署,配置和维护面向