- 使用 etcdctl
- 数据库操作
- put
- get
- del
- 非数据库操作
- watch
- member
- 数据库操作
使用 etcdctl
etcdctl 是一个命令行客户端,它能提供一些简洁的命令,供用户直接跟 etcd 服务打交道,而无需基于 HTTP API 方式。这在某些情况下将很方便,例如用户对服务进行测试或者手动修改数据库内容。我们也推荐在刚接触 etcd 时通过 etcdctl 命令来熟悉相关的操作,这些操作跟 HTTP API 实际上是对应的。
etcd 项目二进制发行包中已经包含了 etcdctl 工具,没有的话,可以从 github.com/coreos/etcd/releases 下载。
etcdctl 支持如下的命令,大体上分为数据库操作和非数据库操作两类,后面将分别进行解释。
NAME:etcdctl - A simple command line client for etcd3.USAGE:etcdctlVERSION:3.2.10API VERSION:3.2COMMANDS:get Gets the key or a range of keysput Puts the given key into the storedel Removes the specified key or range of keys [key, range_end)txn Txn processes all the requests in one transactioncompaction Compacts the event history in etcdalarm disarm Disarms all alarmsalarm list Lists all alarmsdefrag Defragments the storage of the etcd members with given endpointsendpoint health Checks the healthiness of endpoints specified in `--endpoints` flagendpoint status Prints out the status of endpoints specified in `--endpoints` flagwatch Watches events stream on keys or prefixesversion Prints the version of etcdctllease grant Creates leaseslease revoke Revokes leaseslease timetolive Get lease informationlease keep-alive Keeps leases alive (renew)member add Adds a member into the clustermember remove Removes a member from the clustermember update Updates a member in the clustermember list Lists all members in the clustersnapshot save Stores an etcd node backend snapshot to a given filesnapshot restore Restores an etcd member snapshot to an etcd directorysnapshot status Gets backend snapshot status of a given filemake-mirror Makes a mirror at the destination etcd clustermigrate Migrates keys in a v2 store to a mvcc storelock Acquires a named lockelect Observes and participates in leader electionauth enable Enables authenticationauth disable Disables authenticationuser add Adds a new useruser delete Deletes a useruser get Gets detailed information of a useruser list Lists all usersuser passwd Changes password of useruser grant-role Grants a role to a useruser revoke-role Revokes a role from a userrole add Adds a new rolerole delete Deletes a rolerole get Gets detailed information of a rolerole list Lists all rolesrole grant-permission Grants a key to a rolerole revoke-permission Revokes a key from a rolecheck perf Check the performance of the etcd clusterhelp Help about any commandOPTIONS:--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--endpoints=[127.0.0.1:2379] gRPC endpoints--hex[=false] print byte strings as hex encoded strings--insecure-skip-tls-verify[=false] skip server certificate verification--insecure-transport[=true] disable transport security for client connections--key="" identify secure client using this TLS key file--user="" username[:password] for authentication (prompt if password is not supplied)-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
$ etcdctl put /testdir/testkey "Hello world"OK
get
获取指定键的值。例如
$ etcdctl put testkey helloOK$ etcdctl get testkeytestkeyhello
支持的选项为
--sort 对结果进行排序
--consistent 将请求发给主节点,保证获取内容的一致性
del
删除某个键值。例如
$ etcdctl del testkey1
非数据库操作
watch
监测一个键值的变化,一旦键值发生更新,就会输出最新的值。
例如,用户更新 testkey 键值为 Hello world。
$ etcdctl watch testkeyPUTtestkey2
member
通过 list、add、update、remove 命令列出、添加、更新、删除 etcd 实例到 etcd 集群中。
例如本地启动一个 etcd 服务实例后,可以用如下命令进行查看。
$ etcdctl member list422a74f03b622fef, started, node1, http://172.16.238.100:2380, http://172.16.238.100:23
