原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处 、作者信息和本声明。否则将追究法律责任。
http://dgd2010.blog.51cto.com/1539422/1730439
方案概述
配置概述
操作步骤
-
构建consul Cluster(Consul 集群)
-
构建Web应用服务
-
每一个服务应用安装和配置Consul Agent
-
安装并配置consul-template使consul-template与nginx联动
-
验证与测试
构建consul Cluster(Consul 集群)
-
已有方案中有使用progrium/consul的案例,可以供参考;
-
此镜像的星数和pull次数是最多的,pull次数高达9百万次;
-
在它的Repo info中详细的介绍了该镜像如何使用以及如何部署consul集群;
-
此外,该image大小合适,仅有五六十MB,虽然基于busybox,但同样适用于有Linux经验的技术人员。
Servers | Quorum Size | Failure Tolerance |
---|---|---|
1 | 1 | 0 |
2 | 2 | 0 |
3 | 2 | 1 |
4 | 3 | 1 |
5 | 3 | 2 |
6 | 4 | 2 |
7 | 4 | 3 |
1
2
3
4
5
6
7
8
9
|
# Refer:
https:
//hub
.docker.com
/r/progrium/consul/
docker pull progrium
/consul
docker run -d --name node1 -h node1 progrium
/consul
-server -bootstrap-expect 3
JOIN_IP=
"$(docker inspect -f '{{.NetworkSettings.IPAddress}}' node1)"
docker run -d --name node2 -h node2 progrium
/consul
-server -
join
$JOIN_IP
docker run -d --name node3 -h node3 progrium
/consul
-server -
join
$JOIN_IP
docker run -d -p 8400:8400 -p 8500:8500 -p 8600:53
/udp
--name node4 -h node4 progrium
/consul
-
join
$JOIN_IP
|
1
|
/bin/consul
agent -config-
dir
=
/config
-server -bootstrap-expect 3
|
1
|
/bin/consul
agent -config-
dir
=
/config
-server -
join
192.168.0.7
|
1
|
/bin/consul
agent -config-
dir
=
/config
-server -
join
192.168.0.7
|
1
|
/bin/consul
agent -config-
dir
=
/config
-
join
192.168.0.7
|
1
2
3
4
5
6
|
# for consul web ui
wget -c
https:
//releases
.hashicorp.com
/consul/0
.6.0
/consul_0
.6.0_web_ui.zip
mkdir
consul_web_ui
unzip -d consul_web_ui consul_0.6.0_web_ui.zip
|
构建Web应用服务
1
2
3
4
5
|
#docker commit jdk8-firstvert jdk8-firstvert_with_consul
#docker images
docker run -idt --name firstvertnode1 -h firstvertnode1 jdk8-firstvert_with_consul
/bin/bash
docker run -idt --name firstvertnode2 -h firstvertnode2 jdk8-firstvert_with_consul
/bin/bash
docker run -idt --name firstvertnode3 -h firstvertnode2 jdk8-firstvert_with_consul
/bin/bash
|
每一个服务应用安装和配置Consul Agent
1
2
3
4
5
6
7
8
9
10
|
# for test
[ -x
/bin/consul
] || ( wget -c
https:
//releases
.hashicorp.com
/consul/0
.6.0
/consul_0
.6.0_linux_amd64.zip
&& unzip -d
/bin
consul_0.6.0_linux_amd64.zip )
[ -d
/config
] ||
mkdir
/config
[ -d
/data
] ||
mkdir
/data
# JOIN_IP is 192.168.0.7
nohup
/bin/consul
agent -config-
dir
=
/config
-
join
192.168.0.7 -data-
dir
/data
2>&1 >
/tmp/consul
.log &
nohup
java -Dfile.encoding=utf-8 -jar FirstVert.x3-1.0-SNAPSHOT-fat.jar 2>&1 >
/dev/null
&
# echo '{"service": {"name": "web", "tags": ["FirstVert"], "port": 8081}}' > /config/FirstVert.json
|
1
2
3
|
echo
'{
"check"
: {
"name"
:
"ping"
,
"script"
: "
ping
-c1
www.baidu.com
2>&1 >
/dev/null
", "
interval
": "
30s"}}' >
/config/ping
.json
|
1
|
echo
'{"service": {"name": "web", "tags": ["FirstVert"], "port": 8081, "check": {"script": "curl localhost:8081 >/dev/null 2>&1", "interval": "10s"}}}'
>
/config/FirstVert
.json
|
1
2
|
killall consul
nohup
/bin/consul
agent -config-
dir
=
/config
-
join
192.168.0.7 -data-
dir
/data
2>&1 >
/tmp/consul
.log &
|
1
|
while
: ;
do
curl http:
//ipaddress
:port -w %{http_code} -s -o
/dev/null
;
sleep
1;
done
|
安装并配置consul-template使consul-template与nginx联动
1
2
3
4
5
6
|
wget -c
https:
//releases
.hashicorp.com
/consul-template/0
.12.0
/consul-template_0
.12.0_linux_amd64.zip
unzip -d . consul-template_0.12.0_linux_amd64.zip
cp
consul-template
'/bin'
rm
-rf consul*
|
1
|
vim
/root/nginx_web
.ctmpl
|
1
|
consul-template -consul 127.0.0.1:8500 -template
/root/nginx_web
.ctmpl:
/usr/local/nginx/conf/vhost/http_temp_port_80
.conf:
"/usr/local/nginx/sbin/nginx -s reload"
-dry
|
1
|
nohup
consul-template -consul 127.0.0.1:8500 -template nginx_web.ctmpl:
/usr/local/nginx/conf/vhost/http_temp_port_80
.conf:
"/usr/local/nginx/sbin/nginx -s reload"
2>&1 >
/tmp/consul-template
.log &
|