Giter VIP home page Giter VIP logo

confd_haproxy's Introduction

confd_haproxy

proxy tcp port in a dynamic way with confd and haproxy, it can be used to proxy alive tcp port based on the confd read.

current support

confd_haproxy support tcp/ip layer's proxy, which based on haproxy, currently we just support:

mysql slave
memcached. 

Dependency

perl-libwww-perl
perl-JSON
perl-Config-IniFiles
perl-DBD-MySQL
perl-Cache-Memcached

How does confd_haproxy work

We use consul as the confd backend. Currently, you can use bin/checkmysqlslave to check alive mysql slave, such as the following workflow:

                             +---------+    <update>  +-------------+
                             |  consul |   <--------- | bin/check.. |
                             +---------+              +-------------+
                                |                              |
                                | <confd update/reload>        | <check servers> 
                                |                              |
                                |                     +---------------+
       +---------+          +---------+               |               |
       | request |   ---->  | haproxy |   --------->  | mysql slave   |
       +---------+          +---------+               |               |
                                                      +---------------+

checkmysqlslave check and update consul entry when current MySQL slave server is abnormal, then confd capture the consul entry change, update the haproxy.cfg based on consul entry, and execute reload_cmd in confd/conf.d/haproxy_mysql.toml if check_cmd is successed.

How to use

consul acl

checkmysqlslave use 'haproxy/mysqlslave/slavexxxx' as the key name, where xxxx is mysql port. if consul have set consul_acl, you must enable permission to read key 'haproxy/mysqlslave/slavexxxx', such as acl rule:

{
  "ID": "anonymous",
  "Name": "Anonymous Token for haproxy",
  "Type": "client",
  "Rules": "{\"key\":{\"haproxy/\":{\"policy\":\"deny\"}},\"operator\":\"read\"}"
}

start confd

  1. first of all you must export consul token if your consul have set consul_acl:
export CONSUL_HTTP_TOKEN=e95597e0-4045-11e7-a9ef-b6ba84687927
  1. start confd:
# confd -onetime -backend consul -node localhost:8500
2017-10-16T16:06:06+08:00 cz-test1 confd[16454]: INFO Backend set to consul
2017-10-16T16:06:06+08:00 cz-test1 confd[16454]: INFO Starting confd
2017-10-16T16:06:06+08:00 cz-test1 confd[16454]: INFO Backend nodes set to localhost:8500
2017-10-16T16:06:06+08:00 cz-test1 confd[16454]: INFO /etc/haproxy/haproxy.cfg has md5sum af246453ab8f8e3c168dd63f055e9183 should be 0c5f58a6e13d6f2b45292b88d554b7f5
2017-10-16T16:06:06+08:00 cz-test1 confd[16454]: INFO Target config /etc/haproxy/haproxy.cfg out of sync
2017-10-16T16:06:07+08:00 cz-test1 confd[16454]: INFO Target config /etc/haproxy/haproxy.cfg has been updated

or the watch option if you want to continuous check consul entries:

confd -backend consul -node localhost:8500 -watch

run checkmysqlslave

you can run checkmysqlslave to check and update the alive mysql slave info:

# perl bin/checkmysqlslave --conf etc/db.conf --tag slave3301 --consul localhost:8500 --token e95597e0-4045-11e7-a9ef-b6ba84687927
[2017-10-16T16:03:33] connect to 10.0.21.5, 3301, monitor, xxxxxxxx ...
[2017-10-16T16:03:34] slave3301 in consul is ok
[2017-10-16T16:03:35] connect to 10.0.21.5, 3301, monitor, xxxxxxxx ...
[2017-10-16T16:03:35] slave3301 in consul is ok
....
....

run checkmemcached

run checkmemcached to check and update the alive memcached info:

perl checkmemcached --server 10.0.21.5:11211,10.0.21.7:11211 --tag mem11211 --consul localhost:8500 --token e95597e0-4045-11e7-a9ef-b6ba84687927
[2017-10-17T17:03:21] set new alive 10.0.21.7:11211 with tag mem11211 ok
[2017-10-17T17:03:22] current 10.0.21.7:11211 is ok
....
....

toolkit

the bin dir include some useful script to check the backend servers which behind at the haproxy:

checkmysqlslave - check mysql whether is slave or not, set new slave info to consul when current slave is abnormal.
checkmemcached - check memcached and ensure only one alive memcached in consul when current memcached is abnormal.
...

consul key format

read quick-start for more info.

checkmysqlslave

the value in json format, and proxyport is port + 10050 by default, all key name with a prefix name 'haproxy/mysqlslave':

curl -X PUT -d '{"name":"slave3301", "host":"10.0.21.5", "port":3301, "proxyport":13351}' http://localhost:8500/v1/kv/haproxy/mysqlslave/slave3301

checkmemcached

the value in json format, and proxyport is port + 5000 by default, all key name with a prefix name 'haproxy/memcached':

curl -X PUT -d '{"name":"mem11211", "host":"10.0.21.7", "port":11211, "proxyport":16211}' http://localhost:8500/v1/kv/haproxy/memcached/mem11211

TODO list:

  • mysql slave proxy (bin/checkmysqlslave)
  • redis master proxy (TODO)
  • memcached proxy (bin/checkmemcached)

confd_haproxy's People

Contributors

arstercz avatar

Watchers

 avatar

Forkers

w796933

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.