关键词:
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}
其他接口以及更详尽的接口说明可以看官网说明
etcd-etcd快速入门及promql查询etcd指标
...式键值数据库,根据官网介绍,总结来说有如下的特点:简单:etcd的安装简单,且为用户提供了HTTPAPI,用户使 查看详情
浅入深出etcd之集群部署与golang客户端使用(代码片段)
...我们实际使用来说,其实配置并不复杂,下面举例一种最简单的集群配置。(简单到你想不到~)下载https://github.com/etc 查看详情
2etcd单机部署和集群部署(代码片段)
上一章我们认识了etcd,简单的介绍了etcd的基础概念,但是理解起来还是比较抽象的。这一章我们就一起来部署下etcd。这样可以让我们对etcd有更加确切的认识。1、etcd单实例部署对于平常的学习,其实搭建一个单机节点是够了的... 查看详情
k8s实战kubernetes错误排查之etcd篇(代码片段)
...,如何快速排查处理。知识充电:什么是etcd?它是一个简单的数据库,k8s默认使用它存储集群的配置信息。为什么不选择mysql等其他更好的数据库服务?因为etcd有四大特点:简单:安装配置简单,而且提供了HTTPAPI进行 查看详情
简单实用kubernetes的etcd备份与恢复实现恢复集群配置(代码片段)
学习目标内容提示:由于牵涉概念过多,本章主要讲解具体的备份恢复,其他概述官网:https://kubernetes.io/zh-cn/docs/tasks/administer-cluster/configure-upgrade-etcd/#backing-up-an-etcd-cluster一.etcd的工作原理可将其分成两层次:Http层请求、接收消... 查看详情
etcd-cpp-apiv3使用示例介绍(代码片段)
...方式、订阅/通知方案过程中遇到很多问题,这里做下简单记录。本系列总共3篇:《搭建etcd集群》:介绍搭建etcd集群方式、遇到的问题及处理方式;《编译安装etcd-cpp-apiv3》:介绍etcd-cpp-api 查看详情
etcd原理(代码片段)
...了拥有与之类似的功能外,更专注于以下四点:简单:基于HTTP& 查看详情
golang对etcd的简单操作
参考技术A首先获取clientv3:连接etcd:kv是一个用于操作kv的连接,其实它本质上是用了client的conn,为了更加专注于键值对的操作,关闭client后也会使kv无法用。(kv的操作client也能实现)设置一个超时的context:context.WithTimeout()会返... 查看详情
etcd-cpp-apiv3使用示例介绍(代码片段)
...方式、订阅/通知方案过程中遇到很多问题,这里做下简单记录。本系列总共3篇:《搭建etcd集群》:介绍搭建etcd集群方式、遇到的问题及处理方式;《编译安装etcd-cpp-apiv3》:介绍etcd-cpp-apiv3编译安装方式、常... 查看详情
etcd介绍经典适用场景安装配置测试
...项目,除了拥有与之类似的功能外,更专注于以下四点。简单:基于HTTP+JSON的API让你用curl就可以轻松使用。安全:可选SSL客户认证机制。快速:每个实例每秒支持一千次写操作。可信:使用Raft算法充分实现了分布式。 查看详情
搭建etcd集群(代码片段)
...方式、订阅/通知方案过程中遇到很多问题,这里做下简单记录。本系列总共3篇:《搭建etcd集群》:介绍搭建etcd集群方式、遇到的问题及处理方式;《编译安装etcd-cpp-apiv3》:介绍etcd-cpp-api 查看详情
云原生第三周--kubernetes组件详解(代码片段)
...网络问题一致性:每次读取都会返回跨多主机的最新写入简单:包括一个定义良好、面向用户的API(gRPC)安全:实现了带有可选的客户端证书身份验证的自动化TLS快速:每秒10000次写入的基准速度可靠:使用Raft算法实现了存储... 查看详情
搭建etcd集群(代码片段)
...方式、订阅/通知方案过程中遇到很多问题,这里做下简单记录。本系列总共3篇:《搭建etcd集群》:介绍搭建etcd集群方式、遇到的问题及处理方式;《编译安装etcd-cpp-apiv3》:介绍etcd-cpp-apiv3编译安装方式、常... 查看详情
etcd——基础原理
...y。etcd使用raft协议来维护集群内各个节点状态的一致性。简单说,etcd集群是一个分布式系统,由多个节点相互通信构成整体对外服务,每个节点都存储了完整的数据,并且通过Raft协议保证每个节点维护的数据是一致的。每个etcd... 查看详情
etcd简要使用
...什么要用B+树,因为要支持范围查找。本文总结etcd的简要使用,下篇再对底层原理进行解析。 查看详情
goetcd
...lue存储,可以用于配置共享和服务发现。具有以下优点:简单:相比于晦涩难懂的paxos算法,etcd基于相对简单且易实现的raft算法实现一致性,并通过gRPC提供接口调用安全:支持TLS通信,并可以针对不同的用户进 查看详情
服务发现框架选型
...一个采用http协议的分布式键值对存储系统,因其易用,简单。很多系统都采用或支持etcd作为服务发现的一部分,比如kubernetes。但正事因为其只是一个存储系统,如果想要提供完整的服务发现功能,必须搭配一些第三方的工具。... 查看详情
技术分享|基于etcd的分布式锁实现原理及方案(代码片段)
...eeper,如Etcd采用的Raft协议就要比ZooKeeper采用的Zab协议简单、易理解。Etcd作为CoreOS开源项目,有以下的特点。简单:使用Go语言编写, 查看详情