当网站业务量急剧增大的时候,机器的扩容缩容就是家常便饭,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="192.168.2.201:49162" curl -XPUT http://192.168.2.202:2379/v2/keys/web/bbs.test.com/prickly_blackwell -d value="192.168.2.201:49162" curl -XPUT http://192.168.2.202:2379/v2/keys/web/admin.test.com/prickly_blackwell -d value="192.168.2.201:49162"
etcd注册格式
http://192.168.2.202:2379/v2/keys/web/DOMAIN_NAME/SERVER_NAME -d value="IP:PORT"
http://192.168.2.202:2379/v2/keys/web/域名/服务器名 -d value="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
在此感谢!
时间: 2024-10-16 21:23:36