docker_practice/etcd/etcdctl.md

151 lines
5.2 KiB
Go
Raw Normal View History

# 使用 etcdctl
2014-12-31 09:11:09 +00:00
2017-12-03 05:57:02 +00:00
`etcdctl` 是一个命令行客户端它能提供一些简洁的命令供用户直接跟 `etcd` 服务打交道而无需基于 `HTTP API` 方式这在某些情况下将很方便例如用户对服务进行测试或者手动修改数据库内容我们也推荐在刚接触 `etcd` 时通过 `etcdctl` 命令来熟悉相关的操作这些操作跟 `HTTP API` 实际上是对应的
2014-12-31 09:11:09 +00:00
2019-08-31 14:57:43 +00:00
`etcd` 项目二进制发行包中已经包含了 `etcdctl` 工具没有的话可以从 [github.com/etcd-io/etcd/releases](https://github.com/etcd-io/etcd/releases) 下载。
2014-12-31 09:11:09 +00:00
2017-12-03 05:57:02 +00:00
`etcdctl` 支持如下的命令大体上分为数据库操作和非数据库操作两类后面将分别进行解释
2014-12-31 09:11:09 +00:00
2017-12-03 05:57:02 +00:00
```
2014-12-31 09:11:09 +00:00
NAME:
2017-12-03 05:57:02 +00:00
etcdctl - A simple command line client for etcd3.
2014-12-31 09:11:09 +00:00
USAGE:
2017-12-03 05:57:02 +00:00
etcdctl
2014-12-31 09:11:09 +00:00
VERSION:
2019-08-31 14:57:43 +00:00
3.4.0
2017-12-03 05:57:02 +00:00
API VERSION:
2019-08-31 14:57:43 +00:00
3.4
2017-12-03 05:57:02 +00:00
2014-12-31 09:11:09 +00:00
COMMANDS:
2017-12-03 05:57:02 +00:00
get Gets the key or a range of keys
put Puts the given key into the store
del Removes the specified key or range of keys [key, range_end)
txn Txn processes all the requests in one transaction
compaction Compacts the event history in etcd
alarm disarm Disarms all alarms
alarm list Lists all alarms
defrag Defragments the storage of the etcd members with given endpoints
endpoint health Checks the healthiness of endpoints specified in `--endpoints` flag
endpoint status Prints out the status of endpoints specified in `--endpoints` flag
watch Watches events stream on keys or prefixes
version Prints the version of etcdctl
lease grant Creates leases
lease revoke Revokes leases
lease timetolive Get lease information
lease keep-alive Keeps leases alive (renew)
member add Adds a member into the cluster
member remove Removes a member from the cluster
member update Updates a member in the cluster
member list Lists all members in the cluster
snapshot save Stores an etcd node backend snapshot to a given file
snapshot restore Restores an etcd member snapshot to an etcd directory
snapshot status Gets backend snapshot status of a given file
make-mirror Makes a mirror at the destination etcd cluster
migrate Migrates keys in a v2 store to a mvcc store
lock Acquires a named lock
elect Observes and participates in leader election
auth enable Enables authentication
auth disable Disables authentication
user add Adds a new user
user delete Deletes a user
user get Gets detailed information of a user
user list Lists all users
user passwd Changes password of user
user grant-role Grants a role to a user
user revoke-role Revokes a role from a user
role add Adds a new role
role delete Deletes a role
role get Gets detailed information of a role
role list Lists all roles
role grant-permission Grants a key to a role
role revoke-permission Revokes a key from a role
check perf Check the performance of the etcd cluster
help Help about any command
OPTIONS:
--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)
2014-12-31 09:11:09 +00:00
```
## 数据库操作
2017-12-03 05:57:02 +00:00
2014-12-31 09:11:09 +00:00
数据库操作围绕对键值和目录的 CRUD 符合 REST 风格的一套操作Create完整生命周期的管理
etcd 在键的组织上采用了层次化的空间结构类似于文件系统中目录的概念用户指定的键可以为单独的名字 `testkey`此时实际上放在根目录 `/` 下面也可以为指定目录结构 `cluster1/node2/testkey`则将创建相应的目录结构
2017-12-03 05:57:02 +00:00
>CRUD Create, Read, Update, Delete是符合 REST 风格的一套 API 操作
2014-12-31 09:11:09 +00:00
### put
2014-12-31 09:11:09 +00:00
2017-11-22 03:13:23 +00:00
```bash
2017-12-03 05:57:02 +00:00
$ etcdctl put /testdir/testkey "Hello world"
OK
2014-12-31 09:11:09 +00:00
```
### get
2014-12-31 09:11:09 +00:00
2017-12-03 05:57:02 +00:00
获取指定键的值例如
2014-12-31 09:11:09 +00:00
2017-11-22 03:13:23 +00:00
```bash
2017-12-03 05:57:02 +00:00
$ etcdctl put testkey hello
OK
$ etcdctl get testkey
testkey
2014-12-31 09:11:09 +00:00
hello
```
支持的选项为
2017-12-03 05:57:02 +00:00
`--sort` 对结果进行排序
2014-12-31 09:11:09 +00:00
2017-12-03 05:57:02 +00:00
`--consistent` 将请求发给主节点保证获取内容的一致性
2014-12-31 09:11:09 +00:00
### del
2014-12-31 09:11:09 +00:00
2017-12-03 05:57:02 +00:00
删除某个键值例如
2014-12-31 09:11:09 +00:00
2017-11-22 03:13:23 +00:00
```bash
2017-12-03 05:57:02 +00:00
$ etcdctl del testkey
1
2014-12-31 09:11:09 +00:00
```
## 非数据库操作
2014-12-31 09:11:09 +00:00
### watch
2017-12-03 05:57:02 +00:00
2017-12-03 06:04:03 +00:00
监测一个键值的变化一旦键值发生更新就会输出最新的值
2014-12-31 09:11:09 +00:00
2017-12-03 10:53:35 +00:00
例如用户更新 `testkey` 键值为 `Hello world`
2017-12-03 05:57:02 +00:00
2017-11-22 03:13:23 +00:00
```bash
2014-12-31 09:11:09 +00:00
$ etcdctl watch testkey
2017-12-03 06:04:03 +00:00
PUT
testkey
2
2014-12-31 09:11:09 +00:00
```
### member
2017-12-03 05:57:02 +00:00
通过 `list``add``update``remove` 命令列出添加更新删除 etcd 实例到 etcd 集群中
2014-12-31 09:11:09 +00:00
2017-12-03 10:53:35 +00:00
例如本地启动一个 `etcd` 服务实例后可以用如下命令进行查看
2017-12-03 05:57:02 +00:00
2017-11-22 03:13:23 +00:00
```bash
2014-12-31 09:11:09 +00:00
$ etcdctl member list
2017-12-03 05:57:02 +00:00
422a74f03b622fef, started, node1, http://172.16.238.100:2380, http://172.16.238.100:23
2014-12-31 09:11:09 +00:00
```