文章目录
Install
pass
Cli
https://www.consul.io/docs/commands/index.html
Services
add
可以通过配置文件以及Http api
来添加service。
其中通过配置文件添加,或者修改操作需要重启consul服务,而http api
方式则不用。
service definition
保存在/etc/consul.d
目录下
{
"service": {
"name": "web",
"tags": ["rails"],
"port": 80,
"address": "192.168.100.122"
}
}
启动consul时,添加-config-dir=/etc/consul.d
。
eg:
consul agent -dev -config-dir=/etc/consul.d -ui -client=0.0.0.0
完整 service
配置示例:
https://www.consul.io/docs/agent/services.html
http api
定义test.json
:
{
"ID": "test111",
"Name": "test",
"Tags": [
"v1",
"test"
],
"Address": "192.168.100.122",
"Port": 80
}
注册:
# curl --request PUT --data @test.json http://localhost:8500/v1/agent/service/register -v
* About to connect() to localhost port 8500 (#0)
* Trying ::1...
* Connected to localhost (::1) port 8500 (#0)
> PUT /v1/agent/service/register HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:8500
> Accept: */*
> Content-Length: 146
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 146 out of 146 bytes
< HTTP/1.1 200 OK
< Date: Tue, 25 Jun 2019 04:35:41 GMT
< Content-Length: 0
< Content-Type: text/plain; charset=utf-8
<
* Connection #0 to host localhost left intact
查询:
# curl http://localhost:8500/v1/catalog/service/test
[
{
"ID": "e4643884-4972-0406-06bf-fec189123b4e",
"Node": "test.walker.com",
"Address": "127.0.0.1",
"Datacenter": "dc1",
"TaggedAddresses": {
"lan": "127.0.0.1",
"wan": "127.0.0.1"
},
"NodeMeta": {
"consul-network-segment": ""
},
"ServiceID": "test111",
"ServiceName": "test",
"ServiceTags": [
"v1",
"test"
],
"ServiceAddress": "192.168.100.122",
"ServicePort": 80,
"ServiceEnableTagOverride": false,
"CreateIndex": 200,
"ModifyIndex": 200
}
]
Query
注册服务后,可通过dns
或者 http api
两种方式来查询
Dns
# dig @127.0.0.1 -p 8600 web.serices.consul
; <<>> DiG 9.9.4-RedHat-9.9.4-74.el7_6.1 <<>> @127.0.0.1 -p 8600 web.service.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 4107
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;web.service.consul. IN A
;; ANSWER SECTION:
web.service.consul. 0 IN A 192.168.100.122
;; Query time: 0 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: 二 6月 25 11:44:30 CST 2019
;; MSG SIZE rcvd: 63
A 记录中对应注册时的服务地址。
查询地址格式为
<serviceName>.services.consul
, 上面示例中的serviceName
为web
Http api
[root@test ~]# curl http://localhost:8500/v1/catalog/service/web
[
{
"ID": "e4643884-4972-0406-06bf-fec189123b4e",
"Node": "test.walker.com",
"Address": "127.0.0.1",
"Datacenter": "dc1",
"TaggedAddresses": {
"lan": "127.0.0.1",
"wan": "127.0.0.1"
},
"NodeMeta": {
"consul-network-segment": ""
},
"ServiceID": "web",
"ServiceName": "web",
"ServiceTags": [
"rails"
],
"ServiceAddress": "192.168.100.122",
"ServicePort": 80,
"ServiceEnableTagOverride": false,
"CreateIndex": 6,
"ModifyIndex": 6
}
]
K-V
https://learn.hashicorp.com/consul/getting-started/kv
Add & Update
cli
# consul kv put foo bar
# consul kv put redis/config/maxconns 1000
api
# curl --request PUT --data "hello world" http://localhost:9500/v1/kv/foo
[
{
"LockIndex": 0,
"Key": "foo",
"Flags": 0,
"Value": "ImhlbGxvIHdvcmxkIg==",
"CreateIndex": 771,
"ModifyIndex": 771
}
]
[root@test ~]# curl --request PUT --data 1000 http://localhost:8500/v1/kv/redis/config/minconns
true
Value
为base64
算法加密后的值。可通过echo "ImhlbGxvIHdvcmxkIg==" | base64 -d
来查看
Get
cli
# consul kv get foo
# consul kv get redis/config/maxconns
# consul kv get -recurse redis
api
# curl http://localhost:8500/v1/kv/foo
[
{
"LockIndex": 0,
"Key": "foo",
"Flags": 0,
"Value": "ImhlbGxvIHdvcmxkIg==",
"CreateIndex": 771,
"ModifyIndex": 771
}
]
# curl "http://localhost:8500/v1/kv/redis?recurse=true"
[
{
"LockIndex": 0,
"Key": "redis/config/minconns",
"Flags": 0,
"Value": "MTAwMA==",
"CreateIndex": 806,
"ModifyIndex": 806
}
]
Delete
cli
# consul kv delete foo
# consul kv delete -recurse redis
api
[root@test ~]# curl --request DELETE http://localhost:8500/v1/kv/foo
true
# curl --request DELETE 'http://localhost:8500/v1/kv/redis?recurse=true'