etcd常用命令使用手册

Etcd 常用命令整理

etcd的安装和集群部署参考链接:https://blog.csdn.net/WangYouJin321/article/details/80417622

1. Etcd的启动与基本操作

1.通过etcdctl客户端进行操作    2.curl 方式(不做介绍)

启动etcd:

[root@wangyoujin software]# cd etcd/

[root@wangyoujin etcd]# ./etcd

默认数据存储文件(可配置):

2017/09/18 14:36:04 no data-dir provided,using default data-dir ./default.etcd

2017/09/18 14:36:04 etcd: listen tcp 127.0.0.1:2380: bind: address already inuse

[root@wangyoujin etcd]# ls

default.etcd  Documentation etcd  etcd-backup  etcdctl etcd-migrate README-etcdctl.md  README.md  rm.sh

 

   Etcd安装完成后可以通过etcdctl客户端对其进行相关操作,常见的commands如下图中红色圈出的部分。

 

 

关于etcdctl使用注意:

1目录是目录,节点是节点,具有唯一性,不可重名

2目录是唯一存在的,最终的叶子节点不一定

3 版本2与3的差别,坚持读写使用同一个版本,这个文档是以2为例说明的

 

 

2.Etcd 常用命令

set   mk

指定某个键的值。例如

./etcdctl set /testdir/testkey "Hello world"
Hello world

支持的选项包括:

--ttl '0'            该键值的超时时间(单位为秒),不配置(默认为 0)则永不超时
  --swap-with-value value 若该键现在的值是 value,则进行设置操作
--swap-with-index '0'    若该键现在的索引值是指定索引,则进行设置操作

MK

如果给定的键不存在,则创建一个新的键值。例如

./etcdctl mk /testdir/testkey "Hello world"
Hello world

当键存在的时候,执行该命令会报错,例如

./etcdctl mk /testdir/testkey "Hello world"
Error:  105: Key already exists (/testdir/testkey) [21]

支持的选项为

--ttl '0'    超时时间(单位为秒),不配置(默认为 0)则永不超时

 

get

获取指定键的值。例如

./etcdctl get /testdir/testkey
Hello world

当键不存在时,则会报错。例如

./etcdctl get /testdir/testkey2
Error:  100: Key not found (/testdir/testkey2) [18]

支持的选项为

--sort    对结果进行排序
--consistent 将请求发给主节点,保证获取内容的一致性

update

当键存在时,更新值内容。例如

[root@wangyoujin etcd]# ./etcdctl mknewdir/wang/shuai "haoshuai"

haoshuai

[root@wangyoujin etcd]# ./etcdctl updatenewdir/wang/shuai "zhendehaoshuai"

zhendehaoshuai

[root@wangyoujin etcd]# ./etcdctl getnewdir/wang/shuai

zhendehaoshuai

rm

删除某个键值。例如

./etcdctl rm /testdir/testkey
PrevNode.Value: Hello

当键不存在时,则会报错。例如

./etcdctl rm /testdir/testkey
Error:  100: Key not found (/testdir/testkey) [20]

支持的选项为

--dir        如果键是个空目录或者键值对则删除
--recursive        删除目录和所有子键
--with-value     检查现有的值是否匹配
--with-index '0'    检查现有的 index 是否匹配

rmdir

removes the key if it is an empty directory or akey-value pair

删除键值或者空目录 

wang下面节点已删除,变成空目录

[root@wangyoujin etcd]# ./etcdctl rm newdir/wang

Error:  102: Not a file (/newdir/wang) [587532]

 

[root@wangyoujin etcd]# ./etcdctl rmdir newdir/wang

[root@wangyoujin etcd]#

[root@wangyoujin etcd]# ./etcdctl ls/newdir

/newdir/testkey2

/newdir/testkey3

Etcd清空 加: -recursive=true ,删除根目录

[root@wangyoujin etcd]# ./etcdctl rm/newdir

Error: 102: Not a file (/newdir) [589640]

[root@wangyoujin etcd]# ./etcdctl rm/newdir-recursive=true

[root@wangyoujin etcd]# ./etcdctl ls/newdir

Error: 100: Key not found (/newdir) [589694]

[root@wangyoujin etcd]#

 

mkdir

如果给定的键目录不存在,则创建一个新的键目录。例如

➜  ~  etcdctl mkdir testdir2

当键目录存在的时候,执行该命令会报错,例如

➜  ~  etcdctl mkdir testdir2
Error:  105: Key already exists (/testdir2) [22]

支持的选项为

--ttl '0'    超时时间(单位为秒),不配置(默认为 0)则永不超时

setdir

创建一个键目录,无论存在与否。

支持的选项为

--ttl '0'    超时时间(单位为秒),不配置(默认为 0)则永不超时

updatedir

更新一个已经存在的目录。支持的选项为

--ttl '0'    超时时间(单位为秒),不配置(默认为 0)则永不超时

非数据库操作

watch

监测一个键值的变化,一旦键值发生更新,就会输出最新的值并退出。

例如,用户更新 testkey 键值为 Hello watch

➜  ~  etcdctl get /testdir/testkey
Hello world
  ➜  ~  etcdctl set /testdir/testkey "Hello watch"
Hello watch

 

➜  ~  etcdctl watch testdir/testkey
Hello watch

支持的选项包括

--forever        一直监测,直到用户按 `CTRL+C` 退出
--after-index '0'    在指定 index 之前一直监测
--recursive        返回所有的键值和子键值

exec-watch

监测一个键值的变化,一旦键值发生更新,就执行给定命令。

例如,用户更新 testkey 键值。

➜  ~  etcdctl exec-watch testkey -- sh -c 'ls'
default.etcd
Documentation
etcd
etcdctl
etcd-migrate
README-etcdctl.md
README.md

支持的选项包括

--after-index '0'    在指定 index 之前一直监测
--recursive        返回所有的键值和子键值

member

通过 listaddremove 命令列出、添加、删除 etcd 实例到 etcd 集群中。

例如本地启动一个 etcd 服务实例后,可以用如下命令进行查看。

$ etcdctl member list
ce2a822cea30bfca: name=default peerURLs=http://localhost:2380,http://localhost:7001 clientURLs=http://localhost:2379,http://localhost:4001

命令选项

--debug 输出 cURL 命令,显示执行命令的时候发起的请求
--no-sync 发出请求之前不同步集群信息
--output, -o 'simple' 输出内容的格式 (simple 为原始信息,json 为进行json格式解码,易读性好一些)
--peers, -C 指定集群中的同伴信息,用逗号隔开 (默认为: "127.0.0.1:4001")
--cert-file HTTPS 下客户端使用的 SSL 证书文件
--key-file HTTPS 下客户端使用的 SSL 密钥文件
--ca-file 服务端使用 HTTPS 时,使用 CA 文件进行验证
--help, -h 显示帮助命令信息
--version, -v 打印版本信息

 

 

 

 

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小金子的夏天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值