• etcd v3租约、续约、撤销操作大全


    Etcd租约即对特定的key实现时间控制处理。

    本文包含了其关键操作梳理及特点分析,还不收藏等啥呢。

    目录

    创建租约

    使用租约

    租约失效

    续约不成功的情形

    续约及查询

    撤销租约

    附:所有可用的操作


    创建租约

    创建一个240秒的租约:

    1. docker exec etcd_test etcdctl lease grant 240                           
    2. lease 5a578262b3f6f545 granted with TTL(240s)

    使用租约

    设置一个值/abc

    1. docker exec etcd_test etcdctl put /abc  111  --user=root:root
    2. OK

    查看该租约已经在自动减少中:

    1. docker exec etcd_test etcdctl lease timetolive 5a578262b3f6f545
    2. lease 5a578262b3f6f545 granted with TTL(240s), remaining(198s)

    给 /abc绑定租约:

    1. docker exec etcd_test etcdctl put --lease=5a578262b3f6f545 /abc  111 --user=root:root
    2. OK

    租约失效

    查看一下/abc:

    1. docker exec etcd_test etcdctl get /abc --user=root:root     
    2. /abc
    3. 111

    等到租约失效时:

    1. docker exec etcd_test etcdctl lease timetolive 5a578262b3f6f545
    2. lease 5a578262b3f6f545 already expired

    再次查看该key,发现已被删除。

    也就是说,当对指定key设置租约,租约过期后如未续约则会自动删除对应的key。

    续约不成功的情形

    如果给已过期的租约进行续约:

    1. docker exec etcd_test etcdctl lease keep-alive 5a57833bbf5edc50 --user=root:root
    2. lease 5a57833bbf5edc50 expired or revoked.

    会提示租约已经过期或者被撤销了,因此续约不成功。

    注意,续约时不涉及专门的key,而是针对leaseID进行的操作。

    续约及查询

    创建一个60秒的租约,并绑定key:

    1. docker exec etcd_test etcdctl lease grant 60
    2. lease 5a57833bbf5edc5b granted with TTL(60s)
    3. # docker exec etcd_test etcdctl put --lease=5a57833bbf5edc5b /abc  111 --user=root:root
    4. OK

    续约:

    1. docker exec etcd_test etcdctl lease keep-alive 5a57833bbf5edc5b --user=root:root
    2. lease 5a57833bbf5edc5b keepalived with TTL(60)
    3. lease 5a57833bbf5edc5b keepalived with TTL(60)
    4. lease 5a57833bbf5edc5b keepalived with TTL(60)
    5. ……

    (经过确认,每20秒打印一次)

    经过调查,etcd主动给 Lease 进行续约,是以小于 TTL 的间隔来循环调用,可以想的到,如果比租期时间长,则已经过期了才续约就晚了)

    每续约一次打印一次lease 5a57833bbf5edc5b keepalived with TTL(60)

    快速查看租约的剩余时间:

    1. docker exec etcd_test etcdctl lease timetolive 5a57833bbf5edc79
    2. lease 5a57833bbf5edc79 granted with TTL(60s), remaining(41s)
    3. # docker exec etcd_test etcdctl lease timetolive 5a57833bbf5edc79
    4. lease 5a57833bbf5edc79 granted with TTL(60s), remaining(40s)
    5. # docker exec etcd_test etcdctl lease timetolive 5a57833bbf5edc79
    6. lease 5a57833bbf5edc79 granted with TTL(60s), remaining(40s)
    7. # docker exec etcd_test etcdctl lease timetolive 5a57833bbf5edc79
    8. lease 5a57833bbf5edc79 granted with TTL(60s), remaining(59s)

    可以看到每次到40秒时就会立即恢复为60秒。

    需要指出的是,代码调用时也包含了续约单次

    KeepAlive :自动定时的续约某个租约;
    KeepAliveOnce :仅续约一次;

    撤销租约

    1. docker exec etcd_test etcdctl lease revoke 5a57833bbf5edc5b --user=root:root
    2. lease 5a57833bbf5edc5b revoked

    撤销后再次查看,已经过期:

    1. docker exec etcd_test etcdctl lease timetolive 5a57833bbf5edc5b
    2. lease 5a57833bbf5edc5b already expired

    提示:在keep-alive自动续约那边中断,不能达到撤销/销毁租约的目的
     

    !续约期间删除对应的key是什么情况?

    1. docker exec etcd_test etcdctl del /abc --user=root:root
    2. 1
    3. # docker exec etcd_test etcdctl del /abc --user=root:root
    4. 0
    5. # docker exec etcd_test etcdctl lease timetolive 5a57833bbf5edc79
    6. lease 5a57833bbf5edc79 granted with TTL(60s), remaining(54s)
    7. # docker exec etcd_test etcdctl lease timetolive 5a57833bbf5edc79
    8. lease 5a57833bbf5edc79 granted with TTL(60s), remaining(52s)
    9. # docker exec etcd_test etcdctl lease timetolive 5a57833bbf5edc79
    10. lease 5a57833bbf5edc79 granted with TTL(60s), remaining(42s)
    11. # docker exec etcd_test etcdctl lease timetolive 5a57833bbf5edc79
    12. lease 5a57833bbf5edc79 granted with TTL(60s), remaining(59s)

    可以看到,删除key后,不影响租约,租约依旧自动续期。

    附:所有可用的操作

    docker exec etcd etcdctl --help

    或只看租约相关操作:

    docker exec etcd etcdctl lease --help

    1. NAME:
    2.         etcdctl - A simple command line client for etcd3.
    3. USAGE:
    4.         etcdctl [flags]
    5. VERSION:
    6.         3.5.2
    7. API VERSION:
    8.         3.5
    9. COMMANDS:
    10.         alarm disarm            Disarms all alarms
    11.         alarm list              Lists all alarms
    12.         auth disable            Disables authentication
    13.         auth enable             Enables authentication
    14.         auth status             Returns authentication status
    15.         check datascale         Check the memory usage of holding data for different workloads on a given server endpoint.
    16.         check perf              Check the performance of the etcd cluster
    17.         compaction              Compacts the event history in etcd
    18.         defrag                  Defragments the storage of the etcd members with given endpoints
    19.         del                     Removes the specified key or range of keys [key, range_end)
    20.         elect                   Observes and participates in leader election
    21.         endpoint hashkv         Prints the KV history hash for each endpoint in --endpoints
    22.         endpoint health         Checks the healthiness of endpoints specified in `--endpoints` flag
    23.         endpoint status         Prints out the status of endpoints specified in `--endpoints` flag
    24.         get                     Gets the key or a range of keys
    25.         help                    Help about any command
    26.         lease grant             Creates leases
    27.         lease keep-alive        Keeps leases alive (renew)
    28.         lease list              List all active leases
    29.         lease revoke            Revokes leases
    30.         lease timetolive        Get lease information
    31.         lock                    Acquires a named lock
    32.         make-mirror             Makes a mirror at the destination etcd cluster
    33.         member add              Adds a member into the cluster
    34.         member list             Lists all members in the cluster
    35.         member promote          Promotes a non-voting member in the cluster
    36.         member remove           Removes a member from the cluster
    37.         member update           Updates a member in the cluster
    38.         move-leader             Transfers leadership to another etcd cluster member.
    39.         put                     Puts the given key into the store
    40.         role add                Adds a new role
    41.         role delete             Deletes a role
    42.         role get                Gets detailed information of a role
    43.         role grant-permission   Grants a key to a role
    44.         role list               Lists all roles
    45.         role revoke-permission  Revokes a key from a role
    46.         snapshot restore        Restores an etcd member snapshot to an etcd directory
    47.         snapshot save           Stores an etcd node backend snapshot to a given file
    48.         snapshot status         [deprecated] Gets backend snapshot status of a given file
    49.         txn                     Txn processes all the requests in one transaction
    50.         user add                Adds a new user
    51.         user delete             Deletes a user
    52.         user get                Gets detailed information of a user
    53.         user grant-role         Grants a role to a user
    54.         user list               Lists all users
    55.         user passwd             Changes password of user
    56.         user revoke-role        Revokes a role from a user
    57.         version                 Prints the version of etcdctl
    58.         watch                   Watches events stream on keys or prefixes
    59. OPTIONS:
    60.       --cacert=""                               verify certificates of TLS-enabled secure servers using this CA bundle
    61.       --cert=""                                 identify secure client using this TLS certificate file
    62.       --command-timeout=5s                      timeout for short running command (excluding dial timeout)
    63.       --debug[=false]                           enable client-side debug logging
    64.       --dial-timeout=2s                         dial timeout for client connections
    65.   -d, --discovery-srv=""                        domain name to query for SRV records describing cluster endpoints
    66.       --discovery-srv-name=""                   service name to query when using DNS discovery
    67.       --endpoints=[127.0.0.1:2379]              gRPC endpoints
    68.   -h, --help[=false]                            help for etcdctl
    69.       --hex[=false]                             print byte strings as hex encoded strings
    70.       --insecure-discovery[=true]               accept insecure SRV records describing cluster endpoints
    71.       --insecure-skip-tls-verify[=false]        skip server certificate verification (CAUTION: this option should be enabled only for testing purposes)
    72.       --insecure-transport[=true]               disable transport security for client connections
    73.       --keepalive-time=2s                       keepalive time for client connections
    74.       --keepalive-timeout=6s                    keepalive timeout for client connections
    75.       --key=""                                  identify secure client using this TLS key file
    76.       --password=""                             password for authentication (if this option is used, --user option shouldn't include password)
    77.       --user=""                                 username[:password] for authentication (prompt if password is not supplied)
    78.   -w, --write-out="simple"                      set the output format (fields, json, protobuf, simple, table)

  • 相关阅读:
    关于计算机找不到d3dx9_43.dll,无法继续执行代码修复方法
    2.spring框架-----spring framework DI依赖注入
    矩阵求导简记
    计算机毕业设计Java奖助学金评审(源码+mysql数据库+系统+lw文档)
    dds:subscribe
    【元宇宙欧米说】蓝魂,web3专用linktree
    ARM Linux DIY(十三)Qt5 移植
    Eclipse导入项目之后中文注释乱码
    17.cuBLAS开发指南中文版--cuBLAS中的Level-1函数scal()和swap()
    Python装饰器ZERO 2 HERO
  • 原文地址:https://blog.csdn.net/HYZX_9987/article/details/126849794