Docker consul服务注册与发现

目录

一、服务注册与发现

1、什么是服务注册与发现

2、什么是consul 

3、consul提供的一些关键特性

 4、容器更新与发现

二、基于nginx与consul构建自动发现即高可用的Docker服务架构

consul服务器部署

1、建立consul

2、查看集群信息

3、通过http获取集群信息

registrator服务

三、consul-template

1、准备 template nginx 模板文件

 2、编译安装nginx

3、配置nginx

 4、配置并启动template

打开另一个终端

5、访问template-nginx (registrator服务器)

浏览器访问: 192.168.247.160:8000

6、增加nginx容器节点(registrator服务器)

(1)增加一个 nginx 容器节点,测试服务发现及配置更新功能。

(2)查看/usr/local/nginx/conf/vhost/kgc.conf 文件内容(consul服务器)

(3)查看三台 nginx 容器日志,请求正常轮询到各个容器节点上

consul多节点


一、服务注册与发现

1、什么是服务注册与发现

服务注册与发现是微服务架构中不可或缺的重要组件。起初服务都是单节点的,不保障高可用性,也不考虑服务的压力承载,服务之间调用单纯的通过接口访问。直到后来出现了多个节点的分布式架构,起初的解决手段是在服务前端负载均衡,这样前端必须要知道所有后端服务的网络位置,并配置在配置文件中。这里就会有几个问题:

  • 如果需要调用后端服务A-N,就需要配置N个服务的网络位置,配置很麻烦
  • 后端服务的网络位置变化,都需要改变每个调用者的配置

        既然有这些问题,那么服务注册与发现就是解决这些问题的。后端服务A-N可以把当前自己的网络位置注册到服务发现模块,服务发现就以K-V的方式记录下来,K一般是服务名,V就是IP:PORT。服务发现模块定时的进行健康检查,轮询查看这些后端服务能不能访问的了。前端在调用后端服务A-N的时候,就跑去服务发现模块问下它们的网络位置,然后再调用它们的服务。这样的方式就可以解决上面的问题了,前端完全不需要记录这些后端服务的网络位置,前端和后端完全解耦!

2、什么是consul 

consul是google开源的一个使用go语言开发的服务管理软件。支持多数据中心、分布式高可用的、服务发现和配置共享。采用Raft算法,用来保证服务的高可用。内置了服务注册与发现框架、分布一致性协议实现、健康检查、Key/Value存储、多数据中心方案,不再需要依赖其他工具(比如ZooKeeper等)。服务部署简单,只有一个可运行的二进制的包。每个节点都需要运行agent,他有两种运行模式server 和 client。 每个数据中心官方建议需要3或5个server节点以保证数据安全,同时保证server-leader的选举能够正确的进行。

在client模式下,所有注册到当前节点的服务会被转发到server节点,本身是不持久化这些信息。

在server模式下,功能和client模式相似,唯一不同的是,它会把所有的信息持久化到本地,这样遇到故障,信息是可以被保留的。

server-leader是所有server节点的老大,它和其它server节点不同的是,它需要负责同步注册的信息给其它的server节点,同时也要负责各个节点的健康监测。

3、consul提供的一些关键特性

服务注册与发现:consul通过DNS或者HTTP接口使服务注册和服务发现变的很容易,一些外部服务,例如saas提供的也可以一样注册。

健康检查:健康检测使consul可以快速的告警在集群中的操作。和服务发现的集成,可以防止服务转发到故障的服务上面。

Key/Value存储:一个用来存储动态配置的系统。提供简单的HTTP接口,可以在任何地方操作。
多数据中心:无需复杂的配置,即可支持任意数量的区域。

       安装consul是用于服务注册,也就是容器本身的一些信息注册到consul里面,其他程序可以通过consul获取注册的相关服务信息,这就是服务注册与发现。

 4、容器更新与发现

容器服务更新与发现:先发现再更新,发现的是后端节点上容器的变化(registrator),更新的是nginx配置文件(agent)

registrator:是consul安插在docker容器里的眼线,用于监听监控节点上容器的变化(增加或减少,或者宕机),一旦有变化会把这些信息告诉并注册在consul server端(使用回调和协程的方式,所以它的延迟和资源消耗会很少),consul server发生一旦发生注册列表的变化后,会把注册的信息告诉agent

agent(代理):用来控制consul template模板,用template组件去和nginx.conf来进行对接,模板里全是变量,用变量的方式去加载后端由注册到consul server端之后,server端会把信息告诉agent,agent和template进行对接,写入template,template就有了镜像,更新完之后会作为nginx.conf子配置文件被前端的nginx识别,consul agent会控制reload之后会识别nginx.conf配置文件中的变化,相当于识别后端的节点,就可以在地址池中动态调整自己后端资源。

