etcd CRUD命令行操作

etcdctl工具是一个可以对etcd数据进行管理的命令行工具,这个工具在两个不同的etcd版本下的行为方式也完全不同。

export ETCDCTL_API=2
export ETCDCTL_API=3

etcdctl APIv2和APIv3的操作命令略有区别,不过其在和skydns、coredns这类DNS结合时,一般使用APIv2版本,其在k8s中调用时,一般使用的是APIv3版本。由于这这里写的时候主要是和coredns进行集成,所以这里使用apiv2,apiv3也可以通过--help进行查看。

etcd数据库健康状况查看

这里是单节点操作,多节点也是一样的:

[root@361way ~]# etcdctl cluster-health
member 64359695a101 is healthy: got healthy result from http://10.212.52.253:2379
cluster is healthy
[root@361way ~]# etcdctl member list
64359695a101: name=etcd-dns-bj01 peerURLs=http://localhost:2380 clientURLs=http://10.212.52.253:2379 isLeader=true

etcd增删改查

命令简介

etcdctl主要命令参数及作用如下:

set: 指定某个键的值
get: 获取指定键的值
update: 当键存在时,更新值内容;当键不存在时,则会报错
rm: 删除某个键值,当键不存在时,则会报错Key not found。
mk: 如果给定的键不存在,则创建一个新的键值。当键存在的时候,执行该命令会报错Key already exists
mkdir: 如果给定的键目录不存在,则创建一个新的键目录;当键目录存在的时候,执行该命令会报错。
setdir: 创建一个键目录,无论存在与否。
updatedir: 更新一个已经存在的目录。
rmdir: 删除一个空目录,或者键值对。若目录不空,会报错Directory not empty
ls: 列出目录(默认为根目录)下的键或者子目录,默认不显示子目录中内容。支持-r递归和--sort排序

watch: 监测一个键值的变化,一旦键值发生更新,就会输出最新的值并退出。
exec-watch: 监测一个键值的变化,一旦键值发生更新,就执行给定命令。

操作示例

root@361way ~]# etcdctl ls -r /
[root@361way ~]# 
[root@361way ~]# etcdctl mk /com/361way/www  '{"host":"115.28.174.118"}'
{"host":"115.28.174.118"}

[root@361way ~]# etcdctl ls -r /
/com
/com/361way
/com/361way/www

[root@361way ~]# etcdctl get /com/361way/www
{"host":"115.28.174.118"}

从上面的ls操作可以看出,在我们操作前是没有数据的。通过mk创建健值时,其不存在的目录也会一起创建。

目录创建操作:
[root@361way ~]# etcdctl mkdir /test
[root@361way ~]# etcdctl ls /
/com
/test
[root@361way ~]# etcdctl setdir /test/abc
[root@361way ~]# etcdctl setdir /test/abc
Error:  102: Not a file (/test/abc) [1010]
[root@361way ~]# etcdctl ls /test/
/test/abc
健值和更新:
[root@361way ~]# etcdctl set  /test/abc/site "www.361way.com"
www.361way.com
[root@361way ~]# etcdctl get  /test/abc/site "www.361way.com"
www.361way.com

[root@361way ~]# etcdctl update  /test/abc/site "https://www.361way.com"
https://www.361way.com
[root@361way ~]# etcdctl get  /test/abc/site "https://www.361way.com"
https://www.361way.com
扩展操作:
# ls排序
[root@361way ~]# etcdctl ls -r --sort /
/com
/com/361way
/com/361way/www
/test
/test/abc
/test/abc/site

# 数据输出的格式:simple, `extended` or `json`) (default: "simple")
[root@361way ~]# etcdctl -o json get /test/abc/site
{"action":"get","node":{"key":"/test/abc/site","value":"https://www.361way.com","nodes":null,"createdIndex":1011,"modifiedIndex":1012},"prevNode":null}

[root@361way ~]# etcdctl -o extended get /test/abc/site
Key: /test/abc/site
Created-Index: 1011
Modified-Index: 1012
TTL: 0
Index: 1012

https://www.361way.com
watch相关:
# 当修改某个值时,watch捕获到变化输出新值并退出 
[root@361way ~]# etcdctl set /test/abc/site "运维之路"
运维之路

[root@361way ~]# etcdctl watch --recursive /test
[set] /test/abc/site
运维之路


# 使用watch --forever时,值变化,其输出但不退出,只有ctrl+c才会退出
[root@361way ~]# etcdctl update /test/abc/site "linux.361way.com"
linux.361way.com

[root@361way ~]# etcdctl watch -r -f  /test
[update] /test/abc/site
linux.361way.com
^C[root@361way ~]#

从上面的操作中,我们还发现,使用update和set都可以更新一个已经存在值的键值。而使用简单的shell死循环很容易实现值的监测:

while true; do etcdctl watch --recursive /test; done

exec-watch有意思的操作:

[root@361way ~]# etcdctl update /test/abc/site "wiki.361way.com"
wiki.361way.com

[root@361way ~]# etcdctl exec-watch --recursive  /test -- sh -c "ls"
anaconda-ks.cfg  a.sh  Desktop  Documents  Downloads  initial-setup-ks.cfg  Music  Pictures  Public  Templates  Videos
^C[root@361way ~]# 

当使用exec-watch监控有操作变化时,我们可以调用外部命令执行一个操作。这个功能我觉得有很多现象空间。比如监控、动态更新。

区分键和目录

之前我写了etcd备份与恢复,在数据量不多时,也可以通过读取所有配置的方法备份数据。新的主机上通过上面提到的etcdctl mk完成所有数据的创建恢复。

而区分键和目录可以通过-p参数查看后面有没有/,带/的是目录,不带的就是值。

[root@361way ~]# etcdctl ls -r -p /
/com/
/com/361way/
/com/361way/www
/test/
/test/abc/
/test/abc/site

[root@361way ~]# etcdctl ls -r -p /|grep -v '/$'
/test/abc/site
/com/361way/www

donation