etcd 入门
etcd 是一个开源的、分布式的键值对数据存储系统。
etcd应用场景包括服务发现、配置中心(共享配置),以及分布式锁等。
etcd 基于 go 语言实现,内部采用raft协议作为一致性算法。
类似项目有zookeeper 和 consul。
etcd数据模型是一个持久的、多版本并发控制模型。
对于旧的数据,它会压缩存储区,丢弃最旧的版本。
在应用方面,Google 的容器集群管理系统 Kubernetes、开源 PaaS 平台 Cloud Foundry 和 CoreOS 的 Fleet 都广泛使用了 etcd。
etcd有以下特性:
简单:安装配置简单,而且提供了 HTTP API 进行交互,使用简单;
安全:可选的 SSL 客户端证书认证;
快速:根据官方提供的 benchmark 数据,单实例支持每秒 2k+ 读操作;
可靠:采用 raft 算法,实现分布式系统数据的可用性和一致性。
1.基本功能
(1)键值写入和读取
(2)过期时间
(3)观察者
(4)租约
过期时间
的封装。
(5)集群管理的相关操作
(6)维护操作
(7)用户权限和管理
2.安装与配置
下载
下载地址
github etcd
这里下载的是etcd-v3.4.13-linux-amd64.tar.gz
解压缩
tar xzvf etcd-v3.4.13-linux-amd64.tar.gz -C /home/lanyang/workspace
$ cd /home/lanyang/workspace/etcd-v3.4.13-linux-amd64/
$ ls
Documentation etcd etcdctl README-etcdctl.md README.md READMEv2-etcdctl.md
其中,etcd
是etcd服务程序,etcdctl
是命令行工具。其他是文档。
启动etcd服务
[[email protected] etcd-v3.4.13-linux-amd64]$ ./etcd
[WARNING] Deprecated '--logger=capnslog' flag is set; use '--logger=zap' flag instead
2020-10-09 22:54:26.398504 I | etcdmain: etcd Version: 3.4.13
2020-10-09 22:54:26.398533 I | etcdmain: Git SHA: ae9734ed2
2020-10-09 22:54:26.398540 I | etcdmain: Go Version: go1.12.17
2020-10-09 22:54:26.398543 I | etcdmain: Go OS/Arch: linux/amd64
2020-10-09 22:54:26.398546 I | etcdmain: setting maximum number of CPUs to 1, total number of available CPUs is 1
2020-10-09 22:54:26.398553 W | etcdmain: no data-dir provided, using default data-dir ./default.etcd
[WARNING] Deprecated '--logger=capnslog' flag is set; use '--logger=zap' flag instead
2020-10-09 22:54:26.400260 I | embed: name = default
2020-10-09 22:54:26.400270 I | embed: data dir = default.etcd
2020-10-09 22:54:26.400274 I | embed: member dir = default.etcd/member
2020-10-09 22:54:26.400277 I | embed: heartbeat = 100ms
2020-10-09 22:54:26.400281 I | embed: election = 1000ms
2020-10-09 22:54:26.400284 I | embed: snapshot count = 100000
2020-10-09 22:54:26.400295 I | embed: advertise client URLs = http://localhost:2379
2020-10-09 22:54:26.424113 I | etcdserver: starting member 8e9e05c52164694d in cluster cdf818194e3a8c32
raft2020/10/09 22:54:26 INFO: 8e9e05c52164694d switched to configuration voters=()
raft2020/10/09 22:54:26 INFO: 8e9e05c52164694d became follower at term 0
raft2020/10/09 22:54:26 INFO: newRaft 8e9e05c52164694d [peers: [], term: 0, commit: 0, applied: 0, lastindex: 0, lastterm: 0]
raft2020/10/09 22:54:26 INFO: 8e9e05c52164694d became follower at term 1
raft2020/10/09 22:54:26 INFO: 8e9e05c52164694d switched to configuration voters=(10276657743932975437)
2020-10-09 22:54:26.428876 W | auth: simple token is not cryptographically signed
2020-10-09 22:54:26.434151 I | etcdserver: starting server... [version: 3.4.13, cluster version: to_be_decided]
2020-10-09 22:54:26.435951 I | etcdserver: 8e9e05c52164694d as single-node; fast-forwarding 9 ticks (election ticks 10)
2020-10-09 22:54:26.436399 I | embed: listening for peers on 127.0.0.1:2380
raft2020/10/09 22:54:26 INFO: 8e9e05c52164694d switched to configuration voters=(10276657743932975437)
2020-10-09 22:54:26.436628 I | etcdserver/membership: added member 8e9e05c52164694d [http://localhost:2380] to cluster cdf818194e3a8c32
raft2020/10/09 22:54:27 INFO: 8e9e05c52164694d is starting a new election at term 1
raft2020/10/09 22:54:27 INFO: 8e9e05c52164694d became candidate at term 2
raft2020/10/09 22:54:27 INFO: 8e9e05c52164694d received MsgVoteResp from 8e9e05c52164694d at term 2
raft2020/10/09 22:54:27 INFO: 8e9e05c52164694d became leader at term 2
raft2020/10/09 22:54:27 INFO: raft.node: 8e9e05c52164694d elected leader 8e9e05c52164694d at term 2
2020-10-09 22:54:27.425262 I | etcdserver: setting up the initial cluster version to 3.4
2020-10-09 22:54:27.425324 I | etcdserver: published {Name:default ClientURLs:[http://localhost:2379]} to cluster cdf818194e3a8c32
2020-10-09 22:54:27.425794 I | embed: ready to serve client requests
2020-10-09 22:54:27.426584 N | embed: serving insecure client requests on 127.0.0.1:2379, this is strongly discouraged!
2020-10-09 22:54:27.427182 N | etcdserver/membership: set the initial cluster version to 3.4
2020-10-09 22:54:27.427227 I | etcdserver/api: enabled capabilities for version 3.4
此时启动的一个节点的etcd服务。
从输出日志中,可以看到以下几点重要信息:
name
表示节点名称,默认为default
。
data-dir
保存日志和快照的目录,默认为当前工作目录default.etcd/目录下。
[[email protected] etcd-v3.4.13-linux-amd64]$ ll default.etcd/member/
total 8
drwx------. 2 lanyang lanyang 4096 Oct 9 22:54 snap
drwx------. 2 lanyang lanyang 4096 Oct 9 22:54 wal
heartbeat
表示 leader发送心跳给follower的周期,默认100ms。
election
leader选举超时时间,默认1000ms。
snapshot count
,表示raft entries多少时触发snpshot操作,默认值 1000
etcd目前默认使用2379端口提供HTTP API服务。
使用2380端口和peer通信。
也可以根据需要,使用yum进行安装。
3.使用
查看版本号
$ ./etcdctl version
etcdctl version: 3.4.13
API version: 3.4
查看集群成员信息
$ ./etcdctl member list
8e9e05c52164694d, started, default, http://localhost:2380, http://localhost:2379, false
查看集群健康状态
./etcdctl endpoint health --endpoints=127.0.0.1:2379
127.0.0.1:2379 is healthy: successfully committed proposal: took = 1.341607ms
键值对写入和读取
$ ./etcdctl put /msg hello
OK
$ ./etcdctl get /msg
/msg
hello
$ ./etcdctl put /msg2 hello2
OK
$ ./etcdctl get /msg2
/msg2
hello2
前缀匹配查找
$ ./etcdctl get /msg --prefix
/msg
hello
/msg2
hello2
删除
$ ./etcdctl del /msg
1
$ ./etcdctl get /msg
$ ./etcdctl get /msg
租约
新建租约
$ ./etcdctl lease grant 120
lease 694d750ddb227814 granted with TTL(120s)
查看租约
$ ./etcdctl lease list
found 1 leases
694d750ddb227814
使用租约设置key
$ ./etcdctl put /msg3 orange --lease="694d750ddb227814"
OK
$ ./etcdctl get /msg3
/msg3
orange
$ ./etcdctl get /msg3
/msg3
orange
等120s后,再次查看key,发现key已过期,被自动删除:
./etcdctl get /msg3
./etcdctl get /msg3
观察者watch
在终端1下
$ ./etcdctl watch /msg4
此时,终端1进入等待状态
在终端2下,写入
$ ./etcdctl put /msg4 oragne
OK
接着终端1下输出
PUT
/msg4
oragne
对于etcdctl
的使用方法,可通过etcdctl help
查看更多使用方式。
关于使用HTTP API交互方式,简单举例。
获取版本
curl http://127.0.0.1:2379/version
{"etcdserver":"3.4.13","etcdcluster":"3.4.0"}
4.参考
上一篇: extjs Date的使用
下一篇: Redmine安装与入门指南