二、基于nginx与consul构建自动发现即高可用的Docker服务架构

实验环境

服务器IP地址部署服务
consul服务器192.168.247.160运行consul服务、nginx服务、consul-template守护进程
registator服务器192.168.247.150运行registrator容器、运行nginx容器


consul服务器部署

1、建立consul

[root@consul ~]# systemctl stop firewalld
[root@consul ~]# setenforce 0
#建立 Consul 服务
[root@consul ~]# mkdir /opt/consurl
[root@consul ~]# rz -E
rz waiting to receive.
[root@consul ~]# cp consul_0.9.2_linux_amd64.zip /opt/consurl/
[root@consul ~]# cd /opt/consurl/
[root@consul consurl]# unzip consul_0.9.2_linux_amd64.zip 
Archive:  consul_0.9.2_linux_amd64.zip
  inflating: consul                  
[root@consul consurl]# mv consul /usr/local/bin/
#设置代理,在后台启动 consul 服务端
[root@consul consurl]# consul agent \
> -server \
> -bootstrap \
> -ui \
> -data-dir=/var/lib/consul-data \
> -bind=192.168.247.160 \
> -client=0.0.0.0 \
> -node=consul-server01 &> /var/log/consul.log &
[1] 73002

---------
-server: 以server身份启动。默认是client。
-bootstrap :用来控制一个server是否在bootstrap模式,在一个数据中心中只能有一个server处于bootstrap模式,当一个server处于 bootstrap模式时,可以自己选举为 server-leader。
-bootstrap-expect=2 :集群要求的最少server数量,当低于这个数量,集群即失效。
-ui :指定开启 UI 界面,这样可以通过 http://localhost:8500/ui 这样的地址访问 consul 自带的 web UI 界面。
-data-dir :指定数据存储目录。
-bind :指定用来在集群内部的通讯地址,集群内的所有节点到此地址都必须是可达的,默认是0.0.0.0。
-client :指定 consul 绑定在哪个 client 地址上,这个地址提供 HTTP、DNS、RPC 等服务,默认是 127.0.0.1。
-node :节点在集群中的名称,在一个集群中必须是唯一的,默认是该节点的主机名。
-datacenter :指定数据中心名称,默认是dc1。



启动consul后默认会监听5个端口:
8300:replication、leader farwarding的端口
8301:lan cossip的端口
8302:wan gossip的端口
8500:web ui界面的端口
8600:使用dns协议查看节点信息的端口

2、查看集群信息

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

 

3、通过http获取集群信息

curl 127.0.0.1:8500/v1/status/peers 			#查看集群server成员
curl 127.0.0.1:8500/v1/status/leader			#集群 server-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			#集群节点详细信息

registrator服务

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

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

-----------
--net=host :把运行的docker容器设定为host网络模式。
-v /var/run/docker.sock:/tmp/docker.sock :把宿主机的Docker守护进程(Docker daemon)默认监听的Unix域套接字挂载到容器中。
--restart=always :设置在容器退出时总是重启容器。
--ip :刚才把network指定了host模式,所以我们指定ip为宿主机的ip。
consul :指定consul服务器的IP和端口。


2、测试服务发现功能是否正常
[root@registrator ~]# docker run -itd -p:83:80 --name test-01 -h test01 nginx
[root@registrator ~]# docker run -itd -p:84:80 --name test-02 -h test02 nginx
[root@registrator ~]# docker run -itd -p:88:80 --name test-03 -h test03 httpd
[root@registrator ~]# docker run -itd -p:89:80 --name test-04 -h test04 httpd	


3、浏览器访问192.168.247.160:8500  NODES中consul-server01 会出现5个服务

三、consul-template

       Consul-Template是基于Consul的自动替换配置文件的应用。Consul-Template是一个守护进程,用于实时查询Consul集群信息,并更新文件系统上任意数量的指定模板,生成配置文件。更新完成以后,可以选择运行 shell 命令执行更新操作,重新加载 Nginx。

        Consul-Template可以查询Consul中的服务目录、Key、Key-values 等。这种强大的抽象功能和查询语言模板可以使 Consul-Template 特别适合动态的创建配置文件。例如:创建Apache/Nginx Proxy Balancers 、 Haproxy Backends等。

1、准备 template nginx 模板文件

consul服务器

[root@consul ~]# vim /opt/consul/nginx.ctmpl


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

server {
  listen 100;
  server_name localhost 192.168.247.160;
  access_log /var/log/nginx/kgc.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;
   }
}

 2、编译安装nginx

[root@consul ~]# yum install -y gcc gcc-c++ pcre-devel zlib-devel make
[root@consul ~]# useradd -M -s /sbin/nologin nginx
[root@consul ~]# rz -E
rz waiting to receive.
[root@consul ~]# tar xf nginx-1.12.0.tar.gz -C /opt
[root@consul ~]# cd /opt/nginx-1.12.0/
[root@consul nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make -j2 && make install
[root@consul nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

3、配置nginx

[root@consul nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf
......
http {
     include       mime.types;
     include  vhost/*.conf;       				#添加虚拟主机目录
     default_type  application/octet-stream;

#创建虚拟主机目录
[root@consul nginx-1.12.0]# mkdir /usr/local/nginx/conf/vhost
#创建日志文件目录
[root@consul nginx-1.12.0]# mkdir /var/log/nginx
#启动nginx
[root@consul nginx-1.12.0]# /usr/local/nginx/sbin/nginx 
[root@consul nginx-1.12.0]# netstat -natp |grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      62431/nginx: master 

 4、配置并启动template

[root@consul nginx-1.12.0]# cd /opt
[root@consul opt]# rz -E
rz waiting to receive.
[root@consul opt]# unzip consul-template_0.19.3_linux_amd64.zip 
Archive:  consul-template_0.19.3_linux_amd64.zip
  inflating: consul-template         
[root@consul opt]# mv consul-template /usr/local/bin/
#在前台启动 template 服务,启动后不要按 ctrl+c 中止 consul-template 进程。
[root@consul opt]# consul-template --consul-addr 192.168.247.160:8500 \
--template "/opt/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/kgc.conf:/usr/local/nginx/sbin/nginx -s reload" \
--log-level=info

打开另一个终端

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

server {
  listen 100;
  server_name localhost 192.168.247.160;
  access_log /var/log/nginx/kgc.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;
   }
}

5、访问template-nginx (registrator服务器)

[root@registrator ~]# docker ps -a
CONTAINER ID   IMAGE                           COMMAND                   CREATED       STATUS          PORTS                               NAMES
8addafc54244   httpd                           "httpd-foreground"        6 hours ago   Up 6 hours      0.0.0.0:89->80/tcp, :::89->80/tcp   test-04
4149ad396422   httpd                           "httpd-foreground"        6 hours ago   Up 6 hours      0.0.0.0:88->80/tcp, :::88->80/tcp   test-03
8dab3068fc4f   nginx                           "/docker-entrypoint.…"   6 hours ago   Up 14 seconds   0.0.0.0:84->80/tcp, :::84->80/tcp   test-02
38bf898c0bfa   nginx                           "/docker-entrypoint.…"   6 hours ago   Up 4 seconds    0.0.0.0:83->80/tcp, :::83->80/tcp   test-01
894626fbdd25   gliderlabs/registrator:latest   "/bin/registrator --…"   6 hours ago   Up 6 hours                                          registrator
[root@registrator ~]# docker exec -it 8dab3068fc4f bash
root@test02:/# echo "this is 1 web" > /usr/share/nginx/html/index.html
root@test02:/# exit
exit
[root@registrator ~]# docker exec -it 38bf898c0bfa bash
root@test01:/# echo "this is 2 web" > /usr/share/nginx/html/index.html
root@test01:/# exit
exit

浏览器访问: 192.168.247.160:8000

6、增加nginx容器节点(registrator服务器)

(1)增加一个 nginx 容器节点,测试服务发现及配置更新功能。

[root@registrator ~]# docker run -itd -p:85:80 --name test-05 -h test05 nginx

 观察 template 服务,会从模板更新/usr/local/nginx/conf/vhost/kgc.conf 文件内容,并且重载 nginx 服务。

(2)查看/usr/local/nginx/conf/vhost/kgc.conf 文件内容(consul服务器)

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

server {
  listen 100;
  server_name localhost 192.168.247.160;
  access_log /var/log/nginx/kgc.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;
   }
}

 

(3)查看三台 nginx 容器日志,请求正常轮询到各个容器节点上

[root@registrator ~]# docker logs -f test-01
[root@registrator ~]# docker logs -f test-02
[root@registrator ~]# docker logs -f test-05

consul多节点

#添加一台已有docker环境的服务器加入已有的群集中:
 
consul agent \
-server \
-bootstrap \
-ui \
-data-dir=/var/ib/consul-data \
-bind=192.168.247.130 \
-client=0.0.0.0 \
-node=consul-server02 \
-enable-script-checks=true \
-datacenter=dc1 \
-join 192.168.247.160 &> /var/log/consul.log &
 
 
---------------注释-------------
-enable-script-checks=true: 设置检查服务为可用
-datacenter: 数据中心名称
-join: 加入到已有的集群中
 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值