docker-consul

docker consul

一、Consul概述

1、consul简介

consul是一个服务发现和注册的工具,具有分布式、搞扩展性能特点。
docker-consul

2、consul特点

(1)支持健康检查,允许储存值对
(2)基于Golong语言,可移植性强
(3)支持ACL访问量控制(与docker等轻量级容器可无缝配合)

3、docker consul容器服务更新与发现

docker-consul
解读:建立consul服务
(1)每个提供服务的节点上都要部署和允许consul的agent
(2)consul agent有两个运行模式(server、client)
(3)server和cilent只是consul群集层面的区分,与搭建在cluster之上的应用服务无关

二、部署

1、server部署(192.168.177.33)

(需要安装docker-ce、compose、consul、consul-template)

mkdir /root/consul
cd consul
rz consul_0.9.2_linux_amd64.zip

unzip consul_0.9.2_linux_amd64.zip

mv consul /usr/bin

docker-consul

consul agent \
-server \
-bootstrap \
-ui \
-data-dir=/var/lib/consul-data \
-bind=192.168.177.33 \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &

consul members
consul info | grep leader

docker-consul

2、client部署(192.168.177.8)

容器服务自动加入nginx集群
安装Gliderlabs/Registrator Gliderlabs/Registrator
可检查容器运行状态自动注册,还可注销docker容器的服务 到服务配置中心
目前支持Consul、Etcd和SkyDNS2

在192.168.177.8节点上,执行以下操作

docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
-ip=192.168.177.8 \
consul://192.168.177.33:8500

docker-consul
docker-consul

systemctl restart docker
docker run -itd -p:81:80 --name test-01 -h test01 nginx
docker run -itd -p:82:80 --name test-02 -h test02 nginx
docker run -itd -p:83:80 --name test-03 -h test03 httpd
docker run -itd -p:84:80 --name test-04 -h test04 httpd

docker-consul
docker-consul
打开浏览器访问http://192.168.177.33:8500
docker-consul
docker-consul

3、配置template模块自动更新

server(192.168.177.33)

cd consul/
vim nginx.ctmpl

upstream http_backend {
 {{range service "nginx"}}
  server {{.Address}}:{{.Port}};
  {{end}}
}

server {
 listen 100;
 server_name localhost 192.168.177.33;
 access_log /var/log/nginx/lic.com-access.log;
 index index.html index.php;
 location / {
        proxy_set_header HOST $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Client-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://http_backend;
  }     
}

====>:wq

docker-consul

yum -y install gcc pcre-devel zlib-devel
rz nginx-1.12.0.tar.gz
tar zxvf nginx-1.12.0.tar.gz -C /opt
cd /opt/nginx-1.12.0

./configure --prefix=/usr/local/nginx

make && make install

docker-consul
docker-consul

vim /usr/local/nginx/conf/nginx.conf
//19     include vhost/*.conf;

docker-consul

cd /usr/local/nginx/conf/
mkdir vhost
mkdir /var/log/nginx

/usr/local/nginx/sbin/nginx

cd /opt
rz consul-template_0.19.3_linux_amd64.zip

unzip consul-template_0.19.3_linux_amd64.zip

docker-consul

mv consul-template /usr/bin
consul-template -consul-addr 192.168.177.33:8500 -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/lic.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info

docker-consul
再打开一个终端
docker-consul

192.168.177.8

docker run -itd -p:85:80 --name test-05 -h test05 nginx

docker-consul
docker-consul

4、测试访问代理服务器

http://192.168.177.33:100/

docker logs -f test-01
docker logs -f test-02
docker logs -f test-05
上一篇:无法正常启动 template:[ERR] (cli) failed to read template: open /root/consul/nginx.ctmpl: no such file or


下一篇:micro api 无法使用consul解决方法