Etcd常用操作

286 阅读1分钟

描述

etcd是一个分布式键值存储,它提供了一种可靠的方式来存储跨机器集群的数据。它是开源的,可以在GitHub上找到。etcd优雅地处理网络分区期间的领导选举,并容忍包括领导者在内的机器故障。

您的应用程序可以读取和写入数据到etcd。一个简单的用例是将数据库连接详细信息或功能标志作为关键值对存储在etcd中。可以监视这些值,允许您的应用在更改时自行重新配置。

高级应用程序利用一致性保证来实现数据库领导者选举或对一群工作人员进行分布式锁定。

打印成员列表

./etcdctl --write-out=table --endpoints=localhost:2379 member list
+------------------+---------+--------+----------------------------+----------------------------+
|        ID        | STATUS  |  NAME  |         PEER ADDRS         |        CLIENT ADDRS        |
+------------------+---------+--------+----------------------------+----------------------------+
| ba78e54b36d36c50 | started | infra1 | http://192.168.21.151:2380 | http://192.168.21.151:2379 |
| c6a63eef7452c8b1 | started | infra0 | http://192.168.21.149:2380 | http://192.168.21.149:2379 |
| efd972cb002e70bc | started | infra2 | http://192.168.21.153:2380 | http://192.168.21.153:2379 |
+------------------+---------+--------+----------------------------+----------------------------+

更新peerURLs

./etcdctl member update <memberID> <peerURLs>
./etcdctl member update efd972cb002e70bc http://127.0.0.1:2380

删除成员

$ etcdctl member remove a8266ecf031671f3
Removed member a8266ecf031671f3 from cluster

添加成员(扩容)

添加成员是一个两步过程:

1、通过成员API或etcdctl member add命令将新成员添加到集群。

说明:全部操作使用etcd V3

etcdctl member add infra3 --peer-urls=http://192.168.21.202:2380
  
export ETCD_NAME="infra3"
export ETCD_INITIAL_CLUSTER="infra3=http://192.168.21.202:2380,infra1=http://192.168.21.151:2380,infra0=http://192.168.21.149:2380,infra2=http://192.168.21.153:2380"
export ETCD_INITIAL_CLUSTER_STATE="existing"

2、使用新群集配置启动新成员,包括已更新成员(现有成员+新成员)的列表。

etcdctl已通知集群有关新成员的信息,并打印出成功启动它所需的环境变量。现在使用新成员的相关标志启动新的etcd进程:

export ETCD_NAME="infra3"
export ETCD_INITIAL_CLUSTER="infra0=http://10.0.1.10:2380,infra1=http://10.0.1.11:2380,infra2=http://10.0.1.12:2380,infra3=http://10.0.1.13:2380"
export ETCD_INITIAL_CLUSTER_STATE=existing

./etcd  --initial-advertise-peer-urls http://192.168.21.202:2380 --listen-peer-urls http://192.168.21.202:2380 --listen-client-urls http://192.168.21.202:2379,http://127.0.0.1:2379 --advertise-client-urls http://192.168.21.202:2379 --initial-cluster-token etcd-cluster-1  --data-dir infra3

3、查看状态

root@zyzx-node1:[/root/etcd-v3.2.16-linux-amd64]./etcdctl member list  --endpoints=192.168.21.149:2379,192.168.21.151:2379,192.168.21.153:2379,192.168.21.202:2379 
75499131c19cbbbd, started, infra3, http://192.168.21.202:2380, http://192.168.21.202:2379
82aa79e98a2686d6, started, infra2, http://192.168.21.153:2380, http://192.168.21.153:2379
ba78e54b36d36c50, started, infra1, http://192.168.21.151:2380, http://192.168.21.151:2379
c6a63eef7452c8b1, started, infra0, http://192.168.21.149:2380, http://192.168.21.149:2379

root@zyzx-node1:[/root/etcd-v3.2.16-linux-amd64] ./etcdctl endpoint status --endpoints=192.168.21.149:2379,192.168.21.151:2379,192.168.21.153:2379,192.168.21.202:2379
192.168.21.149:2379, c6a63eef7452c8b1, 3.2.16, 2.6 MB, true, 4, 27392
192.168.21.151:2379, ba78e54b36d36c50, 3.2.16, 2.6 MB, false, 4, 27392
192.168.21.153:2379, 82aa79e98a2686d6, 3.2.16, 2.6 MB, false, 4, 27392
192.168.21.202:2379, 75499131c19cbbbd, 3.2.16, 3.2 MB, false, 4, 27392

root@zyzx-node1:[/root/etcd-v3.2.16-linux-amd64]./etcdctl endpoint health --endpoints=192.168.21.149:2379,192.168.21.151:2379,192.168.21.153:2379,192.168.21.202:2379
192.168.21.149:2379 is healthy: successfully committed proposal: took = 1.302649ms
192.168.21.151:2379 is healthy: successfully committed proposal: took = 1.457264ms
192.168.21.153:2379 is healthy: successfully committed proposal: took = 1.48595ms
192.168.21.202:2379 is healthy: successfully committed proposal: took = 1.453579ms

4、数据测试

#在一台节点上增加一条数据,数据是否会同步到其他节点

root@zyzx-node1:[/root/etcd-v3.2.16-linux-amd64]./etcdctl  put hello world
OK
root@zyzx-node1:[/root/etcd-v3.2.16-linux-amd64]./etcdctl  get hello
hello
world

root@zyzx-node2:[/root/etcd-v3.2.16-linux-amd64]./etcdctl get hello
hello
world

root@LY1F-R020510-VM12:[/root/etcd-v3.2.16-linux-amd64]./etcdctl get hello
hello
world

备份与恢复

1、备份操作

#保存快照
./etcdctl  --endpoints=$ENDPOINTS  snapshot save snapshot-test.db

2、恢复操作

./etcdctl snapshot restore snapshot.db --name m3 --data-dir=/home/etcd_data
恢复后的文件需要修改权限为 etcd:etcd
--name:重新指定一个数据目录,可以不指定,默认为 default.etcd
--data-dir:指定数据目录
建议使用时不指定 name 但指定 data-dir,并将 data-dir 对应于 etcd 服务中配置的 data-dir

etcd配置参数

skyao.gitbooks.io/learning-et…