consul 命令及接口調用

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, 上面示例中的serviceNameweb

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

Valuebase64 算法加密後的值。可通過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'
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章