使用 etcdctl

etcdctl 是一个命令行客户端,它能提供一些简洁的命令,供用户直接跟 etcd 服务打交道,而无需基于 HTTP API 方式。这在某些情况下将很方便,例如用户对服务进行测试或者手动修改数据库内容。我们也推荐在刚接触 etcd 时通过 etcdctl 命令来熟悉相关的操作,这些操作跟 HTTP API 实际上是对应的。

etcd 项目二进制发行包中已经包含了 etcdctl 工具,没有的话,可以从 github.com/etcd-io/etcd/releases 下载。

etcdctl 支持如下的命令,大体上分为数据库操作和非数据库操作两类,后面将分别进行解释。

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

数据库操作

数据库操作围绕对键值和目录的 CRUD (符合 REST 风格的一套操作:Create)完整生命周期的管理。

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

注:CRUD 即 Create, Read, Update, Delete,是符合 REST 风格的一套 API 操作。

put

  1. $ etcdctl put /testdir/testkey "Hello world"
  2. OK

get

获取指定键的值。例如

  1. $ etcdctl put testkey hello
  2. OK
  3. $ etcdctl get testkey
  4. testkey
  5. hello

支持的选项为

--sort 对结果进行排序

--consistent 将请求发给主节点,保证获取内容的一致性

del

删除某个键值。例如

  1. $ etcdctl del testkey
  2. 1

非数据库操作

watch

监测一个键值的变化,一旦键值发生更新,就会输出最新的值。

例如,用户更新 testkey 键值为 Hello world

  1. $ etcdctl watch testkey
  2. PUT
  3. testkey
  4. 2

member

通过 listaddupdateremove 命令列出、添加、更新、删除 etcd 实例到 etcd 集群中。

例如本地启动一个 etcd 服务实例后,可以用如下命令进行查看。

  1. $ etcdctl member list
  2. 422a74f03b622fef, started, node1, http://172.16.238.100:2380, http://172.16.238.100:23