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

数据库操作

数据库操作围绕对键值和目录的 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
Copied!

get

获取指定键的值。例如
1
$ etcdctl put testkey hello
2
OK
3
$ etcdctl get testkey
4
testkey
5
hello
Copied!
支持的选项为
--sort 对结果进行排序
--consistent 将请求发给主节点,保证获取内容的一致性

del

删除某个键值。例如
1
$ etcdctl del testkey
2
1
Copied!

非数据库操作

watch

监测一个键值的变化,一旦键值发生更新,就会输出最新的值。
例如,用户更新 testkey 键值为 Hello world
1
$ etcdctl watch testkey
2
PUT
3
testkey
4
2
Copied!

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
Copied!
复制链接
在 GitHub 上编辑