etcd api的使用
etcd除了支持直接使用etcdctl进行管理和配置外,还支持使用http API接口进行操作。官方给出的文档也比较详细,具体如下:
1基本操作api: https://github.com/coreos/etcd/blob/6acb3d67fbe131b3b2d5d010e00ec80182be4628/Documentation/v2/api.md
2集群配置api: https://github.com/coreos/etcd/blob/6acb3d67fbe131b3b2d5d010e00ec80182be4628/Documentation/v2/members_api.md
3鉴权认证api: https://github.com/coreos/etcd/blob/6acb3d67fbe131b3b2d5d010e00ec80182be4628/Documentation/v2/auth_api.md
4配置项:https://github.com/coreos/etcd/blob/master/Documentation/op-guide/configuration.md
本篇就结合一些常用操作,进行etcd API的操作。
一、查看系统信息
1、查看版本信息
1# curl -s http://127.0.0.1:2379/version | python -m json.tool
2{
3    "etcdcluster": "3.1.0",
4    "etcdserver": "3.1.11"
5}
2、查看节点信息
 1# curl  -s http://127.0.0.1:2379/v2/members | python -m json.tool
 2{
 3    "members": [
 4        {
 5            "clientURLs": [
 6                "http://localhost:2379"
 7            ],
 8            "id": "e36820a084c25c35",
 9            "name": "etcd-dns-sqtest",
10            "peerURLs": [
11                "http://10.115.19.115:2380"
12            ]
13        }
14    ]
15}
二、增删改查
1、GET查询当前键值
 1# curl -s http://127.0.0.1:2379/v2/keys/skydns | python -m json.tool
 2{
 3    "action": "get",
 4    "node": {
 5        "createdIndex": 4,
 6        "dir": true,
 7        "key": "/skydns",
 8        "modifiedIndex": 4,
 9        "nodes": [
10            {
11                "createdIndex": 4,
12                "dir": true,
13                "key": "/skydns/com",
14                "modifiedIndex": 4
15            },
16            {
17                "createdIndex": 10,
18                "dir": true,
19                "key": "/skydns/migu",
20                "modifiedIndex": 10
21            },
22            {
23                "createdIndex": 22707,
24                "dir": true,
25                "key": "/skydns/arpa",
26                "modifiedIndex": 22707
27            }
28        ]
29    }
30}
2、PUT创建键值对
 1# curl  -s http://127.0.0.1:2379/v2/keys/mysite -X PUT -d value="www.361way.com" | python -m json.tool
 2{
 3    "action": "set",
 4    "node": {
 5        "createdIndex": 45068,
 6        "key": "/mysite",
 7        "modifiedIndex": 45068,
 8        "value": "www.361way.com"
 9    }
10}
3、PUT修改键值
PUT 修改键值:与创建新值几乎相同,但是反馈时会有一个prevNode值反应了修改前存储的内容。
 1# curl  -s http://127.0.0.1:2379/v2/keys/mysite -X PUT -d value="wiki.361way.com" | python -m json.tool
 2{
 3    "action": "set",
 4    "node": {
 5        "createdIndex": 45069,
 6        "key": "/mysite",
 7        "modifiedIndex": 45069,
 8        "value": "wiki.361way.com"
 9    },
10    "prevNode": {
11        "createdIndex": 45068,
12        "key": "/mysite",
13        "modifiedIndex": 45068,
14        "value": "www.361way.com"
15    }
16}
4、删除键值对
 1# curl -s http://127.0.0.1:2379/v2/keys/mysite -X DELETE | python -m json.tool
 2{
 3    "action": "delete",
 4    "node": {
 5        "createdIndex": 45069,
 6        "key": "/mysite",
 7        "modifiedIndex": 45070
 8    },
 9    "prevNode": {
10        "createdIndex": 45069,
11        "key": "/mysite",
12        "modifiedIndex": 45069,
13        "value": "wiki.361way.com"
14    }
15}
5、PUT创建目录
 1# curl -s http://127.0.0.1:2379/v2/keys/blog -XPUT -d dir=true | python -m json.tool
 2{
 3    "action": "set",
 4    "node": {
 5        "createdIndex": 45071,
 6        "dir": true,
 7        "key": "/blog",
 8        "modifiedIndex": 45071
 9    }
10}
6、列出目录下的键值
GET 列出目录下所有的节点信息,最后以/结尾(不是必须的)。还可以通过recursive参数递归列出所有子目录信息。 没有recursive,返回第二级(包括目录和键值)。后面不在返回。
1# curl -s  http://127.0.0.1:2379/v2/keys/skydns | python -m json.tool
2# curl -s  http://127.0.0.1:2379/v2/keys/skydns?recursive=true | python -m json.tool
也可以按顺序GET列出所有创建的有序键,不过不加recursive参数时,默认还是只返回二级目录和健值,后面的不返回。
1# curl -s 'http://127.0.0.1:2379/v2/keys/skydns?sorted=true' | python -m json.tool
7、DELETE 删除目录
默认情况下只允许删除空目录,如果要删除有内容的目录需要加上recursive=true参数。
1?dir=true        删除目录
2?recursive=true      删除非空目录
删除非空目录必须使用 recursive=true 参数,删除空目录,dir=true或recursive=true至少有一个。
1curl 'http://127.0.0.1:2379/v2/keys/dir1?dir=true' -XDELETE | python -m json.tool
2curl 'http://127.0.0.1:2379/v2/keys/dir1?dir=true&recursive=true' -XDELETE | python -m json.tool
本篇幅就先到这里吧,还有一些高级功能,比如members管理、watch监控、定时删除键值等,这个找时间再做单独的篇幅介绍。
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
 - Link: https://blog.361way.com/etcd-api-base/6136.html
 - License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.