docker-consul服务管理软件(七)

目录

前言

一、consul概述

1、consul定义

2、consul特性

3、Consul容器服务更新与发现

4、consul及各组件的作用

二、Consul部署

1、部署环境及需求

2、部署,及查看集群信息

3、配置容器服务自动加入consul集群

4、consul:192.168.110.134节点部署nginx服务

5、安装consul-template,并准备template nginx模板文件

6、测试consul服务发现及配置更新功能


前言

        服务注册与发现是微服务架构中不可或缺的重要组件。起初服务都是单节点的,不保障高可

用性,也不考虑服务的压力承载,服务之间调用单纯的通过接口访问。直到后来出现了多个节点的

分布式架构,起初的解决手段是在服务前端负裁均衡,这样前端必须要知道所有后端服务的网络位

置,并配置在配置文件中。这里就会有几个问题:

◆如果需要调用后端服务A-N,就需要配置N个服务的网络位置,配置很麻烦

◆后端服务的网络位置变化,都需要改变每个调用者的配置

        既然有这些问题,那么服务注册与发现就是解决这些问题的。

        后端服务A-N可以把当前自己的网络位置注册到服务发现模块,服务发现就以K-V的方式记录

下来,K一般是服务名,V就是IP :PORT。服务发现模块定时的进行健康检查,轮询查看这些后

端服务能不能访向的了。前端在调用后端服务A-N的时候,就跑去服务发现模块问下它们的网络位

置,然后再调用它们的服务。这样的方式就可以解决上面的问题了,前端完全不需要记录这些后端

服务的网络位置,前端和后端完全解耦。

一、consul概述

1、consul定义

        Consul是HashiCorp公司推出的开源工具, 用于实现分布式系统的服务发现与配置。Consul

使用go语言开发,持多数据中心、分布式高可用的、服务发现和配置共享。采用Raft算法,用来保

证服务的高可用。内置了服务注册与发现框架、分布一致性协议实现、健康检查、Key/Value存

储、多数据中心方案,不再需要依赖其他工具(比如ZooKeeper等)。服务部署简单,只有一个可

运行的二进制的包。每个节点都需要运行agent,他有两种运行模式server和client。每个数据中心

官方建议需要3或5个server节点以保证数据安全,同时保证server-leader的选举能够正确的进行。

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

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

到故障,信息是可以被保留的。

server-leader是所有server节点的老大,它和其它server节点不同的是,它需要负责同步注册的信

息给其它的serrer节点,同时也要负责各个节点的健康监测。

2、consul特性

①服务注册与发现:安装consul是用于服务注册,也就是容器本身的一些信息注册到consul里面,

其他程序可以通过consul获取注册的相关服务信息,这就是服务注册与发现。consul通过DNS或者

HTTP接口使服务注册和服务发现变的很容易,一些外部服务,例如saas提供的也可以一样注册。

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

务转发到故障的服务上面。

③Key/Value存储:一个用来存储动态配置的系统。提供简单的HTTP接口,可以在任何地方操作。

④可移植性强:基于Golong语言。

⑤多数据中心:无需复杂的配置,即可支持任意数量的区域。

⑥支持ACL访问控制

3、Consul容器服务更新与发现

 

流程说明:设置registrator监控,监听服务端口,有新的端口监控会注册到agent,交给server端,

把后端真实的容器和ip写入template模板,生成可识别nginx的子配置文件,模板更新完成后,可以

选择运行shell命令执行服务跟新操作,重启nginx,再重载配置文件,从而对外识别新加的服务后

端的节点:docker服务器。

4、consul及各组件的作用

consul template作用:通过变量定义模板;定义的内容就是upstream地址池,通过变量的形式,

并在接收到指令的时候动态更新web中ng的配置文件;Consul-Template是一个守护进程,用于实

时查询Consul集群信息;

Consul-Template可以查询Consul中的服务目录、Key、Key-values等;这种强大的抽象功能和查

