使用 Docker 将Consul、Consul Template、Registrator和Nginx 组成一个可以进行扩展的服务框架

Consul 是什么?

  • Consul是一个分布式、高可用的系统,是一个为了解决在生产环境中服务注册,服务发现,服务配置的一个工具

Consul 特性

  • 支持健康检查,允许存储键值对
  • 基于GO语言开发,可移植性强
  • 支持ACL访问控制

Consul 实验

实验环境:
consul :192.168.177.105 Docker-ce、 Consul、 Consul-template
container :192.168.177.106 Docker-ce、registrator


操作节点 :consul 端
Docker-ce 安装步骤https://blog.csdn.net/qq_35479765/article/details/120029139?spm=1001.2014.3001.5501
创建consul目录并上传consul_0.9.2_linux_amd64.zip压缩包到consul目录下

[root@consul ~]# mkdir consul
[root@consul ~]# cd consul
[root@consul consul]# ls
consul_0.9.2_linux_amd64.zip  consul-template_0.19.3_linux_amd64.zip
[root@consul consul]# unzip consul_0.9.2_linux_amd64.zip 
Archive:  consul_0.9.2_linux_amd64.zip
  inflating: consul                  
[root@consul consul]# ls
consul  consul_0.9.2_linux_amd64.zip  consul-template_0.19.3_linux_amd64.zip
[root@consul consul]# mv consul /usr/bin/

部署consul服务端

[root@consul consul]# consul agent \
> -server \
> -bootstrap \
> -ui \
> -data-dir=/var/lib/consul-data \
> -bind=192.168.177.105 \
> -client=0.0.0.0 \
> -node=consul-server01 &> /var/log/consul.log &
[1] 85407
[root@consul consul]# consul members //查看consul集群信息
Node             Address               Status  Type    Build  Protocol  DC
consul-server01  192.168.177.105:8301  alive   server  0.9.2  2         dc1
[root@consul consul]# consul info | grep leader
	leader = true
	leader_addr = 192.168.177.105:8300

操作节点 container 端
安装 Registrator

