Registrator+consul+consul-template+haproxy implementing dynamic Modification of Haproty configuration files

Source: Internet
Author: User
Tags node server docker run haproxy

Implementation requirements:

Using Haproxy To do load balancing, manually add or remove node server information in the configuration file, it is more troublesome.

Collect the information that needs to be registered to Consul as the Haproxy node server through Registrator, and then register to Consul Key/value .

Consul-template go to Consul key/value to read the information and then automatically modify the Haproxy configuration file, and reload Haproxy. You do not need to modify haproxy.cfg.

Cluster Environment:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/84/57/wKiom1eN1r3Rgw-PAAA8YyDm0b0809.png "title=" ' IH] Cycwu0zcbisvz_jg5q3.png "alt=" Wkiom1en1r3rgw-paaa8yydm0b0809.png "/>

Postil:Mesos cluster construction process omitted here


Turn off selinux and firewalls

Setenforce 0sed-i ' s/selinux=enforcing/selinux=disabled/'/etc/selinux/configsystemctl stop Firewalld && Systemctl Disable FIREWALLD


Zk-server

Container to do consul: Get Consul-server mirror [[email protected] ~]# Docker pull Docker.io/gliderlabs/consul-server start consul-server[[ Email protected] ~]# Docker run-d--name=consul--net=host docker.io/gliderlabs/consul-server-bootstrap-bind= 192.168.200.8


Host to do consul

Download the Consul package and unzip

[Email protected] ~]# wget https://releases.hashicorp.com/consul/0.6.4/consul_0.6.4_linux_386.zip[[email protected ] ~]# unzip Consul_0.6.4_linux_386.zip[[email protected] ~]# mv consul/usr/bin/



Download the consul-template package and unzip

[Email protected] ~]# wget https://releases.hashicorp.com/consul-template/0.15.0/consul-template_0.15.0_linux_ 386.zip[[email protected] ~]# unzip Consul-template_0.15.0_linux_386.zip[[email protected] ~]# mv consul-template/usr/ bin/


installation HaProxy and start

[[email protected] ~]# yum-y install haproxy[[email protected] ~]# systemctl start Haproxy


Create a Consul Server configuration directory

[Email protected] ~]# Mkdir/config


Writing json files for agents and servers

[[email protected] ~]# vi /config/agent.json  add content as follows:{          "Client_addr":  "0.0.0.0",         "Data_dir":   "/data",         "Leave_on_terminate": true,          "Dns_config": {                  "Allow_stale": true,                  "max_stale":  "1s"          }}[[email protected] ~]# vi /config/server.json add content as follows:{          "UI": true,         "Dns_ Config ": {                  "Allow_stale":  FALSE &Nbsp;      }} 


Start Consul single node server, of course, you Consul server node can also do more Consul cluster.

[Email protected] ~]# Consul agent-server-config-dir=/config-bootstrap-bind=192.168.200.8 &

Postil: Consul members are available to view Consul cluster nodes


Slave1-server

Get registrator image

[email protected] ~]# Docker pull Gliderlabs/registrator:latest

Start registrator

Postil: This startup mode is registered to Consul key/value

[email protected] ~]# Docker run-d--restart=always--name=registrator--net=host--volume=/var/run/docker.sock:/tmp/ Docker.sock Docker.io/gliderlabs/registrator-ip 192.168.200.10 Consulkv://192.168.200.8:8500/hello

Postil:-ip behind the host IP that registration belongs to, Be sure to set this property , Otherwise the service IP will be displayed as 127.0.0.1


Test Registrator whether to register the native container with the Consul Key/value

Start a container

[email protected] ~]# Docker run-d-P--name=test--net=bridge Image/nginx


Go to the Consul UI interface to view

http://192.168.200.8:8500/ui/#/dc1/kv/

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/84/59/wKioL1eN2n-BKahmAAAlbgipA10864.png "title=" 3.png " alt= "Wkiol1en2n-bkahmaaalbgipa10864.png"/>


Zk-server

Creating the Consul configuration directory

[[email protected] ~]# mkdir-p/data/cfg/consul[[email protected] ~]# Vi/data/cfg/consul/tmpl.json Add the following: Consul = "127. 0.0.1:8500 "template {Source ="/etc/haproxy/haproxy.ctmpl "Destination ="/etc/haproxy/haproxy.cfg "command =" system CTL Reload Haproxy "}


writing template

[[Email protected] ~]# vi /etc/haproxy/haproxy.ctmpl Add content as follows:global     log         127.0.0.1 local2      chroot      /var/lib/haproxy    pidfile      /var/run/haproxy.pid    maxconn     4000     user        haproxy    group        haproxy    daemon     #  turn on stats unix socket    stats socket /var/lib/haproxy/ statsdefaults    mode                     http    log                      global     option                   httplog    option                   dontlognull    option http-server-close     option forwardfor       except 127.0.0.0/ 8    option                   redispatch    retries                  3    timeout  http-request    10s    timeout queue            1m    timeout connect         10s     timeout client          1m     timeout server          1m     timeout http-keep-alive 10s    timeout check            10s    maxconn                  3000 frontend   main *:80    acl url_static        path_beg       -i /static /images /javascript / stylesheets    acl url_static       path_end        -i .jpg .gif .png .css .js     use_backend static           if url_static    default_ backend             app backend  Static    balance     roundrobin    server       static 127.0.0.1:4331 check backend app     balance     roundrobin     {{range  $key,   $pairs  := tree  "hello/"  | byKey}}{{range  $serverid,  $pair  :=   $pairs}}     server app {{. Value}} check inter 2000 fall 3 weight 1 {{end}}{{end}}


Start consul-template

[[Email protected] ~] #consul-template-config/data/cfg/consul/tmpl.json > Consul-template.out 2>&1 &


start an nginx container with marathon and see If Registrator is registered to Consul, and see consul-template whether the backend server is automatically added to the /etc/haproxy/haproxy.cfg

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/84/5A/wKiom1eN29Ch92f1AAAn-FZu8sk782.png "style=" float: none; "title=" 4.png "alt=" Wkiom1en29ch92f1aaan-fzu8sk782.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/84/59/wKioL1eN29DSwwZMAABEjcHBKnE132.png "style=" float: none; "title=" 5.png "alt=" Wkiol1en29dswwzmaabejchbkne132.png "/>

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/84/5A/wKiom1eN29HBhfyIAAFS6Sq96qE500.png "style=" float: none; "title=" 6.png "alt=" Wkiom1en29hbhfyiaafs6sq96qe500.png "/>


Visit haproxy_ip

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/84/59/wKioL1eN2_DwH-usAABMZxBsa6s864.png "title=" 7.png " alt= "Wkiol1en2_dwh-usaabmzxbsa6s864.png"/>




This article is from the "Ningofan" blog, make sure to keep this source http://renyongfan.blog.51cto.com/11693321/1827797

Registrator+consul+consul-template+haproxy implementing dynamic Modification of Haproty configuration files

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.