询语言模板可以使Consul-Template特别适合动态的创建配置文件。

consul agent作用:获取服务发现的机制;UI界面也可以看到新更新的服务。

registrator作用:一个由Go语言编写的,针对docker使用的,可以用于检测容器状态,自动注册和

注销docker容器的服务到服务配置中心。目前支持Consul、Etcd和SkyDNS2。

consul server作用:服务注册与发现(主要功能),提供HTTP和DNS两种发现方式;健康检查,

支持多种协议,HTTP、TCP等;Key/Value存储;支持多数据中心;基于Golong语言,可移植性

强;支持ACL访问控制;与Docker等轻量级容器可无缝配合。

二、Consul部署

1、部署环境及需求

主机名称

操作系统

IP地址

主要软件及版本

consul

Centos 7.6

192.168.110.134

Docker 、Consul、Consul-template

registrator

Centos 7.6

192.168.110.133

Docker、registrator

需求:实现单机网络下容器与容器之间互通;使用Docker Compose创建容器;            搭建Consul服务实现自动发现和更新

[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# systemctl disable firewalld.service 
[root@localhost ~]# setenforce 0
[root@localhost ~]# vim /etc/resolv.conf
nameserver 114.114.114.114
[root@localhost ~]# ping www.baidu.com
[root@localhost ~]# ntpdate ntp1.aliyun.com   #时间同步
25 Sep 08:46:52 ntpdate[62240]: adjust time server 120.25.115.20 offset 0.007301 sec
[root@localhost ~]# hostnamectl set-hostname consul
[root@localhost ~]# su

2、部署,及查看集群信息

[root@consul ~]# mkdir  /root/consul   #创建consul目录
[root@consul ~]# cd /root/consul/
[root@consul consul]# ls
[root@consul consul]# rz -E   #上传consul安装包
rz waiting to receive.
[root@consul consul]# ls
consul_0.9.2_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]# mv consul /usr/bin/  #将解压出来的consul移动到/usr/bin下,让系统识别
[root@consul consul]# consul agent \     #启动consul集群
> -server \                 #模式为server
> -bootstrap \                    #设置引导模式
> -ui \                      #启动ui界面
> -data-dir=/var/lib/consul-data \             #只当数据目录
> -bind=192.168.110.134 \    #绑定集群通讯IP
> -client=0.0.0.0 \  #监听的客户端网段,0.0.0.0代表所有  
> -node=consul-server01 &> /var/log/consul.log & 
# #指定节点名称,将执行结果输出到/var/log/consul.log文件中,&在后台运行
[1] 43374

#########
consul agent \
-server \
-bootstrap \
-ui \
-data-dir=/var/lib/consul-data \
-bind=192.168.110.134 \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &
##########
[root@consul consul]# consul members  #查看集群信息
Node             Address               Status  Type    Build  Protocol  DC
consul-server01  192.168.110.134:8301  alive   server  0.9.2  2         dc1
[root@consul consul]# consul info | grep leader  #这里查询到的8300端口用于集群内数据的读写和复制
	leader = true
	leader_addr = 192.168.110.134:8300
[root@consul consul]#
[root@consul consul]# curl 127.0.0.1:8500/v1/status/peers
["192.168.110.134:8300"][root@consul consul]# 
[root@consul consul]# curl 127.0.0.1:8500/v1/status/leader
"192.168.110.134:8300"[root@consul consul]# 
[root@consul consul]# 
[root@consul consul]# curl 127.0.0.1:8500/v1/catalog/services
{"consul":[]}[root@consul consul]# 
[root@consul consul]# curl 127.0.0.1:8500/v1/catalog/nginx
[root@consul consul]# curl 127.0.0.1:8500/v1/catalog/nodes
[{"ID":"315ab3ba-9599-2492-81d4-9ad818089f4f","Node":"consul-server01","Address":"192.168.110.134","Datacenter":"dc1","TaggedAddresses":{"lan":"192.168.110.134","wan":"192.168.110.134"},"Meta":{},"CreateIndex":5,"ModifyIndex":6}][root@consul consul]#

