目录
1.概述
1.1什么是consul
Consul是HashiCorp公司推出的开源工具,Consul由Go语言开发,部署起来非常容易,只需要极少的可执行程序和配置文件,具有绿色、轻量级的特点。
Consul是分布式的、高可用的、可横向扩展的用于实现分布式系统的服务发现与配置。
Consul的作用
- 服务注册与发现(主要功能),提供HTTP和DNS两种发现方式
- 健康检查,支持多种协议,HTTP、TCP等
- Key/Value存储
- 支持多数据中心
- 基于Golong语言,可移植性强
- 支持ACL访问控制
- 与Docker等轻量级容器可无缝配合
1.2consul-template概述
- Consul-Template是一个守护进程,用于实时查询Consul集群信息
- Consul-Template可以更新文件系统上任意数量的指定模板,生成配置文件
- 更新完成以后,可以选择运行shell命令执行更新操作,重新加载Nginx。
- Consul-Template可以查询Consul中的服务目录、Key、Key-values等。
- 这种强大的抽象功能和查询语言模板可以使Consul-Template特别适合动态的创建配置文件。
- 例如:创建Apache/Nginx Proxy Balancers、Haproxy Backends
1.3registrator的作用
一个由Go语言编写的,针对docker使用的,可以用于检测容器状态,自动注册和注销docker容器的服务到服务配置中心。目前支持Consul、Etcd和SkyDNS2。
1.4docker容器
这个就不多说了,具体看之前的博客
2.搭建consul集群环境
架构图
案例环境
主机 | 操作系统 | IP地址 | 主要软件及版本 |
---|---|---|---|
consul | Centos 7 | 192.168.163.100 | Docker 、Consul、Consul-template |
registrator | Centos 7 | 192.168.163.150 | Docker、registrator |
案例需求
实现单机网络下容器与容器之间互通
使用Docker Compose创建容器
搭建Consul服务实现自动发现和更新
实验准备
两台主机配置好docker
2.1安装Consul
consul:192.168.163.100
编译安装consul
mkdir /root/consul
cd /root/consul
//放入安装包
unzip consul_0.9.2_linux_amd64.zip
mv consul /usr/bin
consul agent
-server
-bootstrap
-ui
-data-dir=/var/lib/consul-data
-bind=192.168.163.100
-client=0.0.0.0
-node=consul-server01 &> /var/log/consul.log &
查看集群信息
consul members
consul info | grep leader
这里查询到的8300端口用于集群内数据的读写和复制
通过httpd api获取集群信息
curl 127.0.0.1:8500/v1/status/peers //查看集群server成员
curl 127.0.0.1:8500/v1/status/leader //集群Raf leader
curl 127.0.0.1:8500/v1/catalog/services //注册的所有服务
curl 127.0.0.1:8500/v1/catalog/nginx //查看nginx服务信息
curl 127.0.0.1:8500/v1/catalog/nodes //集群节点详细信息
netstat -natp |grep consul
这5个端口的作用:
8300:集群内数据的读写和复制
8301:单个数据中心gossip协议通讯
8302:跨数据中心gossip协议通讯
8500:提供获取服务列表、注册服务、注销服务等HTTP接口;提供UI服务
8600:采用DNS协议提供服务发现功能
2.2配置容器服务自动加入nginx集群
registrator:192.168.163.150
(1)安装Gliderlabs/Registrator
docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
-ip=192.168.163.150 \
consul://192.168.163.100:8500
(2)测试服务
测试发现功能是否正常
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:91:80 --name test-03 -h test03 httpd
docker run -itd -p:92:80 --name test-04 -h test04 httpd
2.3验证http和nginx服务是否注册到consul
真机
浏览器输入http://192.168.163.100:8500,“单击NODES”,然后单击“consurl-server01”,会出现5个服务
consul:192.168.163.100
curl 127.0.0.1:8500/v1/catalog/services
- 1
2.4安装consul-template
consul:192.168.163.100
unzip consul-template_0.19.3_linux_amd64.zip
mv consul-template /usr/bin/
2.5准备template nginx模板文件
consul:192.168.163.100
vim /root/consul/nginx.ctmpl
upstream http_backend {
{
{
range service “nginx”}}
server {
{
.Address}}:{
{
.Port}};
{
{
end}}
}
server {
listen 81;
server_name localhost 192.168.163.100;
access_log /var/log/nginx/nginx01-access.log;
index index.html index.php;
location / {
proxy_set_header HOST
h
o
s
t
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
;
<
/
s
p
a
n
>
p
r
o
x
y
s
e
t
h
e
a
d
e
r
X
−
R
e
a
l
−
I
P
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
v
a
r
i
a
b
l
e
"
>
host</span><span class="token punctuation">;</span> proxy_set_header X-Real-IP <span class="token variable">
host</span><spanclass="tokenpunctuation">;</span>proxysetheaderX−Real−IP<spanclass="tokenvariable">remote_addr;
proxy_set_header Client-IP
r
e
m
o
t
e
a
d
d
r
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
;
<
/
s
p
a
n
>
p
r
o
x
y
s
e
t
h
e
a
d
e
r
X
−
F
o
r
w
a
r
d
e
d
−
F
o
r
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
v
a
r
i
a
b
l
e
"
>
remote_addr</span><span class="token punctuation">;</span> proxy_set_header X-Forwarded-For <span class="token variable">
remoteaddr</span><spanclass="tokenpunctuation">;</span>proxysetheaderX−Forwarded−For<spanclass="tokenvariable">proxy_add_x_forwarded_for;
proxy_pass http://http_backend;
}
}
2.6部署nginx
consul:192.168.163.100
编译安装nginx
yum install -y gcc pcre-devel zlib-devel
cd /opt
tar zxvf nginx-1.12.0.tar.gz
cd nginx-1.12.0/
./configure --prefix=/usr/local/nginx
make && make install
配置 nginx
vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
include vhost/*.conf; //添加虚拟主机目录
default_type application/octet-stream;
//创建虚拟主机目录
mkdir /usr/local/nginx/conf/vhost
//创建日志文件目录
mkdir /var/log/nginx
//启动nginx
/usr/local/nginx/sbin/nginx
2.7启动template
consul:192.168.163.100
consul-template -consul-addr 192.168.163.100:8500 \
-template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/nginx01.conf:/usr/local/nginx/sbin/nginx -s reload" \
--log-level=info
重开一个consul终端
cat /usr/local/nginx/conf/vhost/nginx01.conf
upstream http_backend {
server 192.168.163.150:81;
server 192.168.163.150:82;
server {
listen 81;
server_name localhost 192.168.163.100;
access_log /var/log/nginx/ljj-access.log;
index index.html index.php;
location / {
proxy_set_header HOST
h
o
s
t
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
;
<
/
s
p
a
n
>
p
r
o
x
y
s
e
t
h
e
a
d
e
r
X
−
R
e
a
l
−
I
P
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
v
a
r
i
a
b
l
e
"
>
host</span><span class="token punctuation">;</span> proxy_set_header X-Real-IP <span class="token variable">
host</span><spanclass="tokenpunctuation">;</span>proxysetheaderX−Real−IP<spanclass="tokenvariable">remote_addr;
proxy_set_header Client-IP
r
e
m
o
t
e
a
d
d
r
<
/
s
p
a
n
>
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
p
u
n
c
t
u
a
t
i
o
n
"
>
;
<
/
s
p
a
n
>
p
r
o
x
y
s
e
t
h
e
a
d
e
r
X
−
F
o
r
w
a
r
d
e
d
−
F
o
r
<
s
p
a
n
c
l
a
s
s
=
"
t
o
k
e
n
v
a
r
i
a
b
l
e
"
>
remote_addr</span><span class="token punctuation">;</span> proxy_set_header X-Forwarded-For <span class="token variable">
remoteaddr</span><spanclass="tokenpunctuation">;</span>proxysetheaderX−Forwarded−For<spanclass="tokenvariable">proxy_add_x_forwarded_for;
proxy_pass http://http_backend;
}
}
2.8测试
- 增加一个nginx容器节点,测试服务发现及配置更新功能
registrator:192.168.163.150
在registrator服务端注册
docker run -itd -p:83:80 --name test-05 -h test05 nginx
consul:192.168.163.100
在consul服务器监控装填会有提示自动更新
查看三台nginx容器日志,请求正常轮询到各个容器节点上
registrator:192.168.163.150
docker logs -f test-01
docker logs -f test-02
docker logs -f test-05
- 1
- 2
- 3
2.9部署consul多节点
192.168.163.200
添加一台已有docker环境的服务器192.168.163.200/24加入已有的群集中
mkdir /root/consul
cd /root/consul
//放入安装包
unzip consul_0.9.2_linux_amd64.zip
mv consul /usr/bin
consul agent
-server
-bootstrap
-ui
-data-dir=/var/lib/consul-data
-bind=192.168.163.200
-client=0.0.0.0
-node=consul-server02
-enable-script-checks=true
-datacenter=dc1
-join 192.168.163.100 &> /var/log/consul.log &
#--------参数解释--------------------------------
-enable-script-checks=true:设置检查服务为可用
-datacenter:数据中心名称
-join:加入到已有的集群中
在consul:192.168.163.100或192.168.163.200上检查是否加入
真机上验证