etcd -- 基本操作


  1. etcd 操作,分为数据库操作和非数据库操作两种。

  2. 默认的 etcdApi2.x 版本,需设置为 3.X 的通用版本。

  3. 在 ubuntu 系统中,环境变量更改后,最好使用 reboot 命令重启一下,确保声明的环境变量生效。

export ETCDCTL_API=3

或者在`/etc/profile`文件中添加环境变量
vi /etc/profile
...
export ETCDCTL_API=3
...
source /etc/profile

数据库操作

1. 增

etcdctl put testkey01 111

2. 查

单个查找

etcdctl get testkey01

范围查找

etcdctl get testkey01  testkey03   # 查询范围为 [testkey01  testkey03 ),前闭后开,不含 testkey03

使用前缀查找

etcdctl get --prefix testkey      # 查询所有 testkey 开头的 key

如果以某前缀开始的 key 过多的话,可以限制数量

etcdctl get --prefix --limit 2  testke   #最多显示两个

3. 删除

范围删除

etcdctl del testkey01 testkey03   # 删除范围为 [testkey01  testkey03 ),前闭后开,不含 testkey03

watch 历史变动

在命令行输入watch 命令,则如果该值发生变化,则在该终端中会立马进行显示

etcdctl watch testkey03          # 监视 testkey03

etcdctl put  testkey03  0333  # 另开一个命令行,改变testkey03 的值

lease 租约

Lease 意思为租约。类似于 redis 中的 TTL (time to live)

$ etcdctl lease grant 100    # 授予租约
lease 694d80c19225c31a granted with TTL(100s)    # 得到一个租约

将租约添加到 key4 上

etcdctl put --lease=694d80c19225c31e testkey04 444    # 将租约绑定到 key4上

持续刷新租约,让其永远生效

etcdctl lease keep-alive 694d80c19225c322

刷新租期剩余时间

$ etcdctl lease timetolive 694d80c19225c322                               # 查看租期的剩余时间
lease 694d80c19225c322 granted with TTL(100s), remaining(86s)   # 查询结果:还剩86秒杀

撤销租约;在撤销的过程中,会删除掉该租约所附带的所有的 key

etcdctl lease timetolive 694d80c19225c322