etcd的简单使用

etcd的简单使用

ETCD安装配置

安装

https://github.com/coreos/etcd/releases/下载想要的版本解压etcd包
解压后进入目录,增加x权限

chmod +x etcd
chmod +x etcdctl
 

并将etcd和etcdctl 复制到 /bin

配置启动

简单启动

./bin/etcd 这样就可以启动使用

集群配置

在两台机器上部署了简单的集群

192.168.231.130
192.168.231.132
 

在配置文件/etc/defalut/etcd 中增加:

ETCD_OPTS="-name infra0   -initial-advertise-peer-urls http://192.168.231.130:2380   -listen-peer-urls http://192.168.231.130:2380   -initial-cluster-
token etcd-cluster-1   -initial-cluster infra0=http://192.168.231.130:2380,infra1=http://192.168.231.132:2380   -initial-cluster-state new"
 

也可用参数的方法

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="-name infra1   -initial-advertise-peer-urls http://192.168.231.132:2380   -listen-peer-urls http://192.168.231.132:2380   -initial-cluster-token etcd-cluster-1   -initial-cluster infra0=http://192.168.231.130:2380,infra1=http://192.168.231.132:2380   -initial-cluster-state new"
 

参数解释

启动etcd进程后查看集群

etcdctl member list
27e6981eec74137d: name=infra0 peerURLs=http://192.168.231.130:2380 clientURLs=http://localhost:2379,http://localhost:4001
3955a9b061e52de1: name=infra1 peerURLs=http://192.168.231.132:2380 clientURLs=http://localhost:2379,http://localhost:4001
 

判断leader和followers

