首页 > 代码库 > 通过Etcd+Confd自动管理Haproxy(多站点)
通过Etcd+Confd自动管理Haproxy(多站点)
当网站业务量急剧增大的时候,机器的扩容缩容就是家常便饭,Haproxy作为负载均衡器的变更频率就急剧加大,特别是Docker的出现,这个时候再也不能通过vim修改配置文件的形式去对业务进行变更。
etcd:k/v形式的键值存储,用于存放配置
confd:根据从etcd中获取的配置,对haproxy进行修改
#confd的安装 wget https://github.com/kelseyhightower/confd/releases/download/v0.11.0/confd-0.11.0-linux-amd64 mv confd* /usr/local/bin/confd chmod +x /usr/local/bin/confd /usr/local/bin/confd -version #etcd的安装 wget https://github.com/coreos/etcd/releases/download/v3.1.1/etcd-v3.1.1-linux-amd64.tar.gz tar zxf etcd-v3.1.1-linux-amd64.tar.gz cd etcd-v3.1.1-linux-amd64 cp etcd* /bin/ /bin/etcd -version #etcd的启动 mkdir /data/etcd etcd --name etcdserver --data-dir /data/dir --listen-peer-urls ‘http://192.168.2.202:2380‘ --listen-client-urls ‘http://192.168.2.202:2379,http://127.0.0.1:2379‘ --initial-advertise-peer-urls ‘http://192.168.2.202:2380‘ --advertise-client-urls ‘http://192.168.2.202:2379,http://127.0.0.1:2379‘ & #confg的配置 mkdir -p /etc/confd/{conf.d,templates} vim /etc/confd/conf.d/haproxy.toml [template] src = "haproxy.cfg.tmpl" dest = "/etc/haproxy/haproxy.cfg" keys = [ "/web", ] reload_cmd = "/etc/init.d/haproxy reload" /etc/confd/templates/haproxy.cfg.tmpl global log 127.0.0.1 local3 maxconn 5000 uid 99 gid 99 daemon defaults log 127.0.0.1 local3 mode http option dontlognull retries 3 option redispatch maxconn 2000 contimeout 5000 clitimeout 50000 srvtimeout 50000 frontend webin bind :80 mode http {{ $domains := lsdir "/web"}} {{range $domain := $domains}} acl is_{{$domain}} hdr(host) -i {{$domain}} {{end}} {{range $domain := $domains}} use_backend {{$domain}}_cluster if is_{{$domain}} {{end}} {{range $domain := $domains}} backend {{$domain}}_cluster cookie SERVERID insert indirect nocache {{$servers := printf "/web/%s/*" $domain}}{{range gets $servers}} server {{base .Key}} {{.Value}} check inter 5000 rise 2 fall 3 weight 1 {{end}} {{end}} #启动confd /usr/local/bin/confd -interval 5 -node ‘192.168.2.202:2379‘ -confdir /etc/confd &
注册服务器
curl -XPUT http://192.168.2.202:2379/v2/keys/web/www.test.com/prickly_blackwell -d value="http://www.mamicode.com/192.168.2.201:49162" curl -XPUT http://192.168.2.202:2379/v2/keys/web/bbs.test.com/prickly_blackwell -d value="http://www.mamicode.com/192.168.2.201:49162" curl -XPUT http://192.168.2.202:2379/v2/keys/web/admin.test.com/prickly_blackwell -d value="http://www.mamicode.com/192.168.2.201:49162"
etcd注册格式
http://192.168.2.202:2379/v2/keys/web/DOMAIN_NAME/SERVER_NAME -d value="http://www.mamicode.com/IP:PORT"
http://192.168.2.202:2379/v2/keys/web/域名/服务器名 -d value="http://www.mamicode.com/IP:端口"
查看/etc/haproxy/haproxy.cfg
global log 127.0.0.1 local3 maxconn 5000 uid 99 gid 99 daemon defaults log 127.0.0.1 local3 mode http option dontlognull retries 3 option redispatch maxconn 2000 contimeout 5000 clitimeout 50000 srvtimeout 50000 frontend webin bind :80 mode http acl is_admin.test.com hdr(host) -i admin.test.com acl is_bbs.test.com hdr(host) -i bbs.test.com acl is_www.test.com hdr(host) -i www.test.com use_backend admin.test.com_cluster if is_admin.test.com use_backend bbs.test.com_cluster if is_bbs.test.com use_backend www.test.com_cluster if is_www.test.com backend admin.test.com_cluster cookie SERVERID insert indirect nocache server prickly_blackwell 192.168.2.201:49162 check inter 5000 rise 2 fall 3 weight 1 backend bbs.test.com_cluster cookie SERVERID insert indirect nocache server prickly_blackwell 192.168.2.201:49162 check inter 5000 rise 2 fall 3 weight 1 backend www.test.com_cluster cookie SERVERID insert indirect nocache server prickly_blackwell 192.168.2.201:49162 check inter 5000 rise 2 fall 3 weight 1
本文参考:
http://blog.liuts.com/post/242/
https://yq.aliyun.com/articles/8708
在此感谢!
本文出自 “枫林晚” 博客,请务必保留此出处http://fengwan.blog.51cto.com/508652/1899964
通过Etcd+Confd自动管理Haproxy(多站点)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。