当网站业务量急剧增大的时候,机器的扩容缩容就是家常便饭,Haproxy作为负载均衡器的变更频率就急剧加大,特别是Docker的出现,这个时候再也不能通过vim修改配置文件的形式去对业务进行变更。
etcd:k/v形式的键值存储,用于存放配置
confd:根据从etcd中获取的配置,对haproxy进行修改
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
#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
timeout connect 5000
timeout client 50000
timeout server 50000
frontend webin
bind :80
mode http
{{ $domains := lsdir
"/web"
}}
{{range $domain := $domains}}
acl {{$domain}} hdr(host) -i {{$domain}}
`end`
{{range $domain := $domains}}
use_backend {{$domain}}_cluster
if
{{$domain}}
`end`
{{range $domain := $domains}}
backend {{$domain}}_cluster
option httpchk HEAD
/health
.html HTTP
/1
.1\r\nHost:{{$domain}}
cookie SERVERID insert indirect nocache
{{$servers :=
printf
"/web/%s/*"
$domain}}{{range gets $servers}}
server {{base .Key}} ``.`Value` cookie {{base .Key}} 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
&
|
注册服务器
|
1
2
3
|
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
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
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
option httpchk HEAD
/health
.html HTTP
/1
.1\r\nHost:admin.
test
.com
cookie SERVERID insert indirect nocache
server prickly_blackwell 192.168.2.201:49162 cookie prickly_blackwell check inter 5000 rise 2 fall 3 weight 1
backend bbs.
test
.com_cluster
option httpchk HEAD
/health
.html HTTP
/1
.1\r\nHost:bbs.
test
.com
cookie SERVERID insert indirect nocache
server prickly_blackwell 192.168.2.201:49162 cookie prickly_blackwell check inter 5000 rise 2 fall 3 weight 1
backend
option httpchk HEAD
/health
.html HTTP
/1
.1\r\nHost:www.
test
.com
cookie SERVERID insert indirect nocache
server prickly_blackwell 192.168.2.201:49162 cookie prickly_blackwell check inter 5000 rise 2 fall 3 weight 1
|
本文参考:
http://blog.liuts.com/post/242/
https://yq.aliyun.com/articles/8708
在此感谢!
本文转自 rong341233 51CTO博客,原文链接:http://blog.51cto.com/fengwan/1899964