curl http://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 -vvv http://127.0.0.1:2379/v2/keys/mykey -XPUT -d value="this is test"
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 2379 (#0)
> PUT /v2/keys/mykey HTTP/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
>
* upload completely sent off: 18 out of 18 bytes
< HTTP/1.1 200 OK
< Content-Type: application/json
< X-Etcd-Cluster-Id: 69bc358c20384a4c
< X-Etcd-Index: 16333
< X-Raft-Index: 87030
< X-Raft-Term: 117
< Date: Fri, 14 Aug 2015 01:39:39 GMT
< Content-Length: 201
<
{"action":"set","node":{"key":"/mykey","value":"this is test","modifiedIndex":16333,"createdIndex":16333},"prevNode":{"key":"/mykey","value":"this is test","modifiedIndex":14840,"createdIndex":14840}}
* Connection #0 to host 127.0.0.1 left intact
 

http接口是rest api的风格

body里面字段详解:

  • action: set 操作对应的是url的put,rest api的风格
  • 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: 69bc358c20384a4c X-Etcd-Index: 16333 X-Raft-Index: 87030 X-Raft-Term: 117
  • X-Etcd-Index 等同于createdIndex.
  • X-Etcd-Index is the current etcd index when the watch starts, which means that the watched event may happen after X-Etcd-Index
  • X-Raft-Index 类似etcd index,但是用于raft protocol
  • X-Raft-Term is an integer that will increase whenever an etcd master election happens in the cluster. If this number is increasing rapidly, you may need to tune the election timeout. See the tuning section for details.

可以使用etcdctl简化操作

# etcdctl get mykey
this is test
 

基本操作

可以使用etcdctl或者url去执行手动操作
etcdctl几个有用的附加命令

  • –debug 将指令的url显示出来
# etcdctl --debug get mykey
Cluster-Endpoints: http://localhost:2379, http://localhost:2379, http://localhost:4001, http://localhost:4001
Curl-Example: curl -X GET http://localhost:2379/v2/keys/mykey?consistent=true&recursive=false&sorted=false
this is test
 
  • –output /-o 将输出以指定的方式显示出来
# etcdctl -o json get mykey
{"action":"get","node":{"key":"/mykey","value":"12345","modifiedIndex":18134,"createdIndex":18134},"etcdIndex":18168,"raftIndex":96532,"raftTerm":124}

# etcdctl get mykey
12345
 

查看版本

curl -L http://127.0.0.1:2379/version

etcdctl –version

# etcdctl --version
etcdctl version 2.0.13
 

设定键值

etcdctl set key value
curl -X PUT http://localhost:2379/v2/keys/key -d value=value

如想要创建一个{mykey,kkkkk}的键值

# etcdctl --debug -o json set mykey kkkkk
Curl-Example: curl -X PUT http://localhost:2379/v2/keys/mykey -d value=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 -o json get mykey
Curl-Example: curl -X GET http://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返回的是最新的值
etcdctl mk key value也能起到创建并设置键值的作用,和set操作的区别主要是,不能对已存在的key进行创建的操作

# etcdctl --debug -o json mk aa 11
Curl-Example: curl -X PUT http://localhost:2379/v2/keys/aa?prevExist=false -d value=11
{"action":"create","node":{"key":"/aa","value":"11","modifiedIndex":21093,"createdIndex":21093},"etcdIndex":21093,"raftIndex":112010,"raftTerm":149}

# etcdctl --debug -o json mk aa 22
Curl-Example: curl -X PUT http://localhost:2379/v2/keys/aa?prevExist=false -d value=22
Error:  105: Key already exists (/aa) [21098]
 

查看键值

etcdctl get key
curl -X GET http://localhost:2379/v2/keys/key?

etcdctl --debug -o json get mykey
Cluster-Endpoints: http://localhost:2379, http://localhost:2379, http://localhost:4001, http://localhost:4001
Curl-Example: curl -X GET http://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}
 

查看键值

etcdctl rm key
curl -X DELETE http://localhost:2379/v2/keys/key?

# etcdctl --debug -o json rm mykey
Cluster-Endpoints: http://localhost:2379, http://localhost:2379, http://localhost:4001, http://localhost:4001
Curl-Example: curl -X DELETE http://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 -o json get mykey
Cluster-Endpoints: http://localhost:2379, http://localhost:2379, http://localhost:4001, http://localhost:4001
Curl-Example: curl -X GET http://localhost:2379/v2/keys/mykey?consistent=true&recursive=false&sorted=false
Error:  100: Key not found (/mykey) [18766]


设置键值的TTL

etcdctl set key value –ttl time
curl -X PUT http://localhost:2379/v2/keys/key -d value=value -d ttl=time
通过设置TTL可以让key值在规定时间过期,比如设置这个key的ttl为100

 etcdctl --debug -o json set mykey ok --ttl 100
Cluster-Endpoints: http://localhost:2379, http://localhost:2379, http://localhost:4001, http://localhost:4001
Curl-Example: curl -X PUT http://localhost:2379/v2/keys/mykey -d value=ok -d ttl=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 -o json get mykey
Cluster-Endpoints: http://localhost:2379, http://localhost:2379, http://localhost:4001, http://localhost:4001
Curl-Example: curl -X GET http://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: Key not found (/mykey) [19084]
 

目录的ttl设置方法和key的类似

监控键值的改动

etcdctl watch key
curl -X GET http://localhost:2379/v2/keys/key?consistent=true&wait=true 

监听键值的改动,然后输出信息, –after-index可以根据etcd-index来获取后续index对应的改动

etcdctl --debug watch mykey
Cluster-Endpoints: http://localhost:2379, http://localhost:2379, http://localhost:4001, http://localhost:4001
Curl-Example: curl -X GET http://localhost:2379/v2/keys/mykey?consistent=true&wait=true
 

当监测到键值设置为kkkkk时打印

kkkkk
 

若etcdctl watch 带上–forever参数则会不退出一直监测键值的改动,比如这里检测到了set和delete操作

# etcdctl --debug -o json watch mykey --forever
Curl-Example: curl -X GET http://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 -X GET http://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 -X GET http://localhost:2379/v2/keys/mykey?consistent=true&wait=true&waitIndex=19662
 

对于目录类型的key可以用–recursive 来检测目录下的子keys的改动

同样可以用作监测的命令是exec-watch,相比watch,增加了监测到改动可以执行自定义的操作
etcdctl watch key command

# etcdctl exec-watch mykey -- sh -c ‘echo hit‘
hit
 

目录相关的操作

etcdctl mkdir dirname

创建一个目录可以关联多个子keys,比如先建立一个名叫mydir的dir

etcdctl --debug -o json mkdir mydir
Curl-Example: curl -X PUT http://localhost:2379/v2/keys/mydir?dir=true&prevExist=false
 

在这个dir下面可以建立多个keys

# etcdctl --debug -o json set /mydir/key1 11111
Curl-Example: curl -X PUT http://localhost:2379/v2/keys/mydir/key1 -d value=11111
{"action":"set","node":{"key":"/mydir/key1","value":"11111","modifiedIndex":20850,"createdIndex":20850},"etcdIndex":20850,"raftIndex":110723,"raftTerm":148}

# etcdctl --debug -o json set /mydir/key2 22222
Curl-Example: curl -X PUT http://localhost:2379/v2/keys/mydir/key2 -d value=22222
{"action":"set","node":{"key":"/mydir/key2","value":"22222","modifiedIndex":20855,"createdIndex":20855},"etcdIndex":20855,"raftIndex":110747,"raftTerm":148}
 

可以用etcdctl ls dirname进行查看,得到该dir下面的2个key

# etcdctl --debug -o json ls /mydir
Curl-Example: curl -X GET http://localhost:2379/v2/keys/mydir?consistent=true&recursive=false&sorted=false
/mydir/key2
/mydir/key1
 

如果向目录名的路径 post一个value,那么在这个目录下面会自动用createdIndex创建一个key

# curl -X POST http://localhost:2379/v2/keys/mydir -d value=33333
{"action":"create","node":{"key":"/mydir/21442","value":"33333","modifiedIndex":21442,"createdIndex":21442}}

# etcdctl --debug -o json ls /mydir
Curl-Example: curl -X GET http://localhost:2379/v2/keys/mydir?consistent=true&recursive=false&sorted=false
/mydir/key1
/mydir/key2
/mydir/21442
 

用rmdir 删除目录要保证目录下没有keys,不然会失败

# etcdctl --debug -o json rmdir mydir
Curl-Example: curl -X DELETE http://localhost:2379/v2/keys/mydir?dir=true&recursive=false
Error:  108: Directory not empty (/mydir) [21682]


若要将目录和keys都删除可以用rm指令

# etcdctl --debug -o json rm mydir --recursive=true
Curl-Example: curl -X DELETE http://localhost:2379/v2/keys/mydir?dir=false&recursive=true

# etcdctl --debug -o json ls /mydir
Curl-Example: curl -X GET http://localhost:2379/v2/keys/mydir?consistent=true&recursive=false&sorted=false
Error:  100: Key not found (/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 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}
 
  • 查看store的状态
curl http://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}
 

其他接口以及更详尽的接口说明可以看官网说明

时间: 2024-09-30 15:32:57

etcd的简单使用的相关文章

etcd部署简单说明

etcd是一个K/V分布式存储,每个节点都保存完成的一份数据.有点类似redis.但是etcd不是数据库. 1.先说废话.之所以会用etcd,并不是实际项目需要,而是前面自己写的上传的DBCacheServer服务部署集群的解决方案,想了解服务功能.可以阅读前一篇博文,其实我就是想给大家提供一个查询数据库的服务,能够简化我们一些功能. 最开始我打算使用zokkeeper,因为使用广泛,但是利用这一段学习时间,我在网上查阅了相关资料,采用了consul,因为开箱即用,什么都是好的,并且单文件很适合

Zookeeper和etcd比较

zookeeper: zookeeper是基于paxos的简化版zab,我觉得确实很难理解?,以前看了好多遍<从paxos到zookeper>才感觉似懂非懂了,然而过了几个月发现又一脸蒙蔽了,在这里在整理一下(仅表示我自己的理解) ZAB协议中存在着三种状态,每个节点都属于以下三种中的一种: 1. Looking :系统刚启动时或者Leader崩溃后正处于选举状态 2. Following :Follower节点所处的状态,Follower与Leader处于数据同步阶段: 3. Leading

服务发现框架选型

Zookeeper 1. 确保有所选语言的sdk,理论上github上第三方的库有一些,仔细筛选一下应该可以用. 2. 调用zookeeper接口连接zookeeper服务器. 3. 注册自身服务 4. 通过watcher获取监听服务的状态 5. 服务提供者需自行保持与zookeeper服务器的心跳. 总得来说,ZooKeeper需要胖客户端,每个客户端都需要通过其SDK与ZooKeeper服务保活,增加了编写程序的复杂性.此外,还提供api实现服务注册与发现逻辑,需要服务的消费者实现服务提供者

服务治理与RPC &middot; 跬步

以前写过Django中使用zerorpc的方法,但是由于我们的Django是运行在gevent下,而zeromq需要启动一个后台进程处理消息,与gevent使用的greenlet携程是冲突的. 在Java的世界里,Spring Cloud全家桶覆盖了微服务的方方面面,专注于服务治理的框架也有阿里的Dubbo,微博的Motan.但是Python这边没有找到合适的轮子,甚至于好的RPC框架也没有,只有gRPC,Thrift这种跨语言的RPC框架.而这些跨语言的RPC框架基本上也是基于C/C++的Py

etcd的学习心得和使用

1 etcd key-value 存储结构 "A highly-available key value store for shared configuration and service discovery." Etcd是coreos开发的分布式服务系统,内部采用raft协议作为一致性算法 raft通过选举的方式来实现一致性,在Raft中,任何一个节点都可能成为Leader Etcd是一个高可用的 Key/Value 存储系统,主要用于分享配置和服务发现. ● 简单:支持 curl

Etcd学习(一)安装和测试

Etcd是一个比较新的分布式协调框架,现在才只到0.4.6版本,还没发布1.0版本 从网上搜etcd关键字,基本上就只能看到"开源中国"的介绍: etcd 是一个高可用的 Key/Value 存储系统,主要用于分享配置和服务发现.etcd 的灵感来自于 ZooKeeper 和 Doozer,侧重于: 简单:支持 curl 方式的用户 API (HTTP+JSON) 安全:可选 SSL 客户端证书认证 快速:单实例可达每秒 1000 次写操作 可靠:使用 Raft 实现分布式 Etcd

etcd安装部署及数据同步MySQL

一.etcd说明及原理 二.etcd安装部署说明 三.etcd操作说明 四.python安装etcd 五.python-etcd使用说明 六.通过脚本获取本地的信息上传到etcd 七.通过脚本将etc的数据同步到mysql 一.etcd 简介 etcd是用于共享配置和服务发现的分布式,一致的键值存储,重点是: 简单:定义明确,面向用户的API(gRPC) 安全:使用可选的客户端证书认证的自动TLS 快速:基准测试10,000写/秒 可靠:使用Raft协议来进行合理的分布式 etcd是在Go中编写

服务发现系统etcd介绍

一.概述 etcd是一个高可用的键值存储系统,主要用于共享配置和服务发现.etcd是由CoreOS开发并维护的,灵感来自于 ZooKeeper 和 Doozer,它使用Go语言编写,并通过Raft一致性算法处理日志复制以保证强一致性.Raft是一个新的一致性算法,适用于分布式系统的日志复制,Raft通过选举的方式来实现一致性,在Raft中,任何一个节点都可能成为Leader.Google的容器集群管理系统Kubernetes.开源PaaS平台Cloud Foundry和CoreOS的Fleet都

[转载] etcd全方位解读

原文: http://www.infoq.com/cn/articles/etcd-interpretation-application-scenario-implement-principle etcd使用了更易懂的raft数据一致性协议, 比paxos容易理解的多. 本文对etcd的使用场景和原理做了非常详细的描写, 是学习etcd不可多得的好材料. etcd:从应用场景到实现原理的全方位解读 作者 孙健波 发布于 2015年1月30日 | 讨论 分享到:微博微信FacebookTwitte