Consul简介
Consul有多个组件,但总体而言,它是基础架构中的一款服务发现和配置的工具。 它提供了几个关键功能:
-
服务发现:Consul client 可以提供服务,例如api或mysql,也可以使用Consul client来发现指定服务的提供者。 使用DNS或HTTP,应用程序可以轻松找到他们所依赖的服务。
-
健康检查:Consul client 可以提供任何数量的健康检查,或者与给定的服务(“Web服务器是否返回200 OK”),或与本地节点(“内存利用率是否低于90%”)相关联。 可以使用此信息来监控集群运行状况,服务发现组件使用此信息将流量从有问题的主机中移除出去。
-
KV Store:应用程序可以使用Consul的分层键/值存储,包括动态配置,功能标记,协调,leader选举等等。 简单的HTTP API使其易于使用。
-
多数据中心:Consul支持多个数据中心。 这意味着Consul的用户不必担心构建额外的抽象层以扩展到多个区域。
Consul旨在对DevOps社区和应用程序开发人员友好,使其成为现代化,弹性基础架构的完美选择。
Consul的安装
打开官网的下载地址
选择对应的版本进行下载。本文是再Windows环境下的安装与配置。
下载完成后解压会得到一个exe文件。 将这个exe文件放在C:\Windows\system32
这个目录下面然后打开cmd。
输入consul检擦是否安装完成。
PS C:\Windows\system32> consul
Usage: consul [--version] [--help] <command> [<args>]
Available commands are:
acl Interact with Consul's ACLs
agent Runs a Consul agent
catalog Interact with the catalog
config Interact with Consul's Centralized Configurations
connect Interact with Consul Connect
debug Records a debugging archive for operators
event Fire a new event
exec Executes a command on Consul nodes
force-leave Forces a member of the cluster to enter the "left" state
info Provides debugging information for operators.
intention Interact with Connect service intentions
join Tell Consul agent to join cluster
keygen Generates a new encryption key
keyring Manages gossip layer encryption keys
kv Interact with the key-value store
leave Gracefully leaves the Consul cluster and shuts down
lock Execute a command holding a lock
login Login to Consul using an auth method
logout Destroy a Consul token created with login
maint Controls node or service maintenance mode
members Lists the members of a Consul cluster
monitor Stream logs from a Consul agent
operator Provides cluster-level tools for Consul operators
reload Triggers the agent to reload configuration files
rtt Estimates network round trip time between nodes
services Interact with services
snapshot Saves, restores and inspects snapshots of Consul server state
tls Builtin helpers for creating CAs and certificates
validate Validate config files/directories
version Prints the Consul version
watch Watch for changes in Consul
这样就表示安装成功了。
Consul运行与配置
输入consul agent -dev
PS C:\Windows\system32> consul agent -dev
==> Starting Consul agent...
==> Consul agent running!
Version: 'v1.5.1'
Node ID: '262ed46b-3d26-8907-3ce9-b094d1eeca7e'
Node name: 'DESKTOP-FERRARI'
Datacenter: 'dc1' (Segment: '<all>')
Server: true (Bootstrap: false)
Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, gRPC: 8502, DNS: 8600)
Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false
==> Log data will now stream in as it occurs:
2019/07/03 11:44:04 [DEBUG] agent: Using random ID "262ed46b-3d26-8907-3ce9-b094d1eeca7e" as node ID
2019/07/03 11:44:04 [DEBUG] tlsutil: Update with version 1
2019/07/03 11:44:04 [DEBUG] tlsutil: OutgoingRPCWrapper with version 1
2019/07/03 11:44:04 [DEBUG] tlsutil: IncomingRPCConfig with version 1
2019/07/03 11:44:04 [DEBUG] tlsutil: OutgoingRPCWrapper with version 1
2019/07/03 11:44:04 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:262ed46b-3d26-8907-3ce9-b094d1eeca7e Address:127.0.0.1:8300}]
2019/07/03 11:44:04 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader: "")
2019/07/03 11:44:04 [INFO] serf: EventMemberJoin: DESKTOP-FERRARI.dc1 127.0.0.1
2019/07/03 11:44:04 [INFO] serf: EventMemberJoin: DESKTOP-FERRARI 127.0.0.1
2019/07/03 11:44:04 [INFO] consul: Adding LAN server DESKTOP-FERRARI (Addr: tcp/127.0.0.1:8300) (DC: dc1)
2019/07/03 11:44:04 [INFO] consul: Handled member-join event for server "DESKTOP-FERRARI.dc1" in area "wan"
2019/07/03 11:44:04 [DEBUG] agent/proxy: managed Connect proxy manager started
2019/07/03 11:44:04 [INFO] agent: Started DNS server 127.0.0.1:8600 (tcp)
2019/07/03 11:44:04 [INFO] agent: Started DNS server 127.0.0.1:8600 (udp)
2019/07/03 11:44:04 [INFO] agent: Started HTTP server on 127.0.0.1:8500 (tcp)
2019/07/03 11:44:04 [INFO] agent: started state syncer
2019/07/03 11:44:04 [INFO] agent: Started gRPC server on 127.0.0.1:8502 (tcp)
2019/07/03 11:44:04 [WARN] raft: Heartbeat timeout from "" reached, starting election
2019/07/03 11:44:04 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state in term 2
2019/07/03 11:44:04 [DEBUG] raft: Votes needed: 1
2019/07/03 11:44:04 [DEBUG] raft: Vote granted from 262ed46b-3d26-8907-3ce9-b094d1eeca7e in term 2. Tally: 1
2019/07/03 11:44:04 [INFO] raft: Election won. Tally: 1
2019/07/03 11:44:04 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state
2019/07/03 11:44:04 [INFO] consul: cluster leadership acquired
2019/07/03 11:44:04 [INFO] consul: New leader elected: DESKTOP-FERRARI
2019/07/03 11:44:04 [INFO] connect: initialized primary datacenter CA with provider "consul"
2019/07/03 11:44:04 [DEBUG] consul: Skipping self join check for "DESKTOP-FERRARI" since the cluster is too small
2019/07/03 11:44:04 [INFO] consul: member 'DESKTOP-FERRARI' joined, marking health alive
2019/07/03 11:44:04 [DEBUG] agent: Skipping remote check "serfHealth" since it is managed automatically
2019/07/03 11:44:04 [INFO] agent: Synced node info
2019/07/03 11:44:04 [DEBUG] agent: Node info in sync
2019/07/03 11:44:05 [DEBUG] agent: Skipping remote check "serfHealth" since it is managed automatically
2019/07/03 11:44:05 [DEBUG] agent: Node info in sync
出现这样表示运行成功了。
打开 http://127.0.0.1:8500/ui/dc1/services
查看consul的运行状态。
服务注册
创建consul.d的配置文件夹
mkdir ./consul.d
创建一个服务的.json的配置文件。
echo '{"service": {"name": "web", "tags": ["rails"], "port": 80}}' > ./consul.d/web.json
重启agent
consul agent -dev -config-dir=./consul.d
==> Starting Consul agent...
...
[INFO] agent: Synced service 'web'
...
DNS API
dig @127.0.0.1 -p 8600 web.service.consul
;; QUESTION SECTION:
;web.service.consul. IN A
;; ANSWER SECTION:
web.service.consul. 0 IN A 127.0.0.1
再用SRV的方式检查一下
C:\Windows\system32>dig @127.0.0.1 -p 8600 web.service.consul SRV
; <<>> DiG 9.9.7 <<>> @127.0.0.1 -p 8600 web.service.consul SRV
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 63393
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 3
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;web.service.consul. IN SRV
;; ANSWER SECTION:
web.service.consul. 0 IN SRV 1 1 80 DESKTOP-FERRARI.node.dc1.consul.
;; ADDITIONAL SECTION:
DESKTOP-FERRARI.node.dc1.consul. 0 IN A 127.0.0.1
DESKTOP-FERRARI.node.dc1.consul. 0 IN TXT "consul-network-segment="
;; Query time: 0 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Wed Jul 03 14:14:49 ?D1ú±ê×?ê±?? 2019
;; MSG SIZE rcvd: 150
可以看到他说已经在一台电脑上注册了这个DNS.
我们还可以使用域名过滤的方式在注册一个
dig @127.0.0.1 -p 8600 rails.web.service.consul
; <<>> DiG 9.9.7 <<>> @127.0.0.1 -p 8600 rails.web.service.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 22904
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 2
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;rails.web.service.consul. IN A
;; ANSWER SECTION:
rails.web.service.consul. 0 IN A 127.0.0.1
;; ADDITIONAL SECTION:
rails.web.service.consul. 0 IN TXT "consul-network-segment="
;; Query time: 0 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Wed Jul 03 14:16:52 ?D1ú±ê×?ê±?? 2019
;; MSG SIZE rcvd: 105
再用 HTTP API来查看服务
curl http://localhost:8500/v1/catalog/service/web
[
{
"ID": "b9896ed0-77f7-6f47-05d0-a76c32bd592d",
"Node": "DESKTOP-FERRARI",
"Address": "127.0.0.1",
"Datacenter": "dc1",
"TaggedAddresses": {
"lan": "127.0.0.1",
"wan": "127.0.0.1"
},
"NodeMeta": {
"consul-network-segment": ""
},
"ServiceKind": "",
"ServiceID": "web",
"ServiceName": "web",
"ServiceTags": [
"rails"
],
"ServiceAddress": "",
"ServiceWeights": {
"Passing": 1,
"Warning": 1
},
"ServiceMeta": {},
"ServicePort": 80,
"ServiceEnableTagOverride": false,
"ServiceProxyDestination": "",
"ServiceProxy": {},
"ServiceConnect": {},
"CreateIndex": 10,
"ModifyIndex": 10
}
]
Consul 健康监测
创建两个检测的文件
echo '{"check": {"name": "ping", "args": ["ping", "-c1", "google.com"], "interval": "30s"}}' >./consul.d/ping.json
echo '{"service": {"name": "web", "tags": ["rails"], "port": 80, "check": {"args": ["curl", "localhost"], "interval": "10s"}}}' >./consul.d/web.json
重启一下consul agent
consul agent -dev -config-dir ./consul.d -enable-script-checks=true
现在来检查一下
curl http://localhost:8500/v1/health/state/critical
StatusCode : 200
StatusDescription : OK
Content : [
{
"Node": "DESKTOP-FERRARI",
"CheckID": "service:web",
"Name": "Service 'web' check",
"Status": "critical",
"Notes": "",
"Output": " % Total %...
RawContent : HTTP/1.1 200 OK
Vary: Accept-Encoding
X-Consul-Effective-Consistency: leader
X-Consul-Index: 28
X-Consul-Knownleader: true
X-Consul-Lastcontact: 0
Content-Length: 786
Content-Type: application/...
Forms : {}
Headers : {[Vary, Accept-Encoding], [X-Consul-Effective-Consistency, leader], [X-Consul-Index, 28], [X-Consul
-Knownleader, true]...}
Images : {}
InputFields : {}
Links : {}
ParsedHtml : mshtml.HTMLDocumentClass
RawContentLength : 786