分布式系统工具Consul

来源:互联网 发布:dj混音软件 编辑:程序博客网 时间:2024/05/17 21:48

在寻找简洁的PaaS工具组合时,发现了Consul这个工具。https://www.consul.io

此开源产品来自于 HashiCorp, 就是提供虚拟机管理工具Vagrant的公司。

标题是服务发现和配置。

有多个模块,但无论服务端还是客户端,都只有一个单一的程序Consul。部署非常简单。

功能有:1)服务发现; 2)健康检查;3)KV存储;4)多数据中心

每个提供服务的host需要运行Consul Agent,作用: registers services, runs health checks, and forwards queries to servers.

Consul Agent需要与一个或多个Consul Server通信。 


服务可以通过文件定义或HTTP API调用来定义。 A service can be registered either by providing a service definition or by making the appropriate calls to theHTTP API.

通过文件定义一个简单的服务:   $ echo '{"service": {"name": "web", "tags": ["rails"], "port": 80}}'   >/etc/consul.d/web.json

启动 :consul agent -dev -config-dir /etc/consul.d

如果有多个网卡,启动会失败,需要加 -advertise=ipaddr 参数。 

调用DNS API来查询服务 :  dig @127.0.0.1 -p 8600 web.service.consul

或者使用HTTP API来查询:  curl http://localhost:8500/v1/catalog/service/web


集群配置

如果要知道其他cosul成员,后续启动的agent必须加入到其中之一。 任意一个,无论它是否是运行在服务器模式下。 那么所有集群成员都会知道新成员的存在。

自动加入集群的机制。 HashiCorp做了一个atlas服务,提供注册账号和一个Token,就可以自动加入集群了。不能上外网就不能这么用。但思路很好。

退出集群。正常退出,状态就是left state,如果不打招呼就退出,那么就是failed状态。

当然,任何节点都提供查询服务。查询consul节点和服务的状态。


健康检查

检查分host级别和service级别。

echo '{"check": {"name": "ping", "script": "ping -c1 google.com >/dev/null", "interval": "30s"}}' > /etc/consul.d/ping.json

echo '{"service": {"name": "web", "tags": ["rails"], "port": 80, "check": {"script": "curl localhost >/dev/null 2>&1", "interval": "10s"}}}'  > /etc/consul.d/web.json

都是采用了脚本监测方法。如果返回值非零,就是表示故障。

5中类型检查逻辑: https://www.consul.io/docs/agent/checks.html


键值存储 :  https://www.consul.io/docs/agent/http/kv.html


还有启动cluster和故障恢复问题。待续。







1 0
原创粉丝点击