etcd api的使用

etcd除了支持直接使用etcdctl进行管理和配置外,还支持使用http API接口进行操作。官方给出的文档也比较详细,具体如下:

基本操作api: https://github.com/coreos/etcd/blob/6acb3d67fbe131b3b2d5d010e00ec80182be4628/Documentation/v2/api.md
集群配置api: https://github.com/coreos/etcd/blob/6acb3d67fbe131b3b2d5d010e00ec80182be4628/Documentation/v2/members_api.md
鉴权认证api: https://github.com/coreos/etcd/blob/6acb3d67fbe131b3b2d5d010e00ec80182be4628/Documentation/v2/auth_api.md
配置项:https://github.com/coreos/etcd/blob/master/Documentation/op-guide/configuration.md

本篇就结合一些常用操作,进行etcd API的操作。

一、查看系统信息

1、查看版本信息

# curl -s http://127.0.0.1:2379/version | python -m json.tool
{
    "etcdcluster": "3.1.0",
    "etcdserver": "3.1.11"
}

2、查看节点信息

# curl  -s http://127.0.0.1:2379/v2/members | python -m json.tool
{
    "members": [
        {
            "clientURLs": [
                "http://localhost:2379"
            ],
            "id": "e36820a084c25c35",
            "name": "etcd-dns-sqtest",
            "peerURLs": [
                "http://10.115.19.115:2380"
            ]
        }
    ]
}

二、增删改查

1、GET查询当前键值

# curl -s http://127.0.0.1:2379/v2/keys/skydns | python -m json.tool
{
    "action": "get",
    "node": {
        "createdIndex": 4,
        "dir": true,
        "key": "/skydns",
        "modifiedIndex": 4,
        "nodes": [
            {
                "createdIndex": 4,
                "dir": true,
                "key": "/skydns/com",
                "modifiedIndex": 4
            },
            {
                "createdIndex": 10,
                "dir": true,
                "key": "/skydns/migu",
                "modifiedIndex": 10
            },
            {
                "createdIndex": 22707,
                "dir": true,
                "key": "/skydns/arpa",
                "modifiedIndex": 22707
            }
        ]
    }
}

2、PUT创建键值对

# curl  -s http://127.0.0.1:2379/v2/keys/mysite -X PUT -d value="www.361way.com" | python -m json.tool
{
    "action": "set",
    "node": {
        "createdIndex": 45068,
        "key": "/mysite",
        "modifiedIndex": 45068,
        "value": "www.361way.com"
    }
}

3、PUT修改键值

PUT 修改键值:与创建新值几乎相同,但是反馈时会有一个prevNode值反应了修改前存储的内容。

# curl  -s http://127.0.0.1:2379/v2/keys/mysite -X PUT -d value="wiki.361way.com" | python -m json.tool
{
    "action": "set",
    "node": {
        "createdIndex": 45069,
        "key": "/mysite",
        "modifiedIndex": 45069,
        "value": "wiki.361way.com"
    },
    "prevNode": {
        "createdIndex": 45068,
        "key": "/mysite",
        "modifiedIndex": 45068,
        "value": "www.361way.com"
    }
}

4、删除键值对

# curl -s http://127.0.0.1:2379/v2/keys/mysite -X DELETE | python -m json.tool
{
    "action": "delete",
    "node": {
        "createdIndex": 45069,
        "key": "/mysite",
        "modifiedIndex": 45070
    },
    "prevNode": {
        "createdIndex": 45069,
        "key": "/mysite",
        "modifiedIndex": 45069,
        "value": "wiki.361way.com"
    }
}

5、PUT创建目录

# curl -s http://127.0.0.1:2379/v2/keys/blog -XPUT -d dir=true | python -m json.tool
{
    "action": "set",
    "node": {
        "createdIndex": 45071,
        "dir": true,
        "key": "/blog",
        "modifiedIndex": 45071
    }
}

6、列出目录下的键值

GET 列出目录下所有的节点信息,最后以/结尾(不是必须的)。还可以通过recursive参数递归列出所有子目录信息。 没有recursive,返回第二级(包括目录和键值)。后面不在返回。

# curl -s  http://127.0.0.1:2379/v2/keys/skydns | python -m json.tool
# curl -s  http://127.0.0.1:2379/v2/keys/skydns?recursive=true | python -m json.tool

也可以按顺序GET列出所有创建的有序键,不过不加recursive参数时,默认还是只返回二级目录和健值,后面的不返回。

# curl -s 'http://127.0.0.1:2379/v2/keys/skydns?sorted=true' | python -m json.tool

7、DELETE 删除目录

默认情况下只允许删除空目录,如果要删除有内容的目录需要加上recursive=true参数。

?dir=true        删除目录
?recursive=true      删除非空目录

删除非空目录必须使用 recursive=true 参数,删除空目录,dir=true或recursive=true至少有一个。

curl 'http://127.0.0.1:2379/v2/keys/dir1?dir=true' -XDELETE | python -m json.tool
curl 'http://127.0.0.1:2379/v2/keys/dir1?dir=true&recursive=true' -XDELETE | python -m json.tool

本篇幅就先到这里吧,还有一些高级功能,比如members管理、watch监控、定时删除键值等,这个找时间再做单独的篇幅介绍。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注