Consul+registrator+consul-template implement dynamic modification of nginx configuration file

Source: Internet
Author: User

Meet your needs

Using Nginx to do load balancing, the manual way is to add or remove the backend server in the upstream, more trouble.

Through Registrator collection needs to register to consul as Nginx backend server information and then register to consul Key/value. Consul-template go to Consul key/value to read the information, and then automatically modify the Nginx configuration file and smooth restart Nginx. No need to modify nginx.conf

Environment

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


Steps

Mesosphere and Nginx construction process omitted

Start Consul-server

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

Start Registrator

Note: This startup mode is registered to Consul's Key/value

Docker run-d--name=registrator--net=host--volume=/var/run/docker.sock:/tmp/docker.sock Gliderlabs/registrato R:latest Consulkv://localhost:8500/hello

Installing Consul-template

Wget-c https://releases.hashicorp.com/consul-template/0.12.0/consul-template_0.12.0_linux_amd64.zipunzip-d. Consul-template_0.12.0_linux_amd64.zip CP consul-template/usr/local/bin/.

Configuring nginx.conf Templates

Note:

Consul-template and Nginx must be mounted to a machine because consul-template needs to dynamically modify the Nginx configuration file

Just copy the nginx.conf default configuration here, and then modify the upstream segment.

#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;       }         }    }}

Start 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 "

Test:

Start an nginx container with marathon, see if Registrator is registered to consul, and see if consul-template automatically adds this backend server to/usr/local/nginx/conf/nginx.conf




This article is from the "Ming Linux blog" blog, be sure to keep this source http://zhaijunming5.blog.51cto.com/10668883/1768321

Consul+registrator+consul-template implement dynamic modification of nginx configuration file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.