注:

查询集群信息

#查看集群server成员

curl 127.0.0.1:8500/v1/status/peers

#集群Raf leader

curl 127.0.0.1:8500/v1/status/leader

#注册的所有服务

curl 127.0.0.1:8500/v1/catalog/services

#查看nginx服务信息

curl 127.0.0.1:8500/v1/catalog/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协议提供服务发现功能

3、配置容器服务自动加入consul集群

安装 Gliderlabs/Registrator,它可检查容器运行状态并自动注册,还可注销 docker 容器的服务到

服务配置中心。目前支持 Consul、Etcd 和 SkyDNS2。

在 192.168.110.133节点,执行以下操作:

#运行并创建
docker run -d \
--name=registrator \
--net=host \
-v /var/run/docker.sock:/tmp/docker.sock \
--restart=always \
gliderlabs/registrator:latest \
-ip=192.168.110.133 \
consul://192.168.110.134:8500
[root@regist ~]# docker ps -a  #查看容器是否运行
CONTAINER ID   IMAGE                           COMMAND                  CREATED         STATUS         PORTS     NAMES
4dfeea0e90e0   gliderlabs/registrator:latest   "/bin/registrator -i…"   3 minutes ago   Up 3 minutes             registrator
[root@regist ~]#

 #测试发现功能是否正常,浏览器访问http://192.168.110.134:8500/,“单击NODES”,然后单击

“consurl-server01”,查看http和nginx服务是否注册到consul。

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

4、consul:192.168.110.134节点部署nginx服务

[root@consul ~]# yum install -y gcc pcre-devel zlib-devel
[root@consul ~]# cd /opt   #此目录下上传nginx安装包
[root@consul opt]# tar zxvf nginx-1.12.2.tar.gz
[root@consul opt]# cd nginx-1.12.2/
[root@consul nginx-1.12.2]#./configure --prefix=/usr/local/nginx
[root@consul nginx-1.12.2]# make && make install
[root@consul ~]# vim /usr/local/nginx/conf/nginx.conf   #修改nginx配置文件

……
http {
  include    mime.types;
  include vhost/*.conf;  //添加虚拟主机目录
  default_type application/octet-stream;
……
[root@consul consul]# mkdir /usr/local/nginx/conf/vhost  #创建虚拟主机目录
[root@consul consul]# mkdir /var/log/nginx  #创建日志文件目录
[root@consul consul]# /usr/local/nginx/sbin/nginx  #启动nginx

5、安装consul-template,并准备template nginx模板文件

#准备template nginx模板文件
[root@consul consul]# vim /root/consul/nginx.ctmpl   

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

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

 

#配置并启动 template
[root@consul ~]# cd /root/consul/
[root@consul consul]# rz -E  #上传consul-template相关软件包
[root@consul consul]# unzip consul-template_0.19.3_linux_amd64.zip  #解压

[root@consul consul]# mv consul-template /usr/bin/
#关联nginx 虚拟目录中的子配置文件操作,指定consul集群的服务IP:端口,并指定模板参数,第一个段是模板地址:第二个段是输出位置:第三个段是输出后重载nginx配置文件。
[root@consul consul]# consul-template -consul-addr 192.168.110.134:8500 \
-template "/root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/kang.conf:/usr/local/nginx/sbin/nginx -s reload" \
--log-level=info

 #重新开一个consul会话终端,查看生成的nginx虚拟终端配置文件

6、测试consul服务发现及配置更新功能

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

#regist服务端注册,创建nginx服务容器

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

#接着我们返回consul端正在监听会话终端,查看动态变化

 #consul端另外一个会话终端查看

 #consul集群查看新增加服务是否被发现

 

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

docker logs -f test-01

docker logs -f test-02

docker logs -f test-05

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值