使用 v3 版本,export ETCDCTL_API=3
常用操作
etcdctl 支持命令1
2
3
4
5
6
7
8
9
10
11
12
13
14[root@test-173 test]# etcdctl -h
NAME:
etcdctl - A simple command line client for etcd3.
USAGE:
etcdctl [flags]
VERSION:
3.4.13
API VERSION:
3.4
...
数据库操作
数据库操作基本围绕着对键值和目录的 CRUD 操作及其对应的生命周期管理。 etcd 这些操作符合 REST 风格的一套 API 操作
键操作
键操作包括最常用的增删改查操作,包括 PUT、GET、DELETE 等命令。
PUT 设置或者更新某个键的值
1 | [root@test-173 test]# etcdctl put /test/foo1 "Hello world" |
GET 获取指定键的值
1 | [root@test-173 test]# etcdctl get /test/foo1 |
DELETE 删除一个键或者特定范围的键1
2
3
4
5
6
7
8
9
10
11
12[root@test-173 test]# etcdctl del /test/foo1 /test/foo2 # 删除 [/test/foo1, /test/foo2)
1 # 删除了一个健
[root@test-173 test]# etcdctl get /test/foo1
[root@test-173 test]# etcdctl get /test/foo2
/test/foo2
Hello world2
root@test-173 test]# etcdctl del --prev-kv /test/foo2 # 删除键并返回值
1
/test/foo2
Hello world2
[root@test-173 test]# etcdctl get /test/foo2
watch 键值对的改动
watch 监测一个或多个键值的变化,一旦键值发生更新,就会输出最新的值并退出1
[root@test-173 ~]# etcdctl watch foo1
新开窗口1
2
3
4
5
6[root@test-173 ~]# etcdctl put foo1 Hello watch
OK
[root@test-173 ~]# etcdctl put foo bar
OK
[root@test-173 ~]# etcdctl put foo1 bar1
OK
查看 watch 窗口
1 | [root@test-173 ~]# etcdctl watch foo1 |
lease(租约)
一旦租约的 TTL 到期,租约就会过期并且所有附带的键都将被删除
1 | [root@test-173 test]# etcdctl lease grant 100 # 授予租约,TTL 为 100 秒 |