etcd 终端命令交互

应用通过写入键来储存键到 etcd 中。每个存储的键通过 Raft 协议复制到 etcd 集群的所有成员来实现一致性和可靠性
写入键
这是设置键 name 的值为 justin 的命令: put

[justin@VM_16_9_centos etcd]$ ./etcdctl put name justin
OK

读取键
这是读取键 name 的值的命令:get

[justin@VM_16_9_centos etcd]$ ./etcdctl get name
name
justin

我们也读取范围覆盖从 name to name3 的键的命令

[justin@VM_16_9_centos etcd]$ ./etcdctl put name1 justin1
OK
[justin@VM_16_9_centos etcd]$ ./etcdctl put name2 justin2
OK
[justin@VM_16_9_centos etcd]$ ./etcdctl put name3 justin3
OK
[justin@VM_16_9_centos etcd]$ ./etcdctl get name name3
name
justin
name1
justin1
name2
justin2

由上面的结果我们可以看出,name3是不包括的,这个范围就是半开区间(不包括右边name3)[name name3)

删除键
etcd 集群中删除一个键或者特定范围的键
删除一个键

[justin@VM_16_9_centos etcd]$ ./etcdctl del name1
1

删除一个范围

[justin@VM_16_9_centos etcd]$ ./etcdctl del name2 name3
1

观察键的变化

watch 监视到name变化

[justin@VM_16_9_centos etcd]$ ./etcdctl watch name
# 在另外一个终端: ./etcdctl put name newname
PUT
name
newname

观察前缀为name值的变化

[justin@VM_16_9_centos etcd]$ ./etcdctl watch --prefix name
# 在另外一个终端: ./etcdctl put name justin
PUT
name
justin
# 在另外一个终端: ./etcdctl put name1 justin1
PUT
name1
justin1
# 在另外一个终端: ./etcdctl put name2 justin2
PUT
name2
justin2

观察多个键 name 和 name1 的命令

[justin@VM_16_9_centos etcd]$ ./etcdctl watch -i
watch name
watch name1
# 在另外一个终端: ./etcdctl put name newjustin
PUT
name
newjustin
# 在另外一个终端: ./etcdctl put name1 newjustin1
PUT
name1
newjustin1

租约
租约是对键增加过期时间,到期后键 会自动删除

授予租约
授予租约的命令:

[justin@VM_16_9_centos etcd]$ ./etcdctl lease grant 50
lease 694d73b82f96340d granted with TTL(50s)

假设我们做以下操作

[justin@VM_16_9_centos etcd]$ ./etcdctl lease grant 50
lease 694d73b82f963414 granted with TTL(50s)
[justin@VM_16_9_centos etcd]$ ./etcdctl put --lease=694d73b82f963414 test testlease
OK

维持同一个租约的命令:

[justin@VM_16_9_centos etcd]$ ./etcdctl lease revoke 694d73b82f963414
lease 694d73b82f963414 revoked
[justin@VM_16_9_centos etcd]$ ./etcdctl get test
# 空应答,因为租约撤销导致test被删除

维持租约
假设我们完成了下列操作:

[justin@VM_16_9_centos etcd]$ ./etcdctl lease grant 20
lease 694d73b82f963427 granted with TTL(20s)

这是维持同一个租约的命令:keep-alive

[justin@VM_16_9_centos etcd]$ ./etcdctl lease keep-alive 694d73b82f963427
lease 694d73b82f963427 keepalived with TTL(20)
lease 694d73b82f963427 keepalived with TTL(20)
lease 694d73b82f963427 keepalived with TTL(20)
...

获取租约信息
获取租约信息的命令:timetolive

[justin@VM_16_9_centos etcd]$ ./etcdctl lease timetolive 694d73b82f963427
lease 694d73b82f963427 granted with TTL(20s), remaining(17s)

获取租约信息和租约附带的键的命令:–keys

[justin@VM_16_9_centos etcd]$ ./etcdctl lease timetolive --keys 694d73b82f963427
lease 694d73b82f963427 granted with TTL(20s), remaining(19s), attached keys([test])
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页