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 用时
标签云
alpine(1) api网关(1) async(1) await(1) centos(4) cli(1) client-go(1) debug(1) docker(10) docker-compose(1) dockerfile(1) embed(1) es6(1) etcd(1) etcdctl(1) freemesh(2) git(6) go(5) go-zeus(5) goland(2) golang(7) grafana(2) grpc(5) grpcurl(1) homebrew(1) homedir(1) http(2) hugo(1) java(2) javascript(3) job(1) js(1) json(1) k8s(16) k8s部署指南(1) kubebuilder(1) kubectl(4) kubernetes(1) linux(2) localecompare(1) lombok(1) metrics(2) mock(1) mysql(1) npm(1) nsenter(1) nvm(1) pnpm(2) pprof(1) prettier(1) prometheus(2) protobuf3(1) protoc(1) qiankun(1) request(1) scss(1) servicemesh(1) spring(1) springcloud->freemesh(1) ssh(2) systemctl(1) tcpdump(1) typescript(2) vite(2) vscode(2) vue(8) vue-admin(2) vue3(1) webssh(1) xtermjs(1) xxl-job(1) yarn(1) zookeeper(1) 云原生(4) 云原生框架(1) 介绍(1) 单体架构->freemesh(1) 单元化(1) 博客(1) 压缩(1) 可观测(1) 后台管理系统(1) 命名规范(1) 多云多活(1) 如水网(1) 安全(1) 安装(1) 容器(2) 开发联调神器(1) 微前端(1) 微服务(2) 微服务架构->freemesh(1) 微服务框架(1) 快速开始(1) 控制面(1) 数据面(1) 文集(2) 服务发现(1) 服务网格(1) 流量泳道(1) 流量管理(1) 监控告警(1) 端口映射(1) 证书(1) 负载均衡(1) 配置(1)