• Etcd教程 — 第五章 Etcd之etcdctl的使用


    前言

    Etcd在微服务和Kubernates集群中不仅可以作为服务注册发现,还可以作为 key-value 存储的中间件。

    一、etcdctl介绍

    etcdctl是一个命令行的客户端,它能提供一些简洁的命令,供用户直接跟etcd服务打交道,而无需基于 HTTP API 方式。方便对服务进行测试或者手动修改数据库内容。
    我们刚开始可以通过etdctl来熟悉相关操作。这些操作跟 HTTP API 基本上是对应的。etcdctl在两个不同的 etcd 版本下的行为方式也完全不同。

    export ETCDCTL_API=2
    export ETCDCTL_API=3
    
    • 1
    • 2

    本文主要以API 3为主。
    Etcd二进制发行包中已经包含了etcdctl工具,etcdctl支持的命令大体上分为数据库操作和非数据库操作两类。

    二、非数据库操作

    2.1 查看Etcd版本的命令

    [root@etcd01 ~]# etcd --version
    etcd Version: 3.5.4
    Git SHA: 08407ff76
    Go Version: go1.16.15
    Go OS/Arch: linux/amd64
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.2 查看etcdctl常用的命令

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

    可以看到,etcdctl的命令很多,常用的命令选项【OPTIONS】:

    --debug 输出CURL命令,显示执行命令的时候发起的请求
    --no-sync 发出请求之前不同步集群信息
    --output, -o 'simple' 输出内容的格式(simple 为原始信息,json 为进行json格式解码,易读性好一些)
    --peers, -C 指定集群中的同伴信息,用逗号隔开(默认为: "127.0.0.1:4001")
    --cert-file HTTPS下客户端使用的SSL证书文件
    --key-file HTTPS下客户端使用的SSL密钥文件
    --ca-file 服务端使用HTTPS时,使用CA文件进行验证
    --help, -h 显示帮助命令信息
    --version, -v 打印版本信息
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    三、数据库操作

    数据库操作围绕对键值和目录的 CRUD (即增删改查,符合 REST 风格的一套API操作)完整生命周期的管理。

    Etcd在键的组织上采用了层次化的空间结构(类似于文件系统中目录的概念),用户指定的键可以为单独的名字,如:testkey,此时实际上放在根目录/下面,也可以为指定目录结构,如/cluster1/node2/testkey,则将创建相应的目录结构。

    3.1 写操作

    [root@etcd01 ~]# etcdctl put /testdir/testkey "Hello world"
    OK
    [root@etcd01 ~]# etcdctl put /testdir/testkey2 "Hello world2"
    OK
    [root@etcd01 ~]# etcdctl put /testdir/testkey3 "Hello world3"
    OK
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    成功写入三对键值,/testdir/testkey、/testdir/testkey2 和 /testdir/testkey3。

    3.2 读操作

    [root@etcd01 ~]# etcdctl get /testdir/testkey
    /testdir/testkey
    Hello world
    
    • 1
    • 2
    • 3

    get 十六进制读指定的值:

    [root@etcd01 ~]# etcdctl get /testdir/testkey --hex
    \x2f\x74\x65\x73\x74\x64\x69\x72\x2f\x74\x65\x73\x74\x6b\x65\x79
    \x48\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64
    
    • 1
    • 2
    • 3

    get 指定范围内的值:

    [root@etcd01 ~]# etcdctl get /testdir/testkey /testdir/testkey3
    /testdir/testkey
    Hello world
    /testdir/testkey2
    Hello world2
    
    • 1
    • 2
    • 3
    • 4
    • 5

    可以看到,获取了大于等于 /testdir/testkey,且小于 /testdir/testkey3 的键值对。testkey3 不在范围之内,因为范围是半开区间 [testkey, testkey3), 不包含 testkey3。

    获取指定前缀的所有键值对,通过 –prefix 可以指定前缀:

    [root@etcd01 ~]# etcdctl get --prefix /testdir/testkey
    /testdir/testkey
    Hello world
    /testdir/testkey2
    Hello world2
    /testdir/testkey3
    Hello world3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    这样即可获取所有以 /testdir/testkey 开头的键值对。当前缀获取的结果过多时,还可以通过 –limit=2 限制获取的数量:

    etcdctl get --prefix --limit=2 /testdir/testkey
    
    • 1

    读取键过往版本的值:

    应用可能想读取键的被替代的值【即旧值】。例如,应用可能想通过访问键的过往版本来回滚到旧的配置。或者,应用可能想通过多个请求来得到一个覆盖多个键的统一视图,而这些请求可以通过访问键历史记录而来。因为 etcd 集群上键值存储的每个修改都会增加 etcd 集群的全局修订版本,应用可以通过提供旧有的 etcd 修改版本来读取被替代的键。现有如下这些键值对:

    foo = bar         # revision = 2
    foo1 = bar2       # revision = 3
    foo = bar_new     # revision = 4
    foo1 = bar2_new   # revision = 5
    
    • 1
    • 2
    • 3
    • 4

    准备工作:

    [root@etcd01 ~]# etcdctl put foo bar
    OK
    [root@etcd01 ~]# etcdctl put foo1 bar2
    OK
    [root@etcd01 ~]# etcdctl put foo bar_new
    OK
    [root@etcd01 ~]# etcdctl put foo1 bar2_new
    OK
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    访问以前版本 key 的示例:

    [root@etcd01 ~]# etcdctl get --prefix foo #访问最新版本的key
    foo
    bar_new
    foo1
    bar2_new
    
    [root@etcd01 ~]# etcdctl get --prefix --rev=4 foo # 访问第4个版本的key
    foo
    bar_new
    foo1
    bar2
    
    [root@etcd01 ~]# etcdctl get --prefix --rev=3 foo #  访问第3个版本的key
    foo
    bar
    foo1
    bar2
    
    [root@etcd01 ~]# etcdctl get --prefix --rev=2 foo #  访问第3个版本的key
    foo
    bar
    
    [root@etcd01 ~]# etcdctl get --prefix --rev=1 foo #  访问第1个版本的key
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    读取大于等于指定键的 byte 值的键
    应用可能想读取大于等于指定键 的 byte 值的键。假设 etcd 集群已经有下列键:

    a = 123
    b = 456
    z = 789
    
    • 1
    • 2
    • 3

    读取大于等于键 b 的 byte 值的键的命令:

    [root@etcd01 ~]# etcdctl get --from-key b
    b
    456
    c
    789
    
    • 1
    • 2
    • 3
    • 4
    • 5

    删除键。应用可以从 etcd 集群中删除一个键或者特定范围的键。
    假设 etcd 集群已经有下列键:

    a = 123
    b = 456
    c = 789
    foo = bar_new
    foo1 = bar2_new
    hello = world
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    删除键 foo 的命令:

    [root@etcd01 ~]# etcdctl del foo
    1 # 删除键的个数
    
    • 1
    • 2

    删除从 foo to foo1 范围的键的命令:

    etcdctl del foo foo1
    
    • 1

    删除键 a并返回被删除的键值对的命令:

    [root@etcd01 ~]# etcdctl del --prev-kv a
    1 # 删除键的个数
    a # 被删除的键
    123 # 被删除的键的值
    
    • 1
    • 2
    • 3
    • 4

    删除前缀为 foo 的键的命令:

    [root@etcd01 ~]# etcdctl del --prefix foo
    1 # 删除键的个数
    
    • 1
    • 2

    删除大于等于键 b 的 byte 值的键的命令:

    [root@etcd01 ~]# etcdctl del --from-key b
    3 # 删除键的个数
    
    • 1
    • 2

    3.3 watch 历史改动

  • 相关阅读:
    Shel简介入门
    Day 54 前端 jQuery
    遥感数据与作物模型同化技术
    编写第一个Qt程序和分析第一个Qt程序
    Cesium:实现卷帘效果
    电输运性质测试系统
    通过部署流行 Web 框架掌握 Serverless 技术
    centos8stream 编译安装 php-rabbit-mq模块
    java_springboot水果购物商城销售管理系统
    第三十八篇 Vue中封装Swiper组件 2.0
  • 原文地址:https://blog.csdn.net/Mr_XiMu/article/details/125474908