etcd是一个开源的分布式一致性键值数据库,其基于Raft一致性算法,用于数据存储、服务发现和调度协调。

安装

首先我们下载程序包

useradd etcdsu - etcdwget https://github.com/etcd-io/etcd/releases/download/v3.5.4/etcd-v3.5.4-linux-amd64.tar.gztar -zxvf etcd-v3.5.4-linux-amd64.tar.gzcd etcd-v3.5.4-linux-amd64

之后我们可以查询etcd的版本

$ ./etcd --versionetcd Version: 3.5.4Git SHA: 08407ff76Go Version: go1.16.15Go OS/Arch: linux/amd64

我们在三台负载172.19.34.15172.19.34.56172.19.34.82上分别执行如上操作来安装etcd。

搭建集群

我们使用etcd来构建一个集群,服务启动的相关配置既可以使用命令行设置也可以使用配置文件设置。相关配置的含义如下

配置含义默认值
name当前节点的名称default
initial-advertise-peer-urls告知其它节点,当前节点与其它节点交流的ip地址和端口http://localhost:2380
advertise-client-urls告知其它节点,当前节点与客户端通信的ip地址和端口http://localhost:2379
listen-peer-urls当前节点所监听的,用于与其它节点交流的ip地址和端口http://localhost:2380
listen-client-urls当前节点所监听的,用于与客户端通信的ip地址和端口http://localhost:2379
initial-cluster-token集群的token属性,相当于集群的名称etcd-cluster
initial-cluster集群的节点信息,用于与其它的节点进行网络连接default=http://localhost:2380
initial-cluster-state第一次启动集群设为new,加入已有集群设为existingnew

在三台设备上面设置好一些启动所需要的属性

# 集群的名称TOKEN=my-etcd-cluster# 所有的节点网络连接信息CLUSTER=node1=http://172.19.34.15:2380,node2=http://172.19.34.56:2380,node3=http://172.19.34.82:2380# 只在172.19.34.15上设置NAME=node1IP=172.19.34.15# 只在172.19.34.56上设置NAME=node2IP=172.19.34.56# 只在172.19.34.82上设置NAME=node3IP=172.19.34.82

之后我们使用命令行设置在三个节点上分别启动三个进程

$ ./etcd --name ${NAME} \    --initial-advertise-peer-urls http://${IP}:2380 \    --advertise-client-urls http://${IP}:2379 \    --listen-peer-urls http://${IP}:2380 \    --listen-client-urls http://${IP}:2379,http://127.0.0.1:2379 \    --initial-cluster-token ${TOKEN} \    --initial-cluster ${CLUSTER}

在三台负载都执行了etcd启动命令之后,正常情况下三个节点已经构成了一个etcd集群。集群搭建好了之后,我们可以使用etcdctl客户端获取集群中的节点信息

$ ENDPOINTS=172.19.34.15:2379,172.19.34.56:2379,172.19.34.82:2379$ ./etcdctl --endpoints=$ENDPOINTS member list3524bbececbcecf7, started, node2, http://172.19.34.56:2380, http://172.19.34.56:2379, falseb1d70dbaf73a7a68, started, node1, http://172.19.34.15:2380, http://172.19.34.15:2379, falsee4391b8282b4faf7, started, node3, http://172.19.34.82:2380, http://172.19.34.82:2379, false

操作集群

键值对的写入和读取操作

./etcdctl --endpoints=$ENDPOINTS put name "zhangsan"./etcdctl --endpoints=$ENDPOINTS get name./etcdctl --endpoints=$ENDPOINTS --write-out="json" get name./etcdctl --endpoints=$ENDPOINTS put name1 "lisi"./etcdctl --endpoints=$ENDPOINTS get name --prefix./etcdctl --endpoints=$ENDPOINTS del name

锁操作

# 在两个终端执行该命令,只有一个会成功,另一个会等待直到第一个锁释放./etcdctl --endpoints=$ENDPOINTS lock mutex1

监控key

# 在一个终端对指定key进行监控./etcdctl --endpoints=$ENDPOINTS watch stock1# 在另一个终端操作这个key,第一个终端的监控程序会收到操作数据./etcdctl --endpoints=$ENDPOINTS put stock1 1000

租约

# 创建一个租约,时长30秒./etcdctl --endpoints=$ENDPOINTS lease grant 30# lease 7af781dc5d746b11 granted with TTL(30s)./etcdctl --endpoints=$ENDPOINTS put name zhangsan --lease=7af781dc5d746b11# 30秒后数据就没有了./etcdctl --endpoints=$ENDPOINTS get name

查看集群状态

./etcdctl --write-out=table --endpoints=$ENDPOINTS endpoint status+-------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+|     ENDPOINT      |        ID        | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |+-------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+| 172.19.34.15:2379 | b1d70dbaf73a7a68 |   3.5.4 |   20 kB |      true |      false |         5 |         39 |                 39 |        || 172.19.34.56:2379 | 3524bbececbcecf7 |   3.5.4 |   20 kB |     false |      false |         5 |         39 |                 39 |        || 172.19.34.82:2379 | e4391b8282b4faf7 |   3.5.4 |   20 kB |     false |      false |         5 |         39 |                 39 |        |+-------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+

./etcdctl --endpoints=$ENDPOINTS endpoint health172.19.34.15:2379 is healthy: successfully committed proposal: took = 20.180745ms172.19.34.82:2379 is healthy: successfully committed proposal: took = 21.915962ms172.19.34.56:2379 is healthy: successfully committed proposal: took = 24.358102ms

参考

https://etcd.io/docs/v3.5/tutorials/
https://www.infoq.cn/article/zqzelyy57xgvb6ecxcfb