Registrator+Consul+Consul-template+HaProxy实现动态修改Haproty配置文件

2017-12-30 11:36:18来源:oschina作者:xiaomin0322人点击

分享
Registrator+Consul+Consul-template+HaProxy实现动态修改Haproty配置文件 博客分类: mesos
实现需求:

用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镜像
[[email protected] ~]# docker pull docker.io/gliderlabs/consul-server
启动Consul-server
[[email protected] ~]# docker run -d --name=consul --net=host docker.io/gliderlabs/consul-server -bootstrap -bind=192.168.200.8

宿主机来做consul


下载consul包,并解压


[[email protected] ~]# wget https://releases.hashicorp.com/consul/0.6.4/consul_0.6.4_linux_386.zip
[[email protected] ~]# unzip consul_0.6.4_linux_386.zip
[[email protected] ~]# mv consul /usr/bin/

下载consul-template包,并解压


[[email protected] ~]# wget https://releases.hashicorp.com/consul-template/0.15.0/consul-template_0.15.0_linux_386.zip
[[email protected] ~]# unzip consul-template_0.15.0_linux_386.zip
[[email protected] ~]# mv consul-template /usr/bin/

安装HaProxy并启动


[[email protected] ~]# yum -y install haproxy
[[email protected] ~]# systemctl start haproxy

创建consul服务器配置目录


[[email protected] ~]# mkdir /config

编写agent和server的json文件


[[email protected] ~]# 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"
}
}
[[email protected] ~]# vi /config/server.json
添加内容如下:
{
"ui": true,
"dns_config": {
"allow_stale": false
}
}

启动consul单节点服务器,当然,你consul服务器节点多的话也可以做consul集群。


[[email protected] ~]# consul agent -server -config-dir=/config -bootstrap -bind=192.168.200.8 &

Postil:可用consul members查看consul集群节点


Slave1-server


获取Registrator镜像


[[email protected] ~]# docker pull gliderlabs/registrator:latest

启动Registrator


Postil:这种启动方式是注册到Consul的key/value


[[email protected] ~]# 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


启动个容器


[[email protected] ~]# docker run -d -P --name=test --net=bridge image/nginx

进入Consul UI界面查看


http://192.168.200.8:8500/ui/#/dc1/kv/



ZK-server


创建consul配置目录


[[email protected] ~]# mkdir -p /data/cfg/consul
[[email protected] ~]# 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模版


[[email protected] ~]# vi /etc/haproxy/haproxy.ctmpl
添加内容如下:
global
log127.0.0.1 local2chroot/var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
grouphaproxy
daemon# turn on stats unix socket
stats socket /var/lib/haproxy/stats
defaults
modehttp
log global
optionhttplog
optiondontlognull
option http-server-close
option forwardforexcept 127.0.0.0/8
optionredispatch
retries 3
timeout http-request10s
timeout queue1m
timeout connect10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check10s
maxconn 3000frontendmain *:80
acl url_staticpath_beg-i /static /images /javascript /stylesheets
acl url_staticpath_end-i .jpg .gif .png .css .jsuse_backend static if url_static
default_backendappbackend static
balance roundrobin
serverstatic 127.0.0.1:4331 checkbackend 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


[[email protected] ~]#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

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台