[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.177.106 \
> consul://192.168.177.105:8500
Unable to find image 'gliderlabs/registrator:latest' locally
latest: Pulling from gliderlabs/registrator
Image docker.io/gliderlabs/registrator:latest uses outdated schema1 manifest format. Please upgrade to a schema2 image for better future compatibility. More information at https://docs.docker.com/registry/spec/deprecated-schema-v1/
c87f684ee1c2: Pull complete 
a0559c0b3676: Pull complete 
a28552c49839: Pull complete 
Digest: sha256:6e708681dd52e28f4f39d048ac75376c9a762c44b3d75b2824173f8364e52c10
Status: Downloaded newer image for gliderlabs/registrator:latest
b392b2111d836a2737e72dc3f23e594589ec99322b7d25c906feb528a73886cc
[root@localhost ~]# docker images
REPOSITORY               TAG       IMAGE ID       CREATED       SIZE
gliderlabs/registrator   latest    3b59190c6c80   5 years ago   23.8MB
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE                           COMMAND                  CREATED          STATUS          PORTS     NAMES
b392b2111d83   gliderlabs/registrator:latest   "/bin/registrator -i…"   12 seconds ago   Up 11 seconds             registrator
[root@localhost ~]# 

测试服务发现功能是否正常

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
[root@localhost ~]# docker run -itd -p:83:80 --name test-01 -h test01 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
a330b6cecb98: Pull complete 
e0ad2c0621bc: Pull complete 
9e56c3e0e6b7: Pull complete 
09f31c94adc6: Pull complete 
32b26e9cdb83: Pull complete 
20ab512bbb07: Pull complete 
Digest: sha256:853b221d3341add7aaadf5f81dd088ea943ab9c918766e295321294b035f3f3e
Status: Downloaded newer image for nginx:latest
97445b305d79c79506595464cce10634aa3e71030478ed39139de0aaeb44c722
[root@localhost ~]# docker run -itd -p:84:80 --name test-02 -h test02 nginx
604640448f5a8f25d2bdde61b0444e9304da3ba020baca1d3eecf55650b9529d
[root@localhost ~]# docker run -itd -p:88:80 --name test-03 -h test03 httpd
Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
a330b6cecb98: Already exists 
14e3dd65f04d: Pull complete 
fe59ad2e7efe: Pull complete 
2cb26220caa8: Pull complete 
3138742bd847: Pull complete 
Digest: sha256:af1199cd77b018781e2610923f15e8a58ce22941b42ce63a6ae8b6e282af79f5
Status: Downloaded newer image for httpd:latest
f2a0dd9e54edda67e01617a541ee40111c46eb6ef4052713c8792e865b9d7eae
[root@localhost ~]# docker run -itd -p:89:80 --name test-04 -h test04 httpd
2d7ff3212eceb8140edd024051dc154a8b527e5c0020dd21b1280ff9f6768650
[root@localhost ~]# 

宿主机上使用浏览器查看consul是否自动发现容器
在这里插入图片描述
也可以在consul服务端 使用httpd api 查看注册的服务信息

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

操作节点:consul 端
安装 consul-template
        Consul-Template 是一个守护进程,用于实时查询 Consul 集群信息,并更新文件系统上任意数量的指定模板,生成配置文件。更新完成以后,可以选择运行 shell 命令执行更新 操作,重新加载 Nginx。Consul-Template 可以查询 Consul 中的服务目录、Key、Key-values 等。这种强大的抽象功能和查询语言模板可以使 Consul-Template 特别适合动态的创建配置文件。
准备 template nginx 模板文件

vim /root/consul/nginx.ctmpl

upstream http_backend {
  {{range service "nginx"}}
   server {{.Address}}:{{.Port}};   #此处引用的变量会指向后端的地址和端口(动态变化)
   {{end}}
}

server {
  listen 83;
  server_name localhost 192.168.177.105;		#反向代理的IP地址(前端展示的NG服务的IP)
  access_log /var/log/nginx/test.cn-access.log;
  index index.html index.php;
  location / {
    proxy_set_header HOST $host;
    proxy_set_header X-Real-IP $remote_addr;		#后端真实IP
    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-1.15.9.tar.gz 到 /opt目录下)

[root@consul consul]# yum install gcc pcre-devel zlib-devel -y
[root@consul consul]# cd /opt
[root@consul opt]# ls
containerd  nginx-1.15.9.tar.gz  rh
[root@consul opt]# tar xzvf nginx-1.15.9.tar.gz
[root@consul opt]# cd nginx-1.15.9
[root@consul nginx-1.15.9]# ./configure --prefix=/usr/local/nginx
[root@consul nginx-1.15.9]# make && make install

修改nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    include       vhost/*.conf; //添加虚拟主机目录(consul动态生成的配置文件就会放在这里)
......省略部分内容

创建虚拟主机目录、创建日志文件目录

[root@consul nginx-1.15.9]# mkdir /usr/local/nginx/conf/vhost
[root@consul nginx-1.15.9]# mkdir /var/log/nginx

启动nginx

[root@consul nginx-1.15.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/
bin/     etc/     games/   include/ lib/     lib64/   libexec/ nginx/   sbin/    share/   src/     
[root@consul nginx-1.15.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@consul nginx-1.15.9]# nginx       
[root@consul nginx-1.15.9]# netstat -napt | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      91476/nginx: master 
[root@consul nginx-1.15.9]# 

配置并启动 template

[root@consul nginx-1.15.9]# cd /root/consul/
[root@consul consul]# ls
consul_0.9.2_linux_amd64.zip  consul-template_0.19.3_linux_amd64.zip  nginx.ctmpl
[root@consul consul]# unzip consul-template_0.19.3_linux_amd64.zip
Archive:  consul-template_0.19.3_linux_amd64.zip
  inflating: consul-template         
[root@consul consul]# ls
consul_0.9.2_linux_amd64.zip  consul-template  consul-template_0.19.3_linux_amd64.zip  nginx.ctmpl
[root@consul consul]# mv consul-template /usr/bin/
[root@consul consul]# 

关联 nginx 虚拟目录中的子配置文件操作

[root@consul consul]# consul-template -consul-addr 192.168.177.105:8500 \
> -template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/test.conf:/usr/local/nginx/sbin/nginx -s reload" \
> --log-level=info
2021/09/11 08:13:29.599327 [INFO] consul-template v0.19.3 (ebf2d3d)
2021/09/11 08:13:29.599352 [INFO] (runner) creating new runner (dry: false, once: false)
2021/09/11 08:13:29.599797 [INFO] (runner) creating watcher
2021/09/11 08:13:29.600025 [INFO] (runner) starting
2021/09/11 08:13:29.600155 [INFO] (runner) initiating run
2021/09/11 08:13:29.602542 [INFO] (runner) initiating run
2021/09/11 08:13:29.603638 [INFO] (runner) rendered "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/test.conf"
2021/09/11 08:13:29.603680 [INFO] (runner) executing command "/usr/local/nginx/sbin/nginx -s reload" from "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/test.conf"
2021/09/11 08:13:29.603756 [INFO] (child) spawning: /usr/local/nginx/sbin/nginx -s reload
 //此时是一直运行的状态,需要另开一个终端查看生成的配置文件

另外打开一个终端查看生成配置文件

[root@consul ~]# cat /usr/local/nginx/conf/vhost/test.conf 
upstream http_backend {
  
   server 192.168.177.106:83;  
   
   server 192.168.177.106:84;  
   
}

server {
  listen 83;
  server_name localhost 192.168.177.105;
  access_log /var/log/nginx/test.cn-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;
  }
}
[root@consul ~]# 

操作节点 :container 端
增加一个 nginx 容器节点,测试服务发现及配置更新功能

[root@localhost ~]# docker run -itd -p:85:80 --name test-05 -h test05 nginx
b801f738eb6d684a76fd2bdd39d197368d080d85316bf0ba48ecc3bd7a617727
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE                           COMMAND                  CREATED              STATUS              PORTS                               NAMES
b801f738eb6d   nginx                           "/docker-entrypoint.…"   About a minute ago   Up About a minute   0.0.0.0:85->80/tcp, :::85->80/tcp   test-05
2d7ff3212ece   httpd                           "httpd-foreground"       57 minutes ago       Up 57 minutes       0.0.0.0:89->80/tcp, :::89->80/tcp   test-04
f2a0dd9e54ed   httpd                           "httpd-foreground"       57 minutes ago       Up 57 minutes       0.0.0.0:88->80/tcp, :::88->80/tcp   test-03
604640448f5a   nginx                           "/docker-entrypoint.…"   57 minutes ago       Up 57 minutes       0.0.0.0:84->80/tcp, :::84->80/tcp   test-02
97445b305d79   nginx                           "/docker-entrypoint.…"   57 minutes ago       Up 57 minutes       0.0.0.0:83->80/tcp, :::83->80/tcp   test-01
b392b2111d83   gliderlabs/registrator:latest   "/bin/registrator -i…"   About an hour ago    Up About an hour                                        registrator
[root@localhost ~]# 

操作节点 : consul 端
在consul服务器监控装填会有提示自动更新

2021/09/11 08:23:09.904364 [INFO] (runner) initiating run
2021/09/11 08:23:09.906582 [INFO] (runner) rendered "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/test.conf"
2021/09/11 08:23:09.906641 [INFO] (runner) executing command "/usr/local/nginx/sbin/nginx -s reload" from "/root/consul/nginx.ctmpl" => "/usr/local/nginx/conf/vhost/test.conf"
2021/09/11 08:23:09.906700 [INFO] (child) spawning: /usr/local/nginx/sbin/nginx -s reload

在刚才另开的终端上查看动态生成的配置文件

[root@consul vhost]# cat test.conf 
upstream http_backend {
  
   server 192.168.177.106:83;  
   
   server 192.168.177.106:84;  
   
   server 192.168.177.106:85;  
   
}

server {
  listen 83;
  server_name localhost 192.168.177.105;
  access_log /var/log/nginx/test.cn-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;
  }
}

测试是否可以轮询各个容器节点上
另开两台container的终端
分别执行:
docker logs -f test-01 //第一个终端
docker logs -f test-02 //第二个终端
docker logs -f test-05 //第三个终端
宿主机浏览器输入: http://192.168.105:83 多刷新几次,产看三个终端的日志变化
在这里插入图片描述
上图可以看出成功轮询到各个容器节点上

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值