docker--consul部署服务发现、自动更新

部署环境

两台centos7,IP地址和所需环境如下

192.168.218.142 docker-ce docker-compose3 consul consul-template

192.168.218.148 docker-ce registor

下面开始部署consul

[root@localhost ~]# mkdir /root/consul
[root@localhost ~]# cd consul/
[root@localhost consul]# ls
consul_0.9.2_linux_amd64.zip  consul-template_0.19.3_linux_amd64.zip
[root@localhost consul]# unzip consul_0.9.2_linux_amd64.zip
[root@localhost consul]# mv consul /usr/bin/
[root@localhost consul]# consul agent \
> -server \
> -bootstrap \
> -ui \
> -data-dir=/var/lib/consul-data \
> -bind=192.168.218.142 \
> -client=0.0.0.0 \
> -node=consul-server01 &> /var/log/consul.log &

查看集群信息

[root@localhost consul]# consul members
Node             Address               Status  Type    Build  Protocol  DC
consul-server01  192.168.218.142:8301  alive   server  0.9.2  2         dc1
[root@localhost consul]# consul info |grep leader
	leader = true
	leader_addr = 192.168.218.142:8300

通过httpdapi获取集群信息,注意consul默认端口为8500

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    # 集群节点详细信息

容器服务自动加入consul集群

在需要加入的主机192.168.218.148上操作

安装registor

[root@localhost ~]# docker run -d \
> --name=registrator \
> --net=host \
> -v /var/run/docker.sock:/tmp/docker.sock \
> --restart=always \
> gliderlabs/registrator:latest \
> -ip=192.168.218.148 \
> consul://192.168.218.142:8500

创建容器,测试发现服务是否正常

docker run -itd -p:83:80 --name test-01 -h test01 nginx
docker run -itd -p:84:80 --name test-02 -h test02 nginx
docker run -itd -p:88:80 --name test-03 -h test03 httpd
docker run -itd -p:89:80 --name test-04 -h test04 httpd

回到consul服务器上

[root@localhost consul]# curl 127.0.0.1:8500/v1/catalog/services
{"consul":[],"httpd":[],"nginx":[]}

通过浏览器访问http://192.168.218.142:8500,查看服务

如图
在这里插入图片描述

到此,consul已部署完成,可以服务发现,下面部署consul-template,完成自动更新

在consul服务器上安装 consul-template

准备 template nginx模板文件

vim /root/consul/nginx.ctmpl

upstream http_backend {
  {{range service "nginx"}}
  server {{.Address}}:{{.Port}};
  {{end}}
}
server {
  listen 1216;
  server_name localhost 192.168.218.142;
  access_log /var/log/nginx/nginx-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;
 }
}

模板文件是一个nginx的配置文件,用于对刚刚配置的nginx服务做负载(也可以对httpd服务做负载),下面编译安装nginx,可参照我的博客
编译安装LNMP

配置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

配置并启动template

unzip consul-template_0.19.3_linux_amd64.zip
mv consul-template /usr/bin/

启用模板

[root@localhost consul]# consul-template -consul-addr=192.168.218.142:8500 -template="/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/consul.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info

启动consul-template后会输出信息:

2020/04/22 06:32:59.632332 [INFO] (runner) creating watcher
2020/04/22 06:32:59.632545 [INFO] (runner) starting
2020/04/22 06:32:59.632551 [INFO] (runner) initiating run
2020/04/22 06:32:59.633849 [INFO] (runner) initiating run

新建一个终端,查看ctmpl模板文件生成的nginx配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/vhost/consul.conf

upstream http_backend {

  server 192.168.218.148:83;

  server 192.168.218.148:84;

}
server {
  listen 1216;
  server_name localhost 192.168.218.142;
  access_log /var/log/nginx/nginx-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;
 }
}

下面尝试自动发现

在192.168.218.148上新加一个nginx容器

[root@localhost consul]# docker run -d -p 85:80 --name test-05 -h test
05 nginx

这时consul进程会有提示信息,如下:

2020/04/22 06:54:01.331571 [INFO] (runner) initiating run
2020/04/22 06:54:01.332554 [INFO] (runner) rendered "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/consul.conf"
2020/04/22 06:54:01.332577 [INFO] (runner) executing command "/usr/local/nginx/sbin/nginx -s reload" from "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/consul.conf"
2020/04/22 06:54:01.332743 [INFO] (child) spawning: /usr/local/nginx/sbin/nginx -s reload

再次查看consul服务器上刚刚生成的nginx配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/vhost/consul.conf

upstream http_backend {

  server 192.168.218.148:83;

  server 192.168.218.148:84;

  server 192.168.218.148:85;

}
server {
  listen 1216;
  server_name localhost 192.168.218.142;
  access_log /var/log/nginx/nginx-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;
 }
}

已经增加了新的nginx容器

使用浏览器访问http://192.168.218.142:8500,也可以查看到新增加的容器

在这里插入图片描述

使用curl命令访问nginx,注意监听的端口

curl 192.168.218.142:1216

多用几次curl访问nginx,产生一些访问日志,然后再到192.168.218.148上,查看nginx容器的日志

[root@localhost consul]# docker logs -f test-01
192.168.218.142 - - [22/Apr/2020:06:51:27 +0000] "GET / HTTP/1.0" 200612 "-" "curl/7.29.0" "192.168.218.142"
^C
[root@localhost consul]# docker logs -f test-02
192.168.218.142 - - [22/Apr/2020:06:51:38 +0000] "GET / HTTP/1.0" 200612 "-" "curl/7.29.0" "192.168.218.142"
^C
[root@localhost consul]# docker logs -f test-05
192.168.218.142 - - [22/Apr/2020:06:51:52 +0000] "GET / HTTP/1.0" 200612 "-" "curl/7.29.0" "192.168.218.142"
^C

到此,consul-template自动发现已部署完成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值