etcdctl常用命令

etcdctl -h

1VERSION:
2	3.4.27
3
4API VERSION:
5	3.4

集群

查看集群状态

1export ENDPOINTS="192.168.5.41:2379,192.168.5.45:2379,192.168.5.46:2379"
2etcdctl --write-out=table --endpoints=$ENDPOINTS endpoint status

查看集群成员

1etcdctl --write-out=table --endpoints=$ENDPOINTS member list

删除成员

1MEMBER_ID=fa6333c794b010d8
2etcdctl --endpoints=$ENDPOINTS member remove ${MEMBER_ID}

添加成员(添加已经删除的需要将新节点下面data.etcd必须删除)

1export HOST_1=192.168.5.41
2export HOST_2=192.168.5.45
3export HOST_3=192.168.5.46
4export NAME_1=etcd1
5export NAME_2=etcd2
6export NAME_3=etcd3
7etcdctl --endpoints=${HOST_1}:2379,${HOST_2}:2379 member add ${NAME_3} --peer-urls=http://${HOST_3}:2380
8export TOKEN=etcd-cluster-3
9export ADVERTISE_PRRE_URLS=https:$HOST_3:2380

在新加节点上面,执行命令:

 1export NAME=etcd3
 2export CLINE_URLS="https://192.168.5.46:2379,https://192.168.5.46:4001"
 3export CLUSTER=$NAME_1="https:$HOST_1:2380,https://$HOST_1:4001",$NAME_2="https:$HOST_2:2380,https://$HOST_2:4001"
 4export ADVERTISE_PRRE_URLS=https:$HOST_3:2380
 5
 6#/usr/local/bin/etcd 
 7--peer-client-cert-auth 
 8--client-cert-auth 
 9--data-dir=/var/lib/rancher/etcd/ 
10--advertise-client-urls=$CLINE_URLS
11--key-file=/etc/kubernetes/ssl/kube-etcd-192-168-5-46-key.pem
12--peer-cert-file=/etc/kubernetes/ssl/kube-etcd-192-168-5-46.pem
13--peer-key-file=/etc/kubernetes/ssl/kube-etcd-192-168-5-46-key.pem
14--election-timeout=5000 
15--name=$NAME 
16--listen-peer-urls=https://0.0.0.0:2380 
17--initial-cluster=$CLUSTER 
18--initial-cluster-state=existing 
19--initial-cluster-token=$TOKEN
20--listen-client-urls=https://0.0.0.0:2379
21--heartbeat-interval=500 
22--initial-advertise-peer-urls=$ADVERTISE_PRRE_URLS
23--trusted-ca-file=/etc/kubernetes/ssl/kube-ca.pem
24--peer-trusted-ca-file=/etc/kubernetes/ssl/kube-ca.pem
25--cert-file=/etc/kubernetes/ssl/kube-etcd-192-168-5-46.pem

put操作

1etcdctl --endpoints=$ENDPOINTS put testkey "Hello World"

get操作

得到所有的key(这里是前缀匹配)

1etcdctl --endpoints=$ENDPOINTS  --prefix --keys-only=true get / 

得到对应key的值

1etcdctl --endpoints=$ENDPOINTS  get /testkey
2/testkey
3hello world

del操作

1etcdctl --endpoints=$ENDPOINTS del /testkey
2etcdctl --endpoints=$ENDPOINTS get /testkey

watch 操作

1etcdctl --endpoints=$ENDPOINTS watch /testkey
2PUT
3/testkey
41234
5etcdctl --endpoints=$ENDPOINTS put /testkey "1234"

defrag(磁盘碎片整理)

1etcdctl defrag --data-dir default.etcd
2etcdctl --endpoints=$ENDPOINTS defrag 

snapshot

保存备份当前集群etcd的信息,用于新的etcd集群

 1# save
 2etcdctl snapshot save snapshot.db
 3ls
 4# restore
 5etcdctl snapshot save snapshot.db
 6
 7# restore members
 8bin/etcdctl snapshot restore snapshot.db --initial-cluster-token etcd-cluster-1 --initial-advertise-peer-urls http://127.0.0.1:12380  --name sshot1 --initial-cluster 'sshot1=http://127.0.0.1:12380,sshot2=http://127.0.0.1:22380,sshot3=http://127.0.0.1:32380'
 9bin/etcdctl snapshot restore snapshot.db --initial-cluster-token etcd-cluster-1 --initial-advertise-peer-urls http://127.0.0.1:22380  --name sshot2 --initial-cluster 'sshot1=http://127.0.0.1:12380,sshot2=http://127.0.0.1:22380,sshot3=http://127.0.0.1:32380'
10bin/etcdctl snapshot restore snapshot.db --initial-cluster-token etcd-cluster-1 --initial-advertise-peer-urls http://127.0.0.1:32380  --name sshot3 --initial-cluster 'sshot1=http://127.0.0.1:12380,sshot2=http://127.0.0.1:22380,sshot3=http://127.0.0.1:32380'
11
12# launch members
13bin/etcd --name sshot1 --listen-client-urls http://127.0.0.1:2379 --advertise-client-urls http://127.0.0.1:2379 --listen-peer-urls http://127.0.0.1:12380 &
14bin/etcd --name sshot2 --listen-client-urls http://127.0.0.1:22379 --advertise-client-urls http://127.0.0.1:22379 --listen-peer-urls http://127.0.0.1:22380 &
15bin/etcd --name sshot3 --listen-client-urls http://127.0.0.1:32379 --advertise-client-urls http://127.0.0.1:32379 --listen-peer-urls http://127.0.0.1:32380 &
16# status
17etcdctl snapshot status snapshot.db
18etcdctl snapshot status snapshot.db  -w table

查看当前endpoint的status

1etcdctl --endpoints=$ENDPOINTS endpoint --cluster=true status  -w table

move-leader

1etcdctl --endpoints 192.168.5.45:2379  move-leader d6414a7c7c550d29

访问控制相关

 1# 添加root用户
 2etcdctl user add root
 3# 添加非root用户
 4etcdctl user add wang
 5# 添加role
 6etcdctl role add role1
 7# 将用户wang加入角色role1中
 8etcdctl user grant-role wang role1
 9# 给角色role1复制权限
10etcdctl role grant-permission role1 read /testkey
11# 开启用户认证
12etcdctl --endpoints=$ENDPOINTS auth enable
13# 验证权限(可以读,但是不能写)
14etcdctl  put /testkey "1111"  --user="wang:111111" 
15Error: etcdserver: permission denied
16etcdctl  get /testkey  --user="wang:111111" 
17/testkey
18111111

ui界面

1docker run --rm --name=etcdv3-browser -p 9980:80 joinsunsoft/etcdv3-browser:1.0.0
2
3http://localhost:9980/main.html#
4Username: ginghan Password: 123456

发布日期:2023-09-05 20:26 字数:325 用时 2分钟
tags:etcd etcdctl