新建租约
新建一个过期时间为120s的租约
# etcdctl lease grant 120 lease 018f6d7bb11aba0d granted with TTL(120s)
查看新建的租约信息
# etcdctl lease list found 1 leases 018f6d7bb11aba0d
# etcdctl lease timetolive 018f6d7bb11aba0d --keys
lease 018f6d7bb11aba0d granted with TTL(120s), remaining(99s), attached keys([])
新建key,并为该key指定租约
# etcdctl put name alice --lease="018f6d7bb11aba0d" OK # etcdctl put name1 tina --lease="018f6d7bb11aba0d" OK
# etcdctl lease timetolive 018f6d7bb11aba0d --keys
lease 018f6d7bb11aba0d granted with TTL(120s), remaining(60s), attached keys([name name1])
# etcdctl get --prefix ""
name
alice
name1
tina
等到租约过期后,再次查看租约已经过期,对应的key也已经被自动删除
# etcdctl lease timetolive 018f6d7bb11aba0d --keys
lease 018f6d7bb11aba0d already expired
续约
新建租约并赋予key值
# etcdctl lease grant 30 lease 018f6d7bd032c117 granted with TTL(30s) # etcdctl put name alice --lease=018f6d7bd032c117 OK
在租约即将过期时进行续约,命令不会自动结束,会一直显示该窗口
# etcdctl lease keep-alive 018f6d7bd032c117 lease 018f6d7bd032c117 keepalived with TTL(30) lease 018f6d7bd032c117 keepalived with TTL(30) lease 018f6d7bd032c117 keepalived with TTL(30) ......
重新打开一个窗口,查看该租约续约信息,可以看到该租约会自动续约,相应key值也不会被删除
# etcdctl lease timetolive 018f6d7bd032c117 --keys lease 018f6d7bd032c117 granted with TTL(30s), remaining(23s), attached keys([name]) # etcdctl lease timetolive 018f6d7bd032c117 --keys lease 018f6d7bd032c117 granted with TTL(30s), remaining(22s), attached keys([name]) # etcdctl lease timetolive 018f6d7bd032c117 --keys lease 018f6d7bd032c117 granted with TTL(30s), remaining(21s), attached keys([name]) # etcdctl lease timetolive 018f6d7bd032c117 --keys lease 018f6d7bd032c117 granted with TTL(30s), remaining(20s), attached keys([name]) # etcdctl lease timetolive 018f6d7bd032c117 --keys lease 018f6d7bd032c117 granted with TTL(30s), remaining(29s), attached keys([name]) # etcdctl lease timetolive 018f6d7bd032c117 --keys lease 018f6d7bd032c117 granted with TTL(30s), remaining(28s), attached keys([name]) # etcdctl lease timetolive 018f6d7bd032c117 --keys lease 018f6d7bd032c117 granted with TTL(30s), remaining(27s), attached keys([name]) # etcdctl lease timetolive 018f6d7bd032c117 --keys lease 018f6d7bd032c117 granted with TTL(30s), remaining(27s), attached keys([name]) # etcdctl lease timetolive 018f6d7bd032c117 --keys lease 018f6d7bd032c117 granted with TTL(30s), remaining(26s), attached keys([name]) # etcdctl lease timetolive 018f6d7bd032c117 --keys lease 018f6d7bd032c117 granted with TTL(30s), remaining(25s), attached keys([name]) # etcdctl lease timetolive 018f6d7bd032c117 --keys lease 018f6d7bd032c117 granted with TTL(30s), remaining(25s), attached keys([name]) # etcdctl lease timetolive 018f6d7bd032c117 --keys lease 018f6d7bd032c117 granted with TTL(30s), remaining(24s), attached keys([name]) # etcdctl lease timetolive 018f6d7bd032c117 --keys lease 018f6d7bd032c117 granted with TTL(30s), remaining(23s), attached keys([name]) # etcdctl lease timetolive 018f6d7bd032c117 --keys lease 018f6d7bd032c117 granted with TTL(30s), remaining(22s), attached keys([name]) # etcdctl lease timetolive 018f6d7bd032c117 --keys lease 018f6d7bd032c117 granted with TTL(30s), remaining(22s), attached keys([name]) # etcdctl lease timetolive 018f6d7bd032c117 --keys lease 018f6d7bd032c117 granted with TTL(30s), remaining(20s), attached keys([name]) # etcdctl lease timetolive 018f6d7bd032c117 --keys lease 018f6d7bd032c117 granted with TTL(30s), remaining(29s), attached keys([name])
回收租约
回收租约会自动删除与该租约关联的key
# etcdctl lease revoke 018f6d7bd032c117 lease 018f6d7bd032c117 revoked # etcdctl lease timetolive 018f6d7bd032c117 --keys lease 018f6d7bd032c117 already expired # etcdctl get name
===================来自一泽涟漪的博客,转载请标明出处 www.cnblogs.com/ilifeilong===================