etcd常用命令记录

 

1.查看etcd的版本

[root@etcd01 ssl]# curl -L http://127.0.0.1:2379/version
{"etcdserver":"3.3.10","etcdcluster":"3.3.0"}
 

2.查看etcd暴露出来的prometheus指标;在prometheus对其监控时使用

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

注:prometheus采集指标时,是通过https方式采集的
 

3.查看etcd集群的成员

[root@etcd01 ssl]# etcdctl --ca-file=ca.pem --cert-file=etcd.pem --key-file=etcd-key.pem member list
a03dc3532588a426: name=etcd2 peerURLs=https://10.10.5.183:2380 clientURLs=https://10.10.5.183:2379 isLeader=true
e5760d44b75c8a8f: name=etcd1 peerURLs=https://10.10.5.169:2380 clientURLs=https://10.10.5.169:2379 isLeader=false
f6569926ef77ab79: name=etcd3 peerURLs=https://10.10.5.184:2380 clientURLs=https://10.10.5.184:2379 isLeader=false 注:member命令同时支持添加、删除一个节点 peerURLs:表示此节点对其它的节点开放的通信地址
clientURLs:表示向客户端提供的通信地址
isLeader:表示当前节点是否是leader
 

4.查看etcd集群的健康状态

[root@etcd01 ssl]# etcdctl --ca-file=ca.pem --cert-file=etcd.pem --key-file=etcd-key.pem cluster-health
member a03dc3532588a426 is healthy: got healthy result from https://10.10.5.183:2379
member e5760d44b75c8a8f is healthy: got healthy result from https://10.10.5.169:2379
member f6569926ef77ab79 is healthy: got healthy result from https://10.10.5.184:2379
 

5.备份etcd的整个数据目录;默认的数据目录为/var/lib/etcd/

etcdctl --ca-file=ca.pem --cert-file=etcd.pem --key-file=etcd-key.pem backup --data-dir /var/lib/etcd --backup-dir /tmp/etcd

--data-dir:指明数据目录的位置
--backup-dir:指明备份的位置
 

6.对etcd进行快照备份

[root@etcd01 k8s]# ETCDCTL_API=3 /etc/kubernetes/bin/etcdctl snapshot save snapshot.db
Snapshot saved at snapshot.db ETCDCTL_API=3:表示使用etcd的v3版本的API接口
注:一定要添加ETCDCTL_API=3才能正常备份;如果不添加将无法备份
 

7.通过快照恢复etcd集群;(每个节点都要执行)

ETCDCTL_API=3 etcdctl snapshot restore snapshot.db \
--name {{ NODE_NAME }} \
--initial-cluster {{ ETCD_NODES }} \
--initial-cluster-token etcd-cluster-0 \
--initial-advertise-peer-urls https://{{ inventory_hostname }}:2380 --name:表示当前etcd节点的名字(非主机名)
--initial-cluster:集群中所有节点的peer访问地址;例:etcd01=https://10.10.5.217:2380 ,etcd02=https://10.10.5.219:2380,etcd03=https://10.10.5.220:2380
--initial-cluster-token:集群中各节点通信的token
--initial-advertise-peer-urls:当前节点对其它节点的通信地址
04-08 12:36