在安装了etcd 3.10版本之后,使用etcdctl和etcd交互时需要将api版本设置为3, 默认,为了向后兼容 etcdctl 使用 v2 API 来和 etcd 服务器通讯。为了让 etcdctl 使用 v3 API 来和etcd通讯,API 版本必须通过环境变量 ETCDCTL_API 设置为版本3
export ETCDCTL_API=3
查看ETCD版本方法
etcd --version #返回 etcd Version: 3.4.13 Git SHA: ae9734ed2 Go Version: go1.12.17 Go OS/Arch: linux/amd64
查看 etcd所有存储节点的集群状态
etcdctl --write-out=table member list --cert=/etc/kubernetes/pki/etcd/server.crt --key=/etc/kubernetes/pki/etcd/server.key --cacert=/etc/kubernetes/pki/etcd/ca.crt +------------------+---------+-----------------+------------------------------+------------------------------+------------+ | ID | STATUS | NAME | PEER ADDRS | CLIENT ADDRS | IS LEARNER | +------------------+---------+-----------------+------------------------------+------------------------------+------------+ | 42d99f3f857a5b15 | started | 172.168.102.119 | https://172.168.102.119:2380 | https://172.168.102.119:2379 | false | | 5e5770766d40c343 | started | 172.168.102.120 | https://172.168.102.120:2380 | https://172.168.102.120:2379 | false | | 91f48965ce76fb52 | started | 172.168.102.118 | https://172.168.102.118:2380 | https://172.168.102.118:2379 | false | +------------------+---------+-----------------+------------------------------+------------------------------+------------+查看etcd集群中的leader
etcdctl --write-out=table endpoint status --cert=/etc/kubernetes/pki/etcd/server.crt --key=/etc/kubernetes/pki/etcd/server.key --cacert=/etc/kubernetes/pki/etcd/ca.crt +----------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+ | ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS | +----------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+ | 127.0.0.1:2379 | 91f48965ce76fb52 | 3.4.7 | 361 MB | false | false | 6480 | 34152053 | 34152053 | | +----------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+ #查看etcd成员的健康状况 etcdctl --write-out=table endpoint health --cert=/etc/kubernetes/pki/etcd/server.crt --key=/etc/kubernetes/pki/etcd/server.key --cacert=/etc/kubernetes/pki/etcd/ca.crt +----------------+--------+-------------+-------+ | ENDPOINT | HEALTH | TOOK | ERROR | +----------------+--------+-------------+-------+ | 127.0.0.1:2379 | true | 28.632652ms | | +----------------+--------+-------------+-------+
查看所有etcd节点状态
export ETCDCTL_API=3 export ENDPOINTS=10.24.21.89:2379,10.24.21.90:2379,10.24.21.91:2379 etcdctl --write-out=table --endpoints=$ENDPOINTS endpoint status --cert=/etc/kubernetes/pki/etcd/server.crt --key=/etc/kubernetes/pki/etcd/server.key --cacert=/etc/kubernetes/pki/etcd/ca.crt
etcd压缩碎片整理
查看etcd数据较大的目录
etcdctl --endpoints=$ENDPOINTS --cert=/etc/kubernetes/pki/etcd/server.crt --key=/etc/kubernetes/pki/etcd/server.key --cacert=/etc/kubernetes/pki/etcd/ca.crt get /registry --keys-only --prefix|grep -v '^#' >/tmp/key.txt #etcd数据列表并排序 cat /tmp/key.txt |awk -F '[/]' '{print $2"/"$3"/"$4}'|uniq -c|sort -rn|head #获取当前KV的版本 export ETCDCTL_API=3 rev=$(etcdctl --cacert /etc/kubernetes/pki/etcd/ca.crt --cert /etc/kubernetes/pki/etcd/peer.crt --key /etc/kubernetes/pki/etcd/peer.key --endpoints=https://127.0.0.1:2379 endpoint status --write-out="json" | egrep -o '"revision":[0-9]*' | egrep -o '[0-9].*') #压缩掉所有旧版本KV,注意压缩旧版本不会释放磁盘空间到操作系统,磁盘空间仍被etcd占用 etcdctl --cacert /etc/kubernetes/pki/etcd/ca.crt --cert /etc/kubernetes/pki/etcd/peer.crt --key /etc/kubernetes/pki/etcd/peer.key --endpoints=https://127.0.0.1:2379 compact $rev 整理多余的空间,释放磁盘空间到操作系统 etcdctl --cacert /etc/kubernetes/pki/etcd/ca.crt --cert /etc/kubernetes/pki/etcd/peer.crt --key /etc/kubernetes/pki/etcd/peer.key --endpoints=$ENDPOINTS defrag --command-timeout=30s #删除指定的数据 etcdctl --endpoints=$ENDPOINTS --cacert=/etc/kubernetes/pki/etcd/ca.crt --cert=/etc/kubernetes/pki/etcd/server.crt --key=/etc/kubernetes/pki/etcd/server.key del --prefix "/registry/events" #查看etcd配置中各个配置的条目数量 ETCDCTL_API=3 etcdctl --endpoints=$ENDPOINTS --cacert=/etc/kubernetes/pki/etcd/ca.crt --cert=/etc/kubernetes/pki/etcd/server.crt --key=/etc/kubernetes/pki/etcd/server.key get / --prefix --keys-only --command-timeout=20s | awk -F/ '{print $3}' | sort -n | uniq -c
查看所有key的方法
etcdctl --endpoints=$ENDPOINTS --cert=/etc/kubernetes/pki/etcd/server.crt --key=/etc/kubernetes/pki/etcd/server.key --cacert=/etc/kubernetes/pki/etcd/ca.crt get "" --from-key