实现需求:
用 Haproxy 做负载均衡,手动方式在配置文件中添加或删除节点服务器信息,比较麻烦。
通过 Registrator 收集需要注册到 Consul 作为 Haproxy 节点服务器的信息,然后注册到 Consul key/value 。
Consul-template 去 Consul key/value 中读取信息 , 然后自动修改 Haproxy 配置文件,并重载 Haproxy 。不需要修改 haproxy.cfg 。
集群环境:
Postil : Mesos 集群搭建过程此处省略
关闭 selinux 和防火墙
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
systemctl stop firewalld && systemctl disable firewalld
ZK-server
容器来做consul:
获取Consul-server镜像
[root@zk-server ~]# docker pull docker.io/gliderlabs/consul-server
启动Consul-server
[root@zk-server ~]# docker run -d --name=consul --net=host docker.io/gliderlabs/consul-server -bootstrap -bind=192.168.200.8
宿主机来做 consul
下载 consul 包,并解压
[root@zk-server ~]# wget https://releases.hashicorp.com/consul/0.6.4/consul_0.6.4_linux_386.zip
[root@zk-server ~]# unzip consul_0.6.4_linux_386.zip
[root@zk-server ~]# mv consul /usr/bin/
下载 consul-template 包,并解压
[root@zk-server ~]# wget https://releases.hashicorp.com/consul-template/0.15.0/consul-template_0.15.0_linux_386.zip
[root@zk-server ~]# unzip consul-template_0.15.0_linux_386.zip
[root@zk-server ~]# mv consul-template /usr/bin/
安装 HaProxy 并启动
[root@zk-server ~]# yum -y install haproxy
[root@zk-server ~]# systemctl start haproxy
创建 consul 服务器配置目录
[root@zk-server ~]# mkdir /config
编写 agent 和 server 的 json 文件
[root@zk-server ~]# vi /config/agent.json
添加内容如下:
{
"client_addr": "0.0.0.0",
"data_dir": "/data",
"leave_on_terminate": true,
"dns_config": {
"allow_stale": true,
"max_stale": "1s"
}
}
[root@zk-server ~]# vi /config/server.json
添加内容如下:
{
"ui": true,
"dns_config": {
"allow_stale": false
}
}
启动 consul 单节点服务器,当然,你 consul 服务器节点多的话也可以做 consul 集群。
[root@zk-server ~]# consul agent -server -config-dir=/config -bootstrap -bind=192.168.200.8 &
Postil :可用 consul members 查看 consul 集群节点
Slave1-server
获取 Registrator 镜像
[root@slave1 ~]# docker pull gliderlabs/registrator:latest
启动 Registrator
Postil :这种启动方式是注册到 Consul 的 key/value
[root@slave1 ~]# 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 后面跟 registration 所属的主机 IP, 一定要设置此属性 , 否则服务 IP 会显示为 127.0.0.1
测试 Registrator 是否把本机容器注册到 Consul key/value
启动个容器
[root@slave1 ~]# docker run -d -P --name=test --net=bridge image/nginx
进入Consul UI界面查看
http://192.168.200.8:8500/ui/#/dc1/kv/
ZK-server
创建 consul 配置目录
[root@zk-server ~]# mkdir -p /data/cfg/consul
[root@zk-server ~]# vi /data/cfg/consul/tmpl.json
添加内容如下:
consul = "127.0.0.1:8500"
template {
source = "/etc/haproxy/haproxy.ctmpl"
destination = "/etc/haproxy/haproxy.cfg"
command = "systemctl reload haproxy"
}
编写 haproxy 模版
[root@zk-server ~]# vi /etc/haproxy/haproxy.ctmpl
添加内容如下:
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/stats
defaults
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}}
启动 consul-template
[root@zk-server ~]#consul-template -config /data/cfg/consul/tmpl.json > consul-template.out 2>&1 &
用 marathon 启动一个 nginx 容器,看 registrator 是否注册到 consul, 然后看 consul-template 是否自动添加了这个后端服务器到 /etc/haproxy/haproxy.cfg
访问 HaProxy_IP
http://www.tuicool.com/articles/IniY7vi