etcd的简单使用
2016-09-03 12:04
337 查看
etcd的简单使用
ETCD安装配置
安装
去chmod+xetcd chmod+xetcdctl
并将etcd和etcdctl复制到/bin
配置启动
简单启动
./bin/etcd这样就可以启动使用集群配置
在两台机器上部署了简单的集群192.168.231.130 192.168.231.132
在配置文件/etc/defalut/etcd中增加:
ETCD_OPTS="-nameinfra0-initial-advertise-peer-urlshttp://192.168.231.130:2380-listen-peer-urls http://192.168.231.130:2380-initial-cluster-
tokenetcd-cluster-1-initial-clusterinfra0=http://192.168.231.130:2380,infra1=http://192.168.231.132:2380-initial-cluster-statenew"
也可用参数的方法
ETCD_INITIAL_CLUSTER="infra0=http://192.168.231.130:2380,infra1=http://192.168.231.132:2380" ETCD_INITIAL_CLUSTER_STATE=new
192.168.231.132机器上
ETCD_OPTS="-nameinfra1-initial-advertise-peer-urlshttp://192.168.231.132:2380-listen-peer-urls http://192.168.231.132:2380-initial-cluster-tokenetcd-cluster-1-initial-clusterinfra0=http://192.168.231.130:2380,infra1=http://192.168.231.132:2380-initial-cluster-statenew"
参数解释启动etcd进程后查看集群
etcdctlmemberlist
27e6981eec74137d:name=infra0peerURLs=http://192.168.231.130:2380clientURLs=http://localhost:2379,http://localhost:4001
3955a9b061e52de1:name=infra1peerURLs=http://192.168.231.132:2380clientURLs=http://localhost:2379,http://localhost:4001
判断leader和followers
curlhttp://127.0.0.1:2379/v2/stats/leader
{"leader":"27e6981eec74137d","followers":{"3955a9b061e52de1":{"latency":{"current":0.178536,"average":0.26406266231884085,"standardDeviation":0.3787246458449882,"minimum":0.084328,"maximum":10.527117},"counts":{"fail":0,"success":1380}}}}
协议
简单发送一个请求设置key值的请求#curl-vvvhttp://127.0.0.1:2379/v2/keys/mykey-XPUT-dvalue="thisistest"
*HostnamewasNOTfoundinDNScache
*Trying127.0.0.1...
*Connectedto127.0.0.1(127.0.0.1)port2379(#0)
>PUT/v2/keys/mykeyHTTP/1.1
>User-Agent:curl/7.35.0
>Host:127.0.0.1:2379
>Accept:*/*
>Content-Length:18
>Content-Type:application/x-www-form-urlencoded
>
*uploadcompletelysentoff:18outof18bytes
<HTTP/1.1200OK
<Content-Type:application/json
<X-Etcd-Cluster-Id:69bc358c20384a4c
<X-Etcd-Index:16333
<X-Raft-Index:87030
<X-Raft-Term:117
<Date:Fri,14Aug201501:39:39GMT
<Content-Length:201
<
{"action":"set","node":{"key":"/mykey","value":"thisistest","modifiedIndex":16333,"createdIndex":16333},"prevNode":{"key":"/mykey","value":"thisistest","modifiedIndex":14840,"createdIndex":14840}}
*Connection#0tohost127.0.0.1leftintact
http接口是restapi的风格body里面字段详解:action:set操作对应的是url的put,restapi的风格node.key:设置的key值node.value:设置的value值node.createdIndex:唯一的整数,每当etcd有改变时,这个值也会发生变化.不仅限于key值操作,包括增加和同步服务改变。这里要修改node.modifiedIndex:和createdIndex类似,也是一个唯一证整数set,delete,update,create,-compareAndSwap,compareAndDelete这些操作都会改变这个值,而get和watch命令不会修改改变这个值header字段详解:
X-Etcd-Cluster-Id:69bc358c20384a4cX-Etcd-Index等同于createdIndex.X-Etcd-Indexisthecurrentetcdindexwhenthewatchstarts,whichmeansthatthewatchedeventmayhappenafterX-Etcd-IndexX-Raft-Index类似etcdindex,但是用于raftprotocolX-Raft-Termisanintegerthatwillincreasewheneveranetcdmasterelectionhappensinthecluster.Ifthisnumberisincreasingrapidly,youmayneedtotunetheelectiontimeout.Seethetuningsectionfordetails.可以使用etcdctl简化操作
X-Etcd-Index:16333
X-Raft-Index:87030
X-Raft-Term:117
#etcdctlgetmykey
thisistest
基本操作
可以使用etcdctl或者url去执行手动操作etcdctl几个有用的附加命令–debug将指令的url显示出来#etcdctl--debuggetmykey–output/-o将输出以指定的方式显示出来
Cluster-Endpoints:http://localhost:2379,http://localhost:2379, http://localhost:4001,http://localhost:4001
Curl-Example:curl-XGEThttp://localhost:2379/v2/keys/mykey?consistent=true&recursive=false&sorted=falsethisistest
#etcdctl-ojsongetmykey
{"action":"get","node":{"key":"/mykey","value":"12345","modifiedIndex":18134,"createdIndex":18134},"etcdIndex":18168,"raftIndex":96532,"raftTerm":124}#etcdctlgetmykey
12345
查看版本
curl-Lhttp://127.0.0.1:2379/version
etcdctl–version#etcdctl--version
etcdctlversion2.0.13
设定键值
etcdctlsetkeyvalue如想要创建一个{mykey,kkkkk}的键值
curl-XPUThttp://localhost:2379/v2/keys/key-dvalue=value
#etcdctl--debug-ojsonsetmykeykkkkk
Curl-Example:curl-XPUThttp://localhost:2379/v2/keys/mykey-dvalue=kkkkk
{"action":"set","node":{"key":"/mykey","value":"kkkkk","modifiedIndex":21283,"createdIndex":21283},"prevNode":{"key":"/mykey","value":"kkkkk","modifiedIndex":20087,"createdIndex":20087},"etcdIndex":21283,"raftIndex":112958,"raftTerm":149}#etcdctl--debug-ojsongetmykey
Curl-Example:curl-XGEThttp://localhost:2379/v2/keys/mykey?consistent=true&recursive=false&sorted=false{"action":"get","node":{"key":"/mykey","value":"kkkkk","modifiedIndex":21283,"createdIndex":21283},"etcdIndex":21283,"raftIndex":112963,"raftTerm":149}
这里显示了前一个值,设置后再get返回的是最新的值etcdctlmkkeyvalue也能起到创建并设置键值的作用,和set操作的区别主要是,不能对已存在的key进行创建的操作
#etcdctl--debug-ojsonmkaa11
Curl-Example:curl-XPUThttp://localhost:2379/v2/keys/aa?prevExist=false-dvalue=11
{"action":"create","node":{"key":"/aa","value":"11","modifiedIndex":21093,"createdIndex":21093},"etcdIndex":21093,"raftIndex":112010,"raftTerm":149}#etcdctl--debug-ojsonmkaa22
Curl-Example:curl-XPUThttp://localhost:2379/v2/keys/aa?prevExist=false-dvalue=22
Error:105:Keyalreadyexists(/aa)[21098]
查看键值
etcdctlgetkey
curl-XGEThttp://localhost:2379/v2/keys/key?
etcdctl--debug-ojsongetmykey
Cluster-Endpoints:http://localhost:2379,http://localhost:2379, http://localhost:4001,http://localhost:4001
Curl-Example:curl-XGEThttp://localhost:2379/v2/keys/mykey?consistent=true&recursive=false&sorted=false{"action":"get","node":{"key":"/mykey","value":"12345","modifiedIndex":18134,"createdIndex":18134},"etcdIndex":18661,"raftIndex":99095,"raftTerm":126}
查看键值
etcdctlrmkey
curl-XDELETEhttp://localhost:2379/v2/keys/key?
#etcdctl--debug-ojsonrmmykey
Cluster-Endpoints:http://localhost:2379,http://localhost:2379, http://localhost:4001,http://localhost:4001
Curl-Example:curl-XDELETEhttp://localhost:2379/v2/keys/mykey?dir=false&recursive=false{"action":"delete","node":{"key":"/mykey","modifiedIndex":18766,"createdIndex":18701},"prevNode":{"key":"/mykey","value":"kkkkk","modifiedIndex":18701,"createdIndex":18701},"etcdIndex":18766,"raftIndex":99607,"raftTerm":127}#etcdctl--debug-ojsongetmykey
Cluster-Endpoints:http://localhost:2379,http://localhost:2379, http://localhost:4001,http://localhost:4001
Curl-Example:curl-XGEThttp://localhost:2379/v2/keys/mykey?consistent=true&recursive=false&sorted=falseError:100:Keynotfound(/mykey)[18766]
设置键值的TTL
etcdctlsetkeyvalue–ttltimecurl-XPUTetcdctl--debug-ojsonsetmykeyok--ttl100
Cluster-Endpoints:http://localhost:2379,http://localhost:2379, http://localhost:4001,http://localhost:4001
Curl-Example:curl-XPUThttp://localhost:2379/v2/keys/mykey-dvalue=ok-dttl=100
{"action":"set","node":{"key":"/mykey","value":"ok","expiration":"2015-08-14T03:15:14.665295244Z","ttl":100,"modifiedIndex":19036,"createdIndex":19036},"etcdIndex":19036,"raftIndex":100957,"raftTerm":129}
过了几十秒查看,ttl减为23
#etcdctl--debug-ojsongetmykey
Cluster-Endpoints:http://localhost:2379,http://localhost:2379, http://localhost:4001,http://localhost:4001
Curl-Example:curl-XGEThttp://localhost:2379/v2/keys/mykey?consistent=true&recursive=false&sorted=false{"action":"get","node":{"key":"/mykey","value":"ok","expiration":"2015-08-14T03:15:14.665295244Z","ttl":23,"modifiedIndex":19036,"createdIndex":19036},"etcdIndex":19077,"raftIndex":101148,"raftTerm":129}
当ttl减为0,这个key值就查询不到了
Error:100:Keynotfound(/mykey)[19084]
目录的ttl设置方法和key的类似
监控键值的改动
etcdctlwatchkey监听键值的改动,然后输出信息,–after-index可以根据etcd-index来获取后续index对应的改动
curl-XGEThttp://localhost:2379/v2/keys/key?consistent=true&wait=true
etcdctl--debugwatchmykey
Cluster-Endpoints:http://localhost:2379,http://localhost:2379, http://localhost:4001,http://localhost:4001
Curl-Example:curl-XGET'target='_blank'>http://localhost:2379/v2/keys/mykey?consistent=true&wait=true[/code]
当监测到键值设置为kkkkk时打印kkkkk
若etcdctlwatch带上–forever参数则会不退出一直监测键值的改动,比如这里检测到了set和delete操作#etcdctl--debug-ojsonwatchmykey--forever
Curl-Example:curl-XGEThttp://localhost:2379/v2/keys/mykey?consistent=true&wait=true
{"action":"set","node":{"key":"/mykey","value":"kkkkk","modifiedIndex":19656,"createdIndex":19656},"prevNode":{"key":"/mykey","value":"kkkkk","modifiedIndex":19645,"createdIndex":19645},"etcdIndex":19655,"raftIndex":104107,"raftTerm":131}Curl-Example:curl-XGEThttp://localhost:2379/v2/keys/mykeyconsistent=true&wait=true&waitIndex=19657
{"action":"delete","node":{"key":"/mykey","modifiedIndex":19661,"createdIndex":19656},"prevNode":{"key":"/mykey","value":"kkkkk","modifiedIndex":19656,"createdIndex":19656},"etcdIndex":19656,"raftIndex":104115,"raftTerm":131}Curl-Example:curl-XGET'target='_blank'>http://localhost:2379/v2/keys/mykey?consistent=true&wait=true&waitIndex=19662[/code]
对于目录类型的key可以用–recursive来检测目录下的子keys的改动同样可以用作监测的命令是exec-watch,相比watch,增加了监测到改动可以执行自定义的操作etcdctlwatchkeycommand#etcdctlexec-watchmykey--sh-c'echohit'
hit目录相关的操作
etcdctlmkdirdirname创建一个目录可以关联多个子keys,比如先建立一个名叫mydir的diretcdctl--debug-ojsonmkdirmydir
Curl-Example:curl-XPUT'target='_blank'>http://localhost:2379/v2/keys/mydir?dir=true&prevExist=false[/code]
在这个dir下面可以建立多个keys#etcdctl--debug-ojsonset/mydir/key111111
Curl-Example:curl-XPUThttp://localhost:2379/v2/keys/mydir/key1-dvalue=11111
{"action":"set","node":{"key":"/mydir/key1","value":"11111","modifiedIndex":20850,"createdIndex":20850},"etcdIndex":20850,"raftIndex":110723,"raftTerm":148}#etcdctl--debug-ojsonset/mydir/key222222
Curl-Example:curl-XPUThttp://localhost:2379/v2/keys/mydir/key2-dvalue=22222
{"action":"set","node":{"key":"/mydir/key2","value":"22222","modifiedIndex":20855,"createdIndex":20855},"etcdIndex":20855,"raftIndex":110747,"raftTerm":148}
可以用etcdctllsdirname进行查看,得到该dir下面的2个key#etcdctl--debug-ojsonls/mydir
Curl-Example:curl-XGEThttp://localhost:2379/v2/keys/mydir?consistent=true&recursive=false&sorted=false/mydir/key2
/mydir/key1
如果向目录名的路径post一个value,那么在这个目录下面会自动用createdIndex创建一个key#curl-XPOSThttp://localhost:2379/v2/keys/mydir-dvalue=33333
{"action":"create","node":{"key":"/mydir/21442","value":"33333","modifiedIndex":21442,"createdIndex":21442}}#etcdctl--debug-ojsonls/mydir
Curl-Example:curl-XGEThttp://localhost:2379/v2/keys/mydir?consistent=true&recursive=false&sorted=false/mydir/key1
/mydir/key2
/mydir/21442
用rmdir删除目录要保证目录下没有keys,不然会失败#etcdctl--debug-ojsonrmdirmydir
Curl-Example:curl-XDELETEhttp://localhost:2379/v2/keys/mydir?dir=true&recursive=falseError:108:Directorynotempty(/mydir)[21682]
若要将目录和keys都删除可以用rm指令#etcdctl--debug-ojsonrmmydir--recursive=true
Curl-Example:curl-XDELETEhttp://localhost:2379/v2/keys/mydir?dir=false&recursive=true
#etcdctl--debug-ojsonls/mydir
Curl-Example:curl-XGEThttp://localhost:2379/v2/keys/mydir?consistent=true&recursive=false&sorted=falseError:100:Keynotfound(/mydir)[21883] 状态查看
etcd分别提供了查看leader、自己以及store状态的接口查看leader的状态curl查看自己的状态http://127.0.0.1:2379/v2/stats/leader{"leader":"27e6981eec74137d","followers":{"3955a9b061e52de1":{"latency":{"current":0.158241,"average":0.22540039942528703,"standardDeviation":0.17653730983599686,"minimum":0.087808,"maximum":1.988291},"counts":{"fail":0,"success":348}}}} curl查看store的状态http://127.0.0.1:2379/v2/stats/self#curl http://127.0.0.1:2379/v2/stats/self{"name":"infra0","id":"27e6981eec74137d","state":"StateLeader","startTime":"2015-08-14T12:52:39.624477849+08:00","leaderInfo":{"leader":"27e6981eec74137d","uptime":"2m37.095030303s","startTime":"2015-08-14T12:55:31.332765166+08:00"},"recvAppendRequestCnt":429,"sendAppendRequestCnt":1064,"sendPkgRate":6.896118337343223,"sendBandwidthRate":1170.6850489473857} curlhttp://127.0.0.1:2379/v2/stats/store{"getsSuccess":13,"getsFail":2152,"setsSuccess":120,"setsFail":2,"deleteSuccess":6,"deleteFail":0,"updateSuccess":0,"updateFail":0,"createSuccess":961,"createFail":186,"compareAndSwapSuccess":19631,"compareAndSwapFail":296,"compareAndDeleteSuccess":0,"compareAndDeleteFail":0,"expireCount":849,"watchers":0}
其他接口以及更详尽的接口说明可以看官网说明
相关文章推荐
- Linux文本处理三剑客之sed
- [leetcode] 169. Majority Element
- cookie/Session/request范围与周期
- Linux-makefile命令后面的-j4 -j8是什么意思?
- redis演练(5) redis持久化
- 数据结构及算法
- hive安装
- Get技能---如何压缩图片文件大小不失真
- php获取当地时间
- UITextVewDelegate
- php取字符串的第一个字符
- MySQL、Oracle和SQL Server的分页查询语句
- Unity3D配置文件的相关操作
- 博客百度索引波动大
- jQuery实现一个全选复选框联动效果
- 补
- STM32F407存储器和总线架构
- CSS float
- 杂乱
- Undefined symbols for architecture armv7: "_OBJC_CLASS_$_CTTelephonyNetworkInfo", referenced from: