Etcd操作简单介绍

2 篇文章 0 订阅

etcd 是一个开源的分布式键值存储系统,为CoreOS集群提供共享配置和服务发现功能。当CoreOS集群中的网络出现分区故障,不再联通或主结点丢失时,etcd可以优雅地处理选主问题。因为其类似ZooKeeper实现了分布式一致性协议raft.

操作例子如下:

读写etcd

基于http的API非常易用,这个指导将展示etcdctl和curl例子, 注意必须为curl加上-L标志, 因为etcd会自动透明地将写操作重定向到etcd集群的master。 

设定key : message 的值为: Hello, 如下:

$ etcdctl set /message Hello
Hello
$ curl -L -X PUT http://127.0.0.1:2379/v2/keys/message -d value="Hello"
{"action":"set","node":{"key":"/message","value":"Hello","modifiedIndex":4,"createdIndex":4}}

读取key: message的值

$ etcdctl get /message
Hello
$ curl -L http://127.0.0.1:2379/v2/keys/message
{"action":"get","node":{"key":"/message","value":"Hello","modifiedIndex":4,"createdIndex":4}}


删除key:

$ etcdctl rm /message

$ curl -L -X DELETE http://127.0.0.1:2379/v2/keys/message
{"action":"delete","node":{"key":"/message","modifiedIndex":19,"createdIndex":4}}


创建一个目录:

$ etcdctl mkdir /foo-service
Cannot print key [/foo-service: Is a directory]
$ etcdctl set /foo-service/container1 localhost:1111
localhost:1111
$ curl -L -X PUT http://127.0.0.1:2379/v2/keys/foo-service/container1 -d value="localhost:1111"
{"action":"set","node":{"key":"/foo-service/container1","value":"localhost:1111","modifiedIndex":17,"createdIndex":17}}

读取目录:

$ etcdctl ls /foo-service
/foo-service/container1
$ curl -L http://127.0.0.1:2379/v2/keys/foo-service
{"action":"get","node":{"key":"/foo-service","dir":true,"nodes":[{"key":"/foo-service/container1","value":"localhost:1111","modifiedIndex":17,"createdIndex":17}],"modifiedIndex":17,"createdIndex":17}}

监视目录:

 Many events can trigger a change, including a new, updated, deleted or expired key.

当此目录有变化时,如新建,更新,删除,过期,监视者将得到通知

$ etcdctl watch --recursive /foo-service

$ curl -L http://127.0.0.1:2379/v2/keys/foo-service?wait=true\&recursive=true

打开另一个shell:

$ etcdctl set /foo-service/container2 localhost:2222
localhost:2222
$ curl -L -X PUT http://127.0.0.1:2379/v2/keys/foo-service/container2 -d value="localhost:2222"
{"action":"set","node":{"key":"/foo-service/container2","value":"localhost:2222","modifiedIndex":23,"createdIndex":23}}

这样当目录变化时,你就可以看到通知,当然现实的系统通常会更新自己本地的配置缓存。

$ etcdctl watch --recursive /foo-service
localhost:2222
$ curl -L http://127.0.0.1:2379/v2/keys/foo-service?wait=true\&recursive=true
{"action":"set","node":{"key":"/foo-service/container2","value":"localhost:2222","modifiedIndex":23,"createdIndex":23}}


监视目录并触发执行一段代码

$ etcdctl exec-watch --recursive /foo-service -- sh -c 'echo "\"$ETCD_WATCH_KEY\" key was updated to \"$ETCD_WATCH_VALUE\" value by \"$ETCD_WATCH_ACTION\" action"'

打开一个新shell

$ etcdctl set /foo-service/container3 localhost:2222
localhost:2222

在第一个shell中将看到变化处发的执行结果 . 可以使用环境变量 $ETCD_WATCH_* , 这些环境变量由 etcdctl设定.

$ etcdctl exec-watch --recursive /foo-service -- sh -c 'echo "\"$ETCD_WATCH_KEY\" key was updated to \"$ETCD_WATCH_VALUE\" value by \"$ETCD_WATCH_ACTION\" action"'
"/foo-service/container3" key was updated to "localhost:2222" value by "set" action

测试和设定

如果之前的值与"Hello"匹配, 则用新值““Hi””替换之

$ etcdctl set /message "Hi" --swap-with-value "Hello"
Hi
$ curl -L -X PUT http://127.0.0.1:2379/v2/keys/message?prevValue=Hello -d value=Hi
{"action":"compareAndSwap","node":{"key":"/message","value":"Hi","modifiedIndex":28,"createdIndex":27}}

TTL过期

设定目录过期时间, 单位为秒

$ etcdctl set /foo "Expiring Soon" --ttl 20
Expiring Soon

curl的响应中包括:过期的绝对时间点,以及相对的时间差。

$ curl -L -X PUT http://127.0.0.1:2379/v2/keys/foo?ttl=20 -d value=bar
{"action":"set","node":{"key":"/foo","value":"bar","expiration":"2014-02-10T19:54:49.357382223Z","ttl":20,"modifiedIndex":31,"createdIndex":31}}

如果你去读取了个过期的目录,将得到100错误码

$ etcdctl get /foo
Error: 100: Key not found (/foo) [32]
$ curl -L http://127.0.0.1:2379/v2/keys/foo
{"errorCode":100,"message":"Key not found","cause":"/foo","index":32}
注意: 此文章只是我个人笔记, 如有错漏,请一定指正, 共同学习, 我的邮箱: htyu_0203_39@sina.com

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值