1.简单使用
1.1 增加
指定某个键的值。例如:
$ etcdctl set /testdir/testkey "Hello world"
Hello world
复制代码
支持的选项包括:
--ttl '0' 该键值的超时时间(单位为秒),不配置(默认为0)则永不超时
--swap-with-value value 若该键现在的值是value,则进行设置操作
--swap-with-index '0' 若该键现在的索引值是指定索引,则进行设置操作
复制代码
如果给定的键不存在,则创建一个新的键值。例如:
$ etcdctl mk /testdir/testkey "Hello world"
Hello world
复制代码
当键存在的时候,执行该命令会报错,例如:
$ etcdctl mk /testdir/testkey "Hello world"
Error: 105: Key already exists (/testdir/testkey) [8]
复制代码
支持的选项为:
--ttl '0' 超时时间(单位为秒),不配置(默认为 0)。则永不超时
复制代码
如果给定的键目录不存在,则创建一个新的键目录。例如:
$ etcdctl mkdir testdir2
复制代码
当键目录存在的时候,执行该命令会报错,例如:
$ etcdctl mkdir testdir2
Error: 105: Key already exists (/testdir2) [9]
复制代码
支持的选项为:
--ttl '0' 超时时间(单位为秒),不配置(默认为0)则永不超时。
复制代码
创建一个键目录。如果目录不存在就创建,如果目录存在更新目录TTL。
$ etcdctl setdir testdir3
复制代码
支持的选项为:
--ttl '0' 超时时间(单位为秒),不配置(默认为0)则永不超时。
复制代码
1.2 删除
删除某个键值。例如:
$ etcdctl rm /testdir/testkey
PrevNode.Value: Hello
复制代码
当键不存在时,则会报错。例如:
$ etcdctl rm /testdir/testkey
Error: 100: Key not found (/testdir/testkey) [7]
复制代码
支持的选项为:
--dir 如果键是个空目录或者键值对则删除
--recursive 删除目录和所有子键
--with-value 检查现有的值是否匹配
--with-index '0'检查现有的index是否匹配
复制代码
删除一个空目录,或者键值对。
$ etcdctl setdir dir1
$ etcdctl rmdir dir1
复制代码
若目录不空,会报错:
$ etcdctl set /dir/testkey hi
hi
$ etcdctl rmdir /dir
Error: 108: Directory not empty (/dir) [17]
复制代码
1.3 更新
当键存在时,更新值内容。例如:
$ etcdctl update /testdir/testkey "Hello"
Hello
复制代码
当键不存在时,则会报错。例如:
$ etcdctl update /testdir/testkey2 "Hello"
Error: 100: Key not found (/testdir/testkey2) [6]
复制代码
支持的选项为:
--ttl '0' 超时时间(单位为秒),不配置(默认为 0)则永不超时。
复制代码
更新一个已经存在的目录。
$ etcdctl updatedir testdir2
复制代码
支持的选项为:
--ttl '0' 超时时间(单位为秒),不配置(默认为0)则永不超时。
复制代码
1.4 查询
获取指定键的值。例如:
$ etcdctl get /testdir/testkey
Hello world
复制代码
当键不存在时,则会报错。例如:
$ etcdctl get /testdir/testkey2
Error: 100: Key not found (/testdir/testkey2) [5]
复制代码
支持的选项为:
--sort 对结果进行排序
--consistent 将请求发给主节点,保证获取内容的一致性。
复制代码
列出目录(默认为根目录)下的键或者子目录,默认不显示子目录中内容。
例如:
$ etcdctl ls
/testdir
/testdir2
/dir
$ etcdctl ls dir
/dir/testkey
复制代码
支持的选项包括:
--sort 将输出结果排序
--recursive 如果目录下有子目录,则递归输出其中的内容
-p 对于输出为目录,在最后添加/进行区分
复制代码
1.5 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 返回所有的键值和子键值
复制代码
监测一个键值的变化,一旦键值发生更新,就执行给定命令。
例如:用户更新testkey键值。
$ etcdctl exec-watch testdir/testkey -- sh -c 'ls'
config Documentation etcd etcdctl README-etcdctl.md README.md READMEv2-etcdctl.md
复制代码
支持的选项包括:
--after-index '0' 在指定 index 之前一直监测
--recursive 返回所有的键值和子键值
复制代码
1.6 备份
备份etcd的数据。
$ etcdctl backup --data-dir /var/lib/etcd --backup-dir /home/etcd_backup
复制代码
支持的选项包括:
--data-dir etcd的数据目录
--backup-dir 备份到指定路径
复制代码
1.7 member
通过list
、add
、remove
命令列出、添加、删除etcd实例到etcd集群中。
查看集群中存在的节点
$ etcdctl member list
8e9e05c52164694d: name=dev-master-01 peerURLs=http://localhost:2380 clientURLs=http://localhost:2379 isLeader=true
复制代码
删除集群中存在的节点
$ etcdctl member remove 8e9e05c52164694d
Removed member 8e9e05c52164694d from cluster
复制代码
向集群中新加节点
$ etcdctl member add etcd3 http:
Added member named etcd3 with ID 8e9e05c52164694d to cluster
2.使用示例
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)