1. etcdctl的安装

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

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 update Updates a member in the cluster
  32. member list Lists all members in the cluster
  33. snapshot save Stores an etcd node backend snapshot to a given file
  34. snapshot restore Restores an etcd member snapshot to an etcd directory
  35. snapshot status Gets backend snapshot status of a given file
  36. make-mirror Makes a mirror at the destination etcd cluster
  37. migrate Migrates keys in a v2 store to a mvcc store
  38. lock Acquires a named lock
  39. elect Observes and participates in leader election
  40. auth enable Enables authentication
  41. auth disable Disables authentication
  42. user add Adds a new user
  43. user delete Deletes a user
  44. user get Gets detailed information of a user
  45. user list Lists all users
  46. user passwd Changes password of user
  47. user grant-role Grants a role to a user
  48. user revoke-role Revokes a role from a user
  49. role add Adds a new role
  50. role delete Deletes a role
  51. role get Gets detailed information of a role
  52. role list Lists all roles
  53. role grant-permission Grants a key to a role
  54. role revoke-permission Revokes a key from a role
  55. check perf Check the performance of the etcd cluster
  56. help Help about any command
  57. OPTIONS:
  58. --cacert="" verify certificates of TLS-enabled secure servers using this CA bundle
  59. --cert="" identify secure client using this TLS certificate file
  60. --command-timeout=5s timeout for short running command (excluding dial timeout)
  61. --debug[=false] enable client-side debug logging
  62. --dial-timeout=2s dial timeout for client connections
  63. -d, --discovery-srv="" domain name to query for SRV records describing cluster endpoints
  64. --endpoints=[127.0.0.1:2379] gRPC endpoints
  65. --hex[=false] print byte strings as hex encoded strings
  66. --insecure-discovery[=true] accept insecure SRV records describing cluster endpoints
  67. --insecure-skip-tls-verify[=false] skip server certificate verification
  68. --insecure-transport[=true] disable transport security for client connections
  69. --keepalive-time=2s keepalive time for client connections
  70. --keepalive-timeout=6s keepalive timeout for client connections
  71. --key="" identify secure client using this TLS key file
  72. --user="" username[:password] for authentication (prompt if password is not supplied)
  73. -w, --write-out="simple" set the output format (fields, json, protobuf, simple, table)

3. etcdctl 常用命令

  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

3、删**

  1. etcdctl --endpoints=$ENDPOINTS put key myvalue
  2. etcdctl --endpoints=$ENDPOINTS put k2 value2
  3. etcdctl --endpoints=$ENDPOINTS del k --prefix
  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

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

  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列出集群成员的命令。

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)

文章参考: