May 22, 2021 Docker From entry to practice
etcdctl is a command-line client that provides concise commands for users to work directly with the etcd service without having to do so based on http API. T his can be convenient in some cases, such as when a user tests a service or manually modifies the contents of a database. We also recommend familiarising yourself with the etcdctl command when you first come into contact with etcd, which actually correspond to the HTTP API.
The etcdctl tool is already included in the etcd project binary distribution package and, if not, can be downloaded github.com/coreos/etcd/releases program.
etcdctl supports the following commands, which are broadly divided into database operations and non-database operations, which are explained separately later.
$ etcdctl -h
NAME:
etcdctl - A simple command line client for etcd.
USAGE:
etcdctl [global options] command [command options] [arguments...]
VERSION:
2.0.0-rc.1
COMMANDS:
backup backup an etcd directory
mk make a new key with a given value
mkdir make a new directory
rm remove a key
rmdir removes the key if it is an empty directory or a key-value pair
get retrieve the value of a key
ls retrieve a directory
set set the value of a key
setdir create a new or existing directory
update update an existing key with a given value
updatedir update an existing directory
watch watch a key for changes
exec-watch watch a key for changes and exec an executable
member member add, remove and list subcommands
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--debug output cURL commands which can be used to reproduce the request
--no-sync don't synchronize cluster information before sending request
--output, -o 'simple' output response in the given format (`simple` or `json`)
--peers, -C a comma-delimited list of machine addresses in the cluster (default: "127.0.0.1:4001")
--cert-file identify HTTPS client using this SSL certificate file
--key-file identify HTTPS client using this SSL key file
--ca-file verify certificates of HTTPS-enabled servers using this CA bundle
--help, -h show help
--version, -v print the version
Database operations revolve around the management of the full lifecycle of the CRUD (REST-style set of operations: Create) for key values and directories.
Etcd uses a hierarchical spatial structure (similar to the concept of a directory in a file system) on the organization of keys, where the user-specified key can be a separate name,
testkey
which is actually
/
assigned to a
cluster1/node2/testkey
which creates the appropriate directory structure.
Note: CRUD is Create, Read, Update, Delete, and is a SETH-style set of API actions.
Specifies the value of a key. For example
$ etcdctl set /testdir/testkey "Hello world"
Hello world
Supported options include:
--ttl '0' 该键值的超时时间(单位为秒),不配置(默认为 0)则永不超时
--swap-with-value value 若该键现在的值是 value,则进行设置操作
--swap-with-index '0' 若该键现在的索引值是指定索引,则进行设置操作
Gets the value of the specified key. For example
$ etcdctl set testkey hello
hello
$ etcdctl update testkey world
world
When the key does not exist, an error is reported. For example
$ etcdctl get testkey2
Error: 100: Key not found (/testkey2) [1]
The supported options are
--sort 对结果进行排序
--consistent 将请求发给主节点,保证获取内容的一致性
When the key is present, update the value content. For example
$ etcdctl set testkey hello
hello
$ etcdctl update testkey world
world
When the key does not exist, an error is reported. For example
$ etcdctl update testkey2 world
Error: 100: Key not found (/testkey2) [1]
The supported options are
--ttl '0' 超时时间(单位为秒),不配置(默认为 0)则永不超时
Delete a key value. For example
$ etcdctl rm testkey
When the key does not exist, an error is reported. For example
$ etcdctl rm testkey2
Error: 100: Key not found (/testkey2) [8]
The supported options are
--dir 如果键是个空目录或者键值对则删除
--recursive 删除目录和所有子键
--with-value 检查现有的值是否匹配
--with-index '0' 检查现有的 index 是否匹配
If a given key does not exist, a new key value is created. For example
$ etcdctl mk /testdir/testkey "Hello world"
Hello world
When a key exists, executing the command will report an error, for example
$ etcdctl set testkey "Hello world"
Hello world
$ ./etcdctl mk testkey "Hello world"
Error: 105: Key already exists (/testkey) [2]
The supported options are
--ttl '0' 超时时间(单位为秒),不配置(默认为 0)则永不超时
If the given key directory does not exist, a new key directory is created. For example
$ etcdctl mkdir testdir
When a key directory exists, executing the command will report an error, for example
$ etcdctl mkdir testdir
$ etcdctl mkdir testdir
Error: 105: Key already exists (/testdir) [7]
The supported options are
--ttl '0' 超时时间(单位为秒),不配置(默认为 0)则永不超时
Create a key directory, whether it exists or not.
The supported options are
--ttl '0' 超时时间(单位为秒),不配置(默认为 0)则永不超时
Update a directory that already exists. The supported options are
--ttl '0' 超时时间(单位为秒),不配置(默认为 0)则永不超时
Delete an empty directory, or a key value pair.
If the directory is not empty, an error will be reported
$ etcdctl set /dir/testkey hi
hi
$ etcdctl rmdir /dir
Error: 108: Directory not empty (/dir) [13]
Lists the keys or subdirectdirecters under the directory (the default is the root), and the contents of the subdirectage are not displayed by default.
For example
$ ./etcdctl set testkey 'hi'
hi
$ ./etcdctl set dir/test 'hello'
hello
$ ./etcdctl ls
/testkey
/dir
$ ./etcdctl ls dir
/dir/test
Supported options are included
--sort 将输出结果排序
--recursive 如果目录下有子目录,则递归输出其中的内容
-p 对于输出为目录,在最后添加 `/` 进行区分
Back up etcd's data.
Supported options are included
--data-dir etcd 的数据目录
--backup-dir 备份到指定路径
Monitors the change of a key value, and once the key value is updated, the latest value is output and exited.
For example, the user updates the testkey key value to Hello world.
$ etcdctl watch testkey
Hello world
Supported options are included
--forever 一直监测,直到用户按 `CTRL+C` 退出
--after-index '0' 在指定 index 之前一直监测
--recursive 返回所有的键值和子键值
Monitors a change in a key value and executes a given command as soon as the key value is updated.
For example, the user updates the testkey key value.
$etcdctl exec-watch testkey -- sh -c 'ls'
default.etcd
Documentation
etcd
etcdctl
etcd-migrate
README-etcdctl.md
README.md
Supported options are included
--after-index '0' 在指定 index 之前一直监测
--recursive 返回所有的键值和子键值
List, add, and delete etcd instances into the etcd cluster through the list, add, remove commands.
For example, after you start an etcd service instance locally, you can view it with the following command.
$ etcdctl member list
ce2a822cea30bfca: name=default peerURLs=http://localhost:2380,http://localhost:7001 clientURLs=http://localhost:2379,http://localhost:4001
--debug
the cURL command, which shows the request that was initiated when the command was executed
--no-sync
of sync cluster information before making a request
--output, -o 'simple'
output format
simple
for raw information,
json
for json format decoding, readability is better)
--peers, -C
peer information in the cluster, separated by a comma (default: "127.0.0.1:4001")
--cert-file
The SSL certificate file used by the client under HTTPS
--key-file
The SSL key file used by the client under HTTPS
--ca-file
uses CA files for validation when using HTTPS
--help, -h
help command information
--version, -v
print version information