• 1. etcdctl的安装
  • 2. etcdctl V3
  • 3. etcdctl 常用命令
    • 3.1. 指定etcd集群
    • 3.2. 增删改查
    • 3.3. 集群状态
    • 3.4. 集群成员
  • 4. etcdctl get

    etcdctlv3版本与v2版本使用命令有所不同,本文介绍etcdctl v3版本的命令工具的使用方式。

    1. etcdctl的安装

    etcdctl的二进制文件可以在 github.com/coreos/etcd/releases 选择对应的版本下载,例如可以执行以下install_etcdctl.sh的脚本,修改其中的版本信息。

    1. #!/bin/bash
    2. ETCD_VER=v3.3.4
    3. ETCD_DIR=etcd-download
    4. DOWNLOAD_URL=https://github.com/coreos/etcd/releases/download
    5. # Download
    6. mkdir ${ETCD_DIR}
    7. cd ${ETCD_DIR}
    8. wget ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz
    9. tar -xzvf etcd-${ETCD_VER}-linux-amd64.tar.gz
    10. # install
    11. cd etcd-${ETCD_VER}-linux-amd64
    12. cp etcdctl /usr/local/bin/

    2. etcdctl V3

    使用etcdctlv3的版本时,需设置环境变量ETCDCTL_API=3

    1. export ETCDCTL_API=3
    2. 或者在`/etc/profile`文件中添加环境变量
    3. vi /etc/profile
    4. ...
    5. ETCDCTL_API=3
    6. ...
    7. source /etc/profile

    查看当前etcdctl的版本信息etcdctl version

    1. [root@k8s-dbg-master-1 etcd]# etcdctl version
    2. etcdctl version: 3.3.4
    3. API version: 3.3

    更多命令帮助可以查询etcdctl —help

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

    3. etcdctl 常用命令

    3.1. 指定etcd集群

    1. HOST_1=10.240.0.17
    2. HOST_2=10.240.0.18
    3. HOST_3=10.240.0.19
    4. ENDPOINTS=$HOST_1:2379,$HOST_2:2379,$HOST_3:2379
    5. etcdctl --endpoints=$ENDPOINTS member list

    3.2. 增删改查

    1、增

    1. etcdctl --endpoints=$ENDPOINTS put foo "Hello World!"

    2、查

    1. etcdctl --endpoints=$ENDPOINTS get foo
    2. etcdctl --endpoints=$ENDPOINTS --write-out="json" get foo

    基于相同前缀查找

    1. etcdctl --endpoints=$ENDPOINTS put web1 value1
    2. etcdctl --endpoints=$ENDPOINTS put web2 value2
    3. etcdctl --endpoints=$ENDPOINTS put web3 value3
    4. etcdctl --endpoints=$ENDPOINTS get web --prefix

    列出所有的key

    1. etcdctl --endpoints=$ENDPOINTS get / --prefix --keys-only

    3、删**

    1. etcdctl --endpoints=$ENDPOINTS put key myvalue
    2. etcdctl --endpoints=$ENDPOINTS del key
    3. etcdctl --endpoints=$ENDPOINTS put k1 value1
    4. etcdctl --endpoints=$ENDPOINTS put k2 value2
    5. etcdctl --endpoints=$ENDPOINTS del k --prefix

    3.3. 集群状态

    集群状态主要是etcdctl endpoint statusetcdctl endpoint health两条命令。

    1. etcdctl --write-out=table --endpoints=$ENDPOINTS endpoint status
    2. +------------------+------------------+---------+---------+-----------+-----------+------------+
    3. | ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | RAFT TERM | RAFT INDEX |
    4. +------------------+------------------+---------+---------+-----------+-----------+------------+
    5. | 10.240.0.17:2379 | 4917a7ab173fabe7 | 3.0.0 | 45 kB | true | 4 | 16726 |
    6. | 10.240.0.18:2379 | 59796ba9cd1bcd72 | 3.0.0 | 45 kB | false | 4 | 16726 |
    7. | 10.240.0.19:2379 | 94df724b66343e6c | 3.0.0 | 45 kB | false | 4 | 16726 |
    8. +------------------+------------------+---------+---------+-----------+-----------+------------+
    9. etcdctl --endpoints=$ENDPOINTS endpoint health
    10. 10.240.0.17:2379 is healthy: successfully committed proposal: took = 3.345431ms
    11. 10.240.0.19:2379 is healthy: successfully committed proposal: took = 3.767967ms
    12. 10.240.0.18:2379 is healthy: successfully committed proposal: took = 4.025451ms

    3.4. 集群成员

    跟集群成员相关的命令如下:

    1. member add Adds a member into the cluster
    2. member remove Removes a member from the cluster
    3. member update Updates a member in the cluster
    4. member list Lists all members in the cluster

    例如 etcdctl member list列出集群成员的命令。

    1. etcdctl --endpoints=http://172.16.5.4:12379 member list -w table
    2. +-----------------+---------+-------+------------------------+-----------------------------------------------+
    3. | ID | STATUS | NAME | PEER ADDRS | CLIENT ADDRS |
    4. +-----------------+---------+-------+------------------------+-----------------------------------------------+
    5. | c856d92a82ba66a | started | etcd0 | http://172.16.5.4:2380 | http://172.16.5.4:2379,http://172.16.5.4:4001 |
    6. +-----------------+---------+-------+------------------------+-----------------------------------------------+

    4. etcdctl get

    使用etcdctl {command} --help可以查看具体命令的帮助信息。

    1. # etcdctl get --help
    2. NAME:
    3. get - Gets the key or a range of keys
    4. USAGE:
    5. etcdctl get [options] <key> [range_end]
    6. OPTIONS:
    7. --consistency="l" Linearizable(l) or Serializable(s)
    8. --from-key[=false] Get keys that are greater than or equal to the given key using byte compare
    9. --keys-only[=false] Get only the keys
    10. --limit=0 Maximum number of results
    11. --order="" Order of results; ASCEND or DESCEND (ASCEND by default)
    12. --prefix[=false] Get keys with matching prefix
    13. --print-value-only[=false] Only write values when using the "simple" output format
    14. --rev=0 Specify the kv revision
    15. --sort-by="" Sort target; CREATE, KEY, MODIFY, VALUE, or VERSION
    16. GLOBAL OPTIONS:
    17. --cacert="" verify certificates of TLS-enabled secure servers using this CA bundle
    18. --cert="" identify secure client using this TLS certificate file
    19. --command-timeout=5s timeout for short running command (excluding dial timeout)
    20. --debug[=false] enable client-side debug logging
    21. --dial-timeout=2s dial timeout for client connections
    22. --endpoints=[127.0.0.1:2379] gRPC endpoints
    23. --hex[=false] print byte strings as hex encoded strings
    24. --insecure-skip-tls-verify[=false] skip server certificate verification
    25. --insecure-transport[=true] disable transport security for client connections
    26. --key="" identify secure client using this TLS key file
    27. --user="" username[:password] for authentication (prompt if password is not supplied)
    28. -w, --write-out="simple" set the output format (fields, json, protobuf, simple, table)

    文章参考:

    https://coreos.com/etcd/docs/latest/demo.html