默认安装自带etcdctl 命令行客户端,分两个版本ETCDCTL_API=2和ETCDCTL_API=3,两个版本不一样,操作的数据也不相容。
本文以v3 为例。
使用之前需要先设置:export ETCDCTL_API=3。
1 etcd查询集群节点列表及状态
标准输出:
$ etcdctl member list
1c70f9bbb41018f, started, my-etcd-1, http://0.0.0.0:2380, http://0.0.0.0:2379, false
table输出;
$ etcdctl member list -w table
+-----------------+---------+-----------+---------------------+---------------------+------------+
| ID | STATUS | NAME | PEER ADDRS | CLIENT ADDRS | IS LEARNER |
+-----------------+---------+-----------+---------------------+---------------------+------------+
| 1c70f9bbb41018f | started | my-etcd-1 | http://0.0.0.0:2380 | http://0.0.0.0:2379 | false |
+-----------------+---------+-----------+---------------------+---------------------+------------+
2 集群节点健康状态查询
标准输出:
$ etcdctl endpoint health
127.0.0.1:2379 is healthy: successfully committed proposal: took = 10.064875ms
table输出
$ etcdctl endpoint health -w table
+----------------+--------+-------------+-------+
| ENDPOINT | HEALTH | TOOK | ERROR |
+----------------+--------+-------------+-------+
| 127.0.0.1:2379 | true | 18.411292ms | |
+----------------+--------+-------------+-------+
3 help 查看所有命令集
$ etcdctl help
NAME:etcdctl - A simple command line client for etcd3.USAGE:etcdctl [flags]VERSION:3.5.16API VERSION:3.5COMMANDS:alarm disarm Disarms all alarmsalarm list Lists all alarmsauth disable Disables authenticationauth enable Enables authenticationauth status Returns authentication statuscheck datascale Check the memory usage of holding data for different workloads on a given server endpoint.check perf Check the performance of the etcd clustercompaction Compacts the event history in etcddefrag Defragments the storage of the etcd members with given endpointsdel Removes the specified key or range of keys [key, range_end)elect Observes and participates in leader electionendpoint hashkv Prints the KV history hash for each endpoint in --endpointsendpoint health Checks the healthiness of endpoints specified in `--endpoints` flagendpoint status Prints out the status of endpoints specified in `--endpoints` flagget Gets the key or a range of keyshelp Help about any commandlease grant Creates leaseslease keep-alive Keeps leases alive (renew)lease list List all active leaseslease revoke Revokes leaseslease timetolive Get lease informationlock Acquires a named lockmake-mirror Makes a mirror at the destination etcd clustermember add Adds a member into the clustermember list Lists all members in the clustermember promote Promotes a non-voting member in the clustermember remove Removes a member from the clustermember update Updates a member in the clustermove-leader Transfers leadership to another etcd cluster member.put Puts the given key into the storerole add Adds a new rolerole delete Deletes a rolerole get Gets detailed information of a rolerole grant-permission Grants a key to a rolerole list Lists all rolesrole revoke-permission Revokes a key from a rolesnapshot restore Restores an etcd member snapshot to an etcd directorysnapshot save Stores an etcd node backend snapshot to a given filesnapshot status [deprecated] Gets backend snapshot status of a given filetxn Txn processes all the requests in one transactionuser add Adds a new useruser delete Deletes a useruser get Gets detailed information of a useruser grant-role Grants a role to a useruser list Lists all usersuser passwd Changes password of useruser revoke-role Revokes a role from a userversion Prints the version of etcdctlwatch Watches events stream on keys or prefixesOPTIONS:--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-d, --discovery-srv="" domain name to query for SRV records describing cluster endpoints--discovery-srv-name="" service name to query when using DNS discovery--endpoints=[127.0.0.1:2379] gRPC endpoints-h, --help[=false] help for etcdctl--hex[=false] print byte strings as hex encoded strings--insecure-discovery[=true] accept insecure SRV records describing cluster endpoints--insecure-skip-tls-verify[=false] skip server certificate verification (CAUTION: this option should be enabled only for testing purposes)--insecure-transport[=true] disable transport security for client connections--keepalive-time=2s keepalive time for client connections--keepalive-timeout=6s keepalive timeout for client connections--key="" identify secure client using this TLS key file--password="" password for authentication (if this option is used, --user option shouldn't include password)--user="" username[:password] for authentication (prompt if password is not supplied)-w, --write-out="simple" set the output format (fields, json, protobuf, simple, table)
4 put 创建键值对
etcdctl put /test1 value1 #设置test1=value1
etcdctl put /test1 value2 # 存在直接覆盖etcdctl put /test1 value3 --lease=1234abc #绑定租赁id,租赁时间到期自动删除
etcdctl put /test1 value4 --ignore-lease # 使用当前key的租赁
etcdctl put /test1 value5 --prev-kv #设置值,并返回上一个版本的值
5 get查看键值对
etcdctl get /test1 #获取key=/test1的值,返回key和value
etcdctl get "" --from-key #返回比空字符串大的key的键值对,该例返回所有数据
etcdctl get "dir" --prefix --keys-only --limit=5 # 返回"dir" 前缀的键最多5个
etcdctl get /test1 /test4 #获取[/test1,/test4) 范围的键值对,左闭右开,不包括/test4
etcdctl get /test1 --print-value-only #只返回值
etcdctl get /test1 --consistency='l' #获取key=/test1的键值对,consistency 'l' 代表线性读(执行raft), 's' 代表串行化读etcdctl get /test1 -w json #查看key=/test1的版本数据,返回数据如下:
#cluster_id:请求的etcd集群ID。
#member_id:请求的etcd节点ID。
#revision:etcd服务端当前全局数据版本号。对任一key的put或delete操作都会使#revision自增1。revision=1是etcd的保留版本号,因此用户的key版本号将从2开始。
#raft_term:etcd当前raft主节点任期号。
#create_revision:当前key创建时全局数据版本号revision的值。
#mod_revision:当前key最后一次修改时全局数据版本号revision的值。
#version:当前key的数据版本号。key创建时version为1,对当前key进行put操作会使version自增1,将key删除后,重新创建,version又会从1开始计数
6 del删除键值对
etcdctl del /test1 #删除指定key
etcdctl del /test1 --prev-kv #返回删除的键值对
etcdctl del /test --from-key # 删除比/test大的所有key,也支持--prefix 按前缀删除
7 watch监听键值对
etcdctl watch foo -- echo watch event received #watch key=foo 输出指定文字
etchctl watch foo --rev=2 #从2版本开始watch,返回中间所有修改的版本
etcdctl watch foo -- sh -c "env | grep ETCD_WATCH_"
# PUT
# foo
# bar
# ETCD_WATCH_REVISION=11
# ETCD_WATCH_KEY="foo"
# ETCD_WATCH_EVENT_TYPE="PUT"
# ETCD_WATCH_VALUE="bar"
8 lease 命令(租约)
etcdctl lease grant 10 #创建一个10s的租约,返回租约的id,用于绑定key
etcdctl lease revoke 1234abc #删除id=1234abc 的租约
etcdctl lease timetolive 1234abc #查看id=1234abc 的租约剩余时间
etcdctl lease timetolive --keys 1234abc #查看id=1234abc 的租约剩余时间,并显示关联的key
etcdctl lease keep-alive -- once 1234abc #一次续约id=1234abc 的租约,并退出,不加--once 命令挂起,到期自动续约
etcdctl lease list #查看所有激活的租期
9 member 命令(集群成员管理)
etcdctl member add newMember --peer-urls=https://127.0.0.1:12345Member ced000fda4d05edf added to cluster 8c4281cc65c7b112ETCD_NAME="newMember"
ETCD_INITIAL_CLUSTER="newMember=https://127.0.0.1:12345,default=http://10.0.0.30:2380"
ETCD_INITIAL_CLUSTER_STATE="existing"etcdctl member update 2be1eb8f84b7f63e --peer-urls=https://127.0.0.1:11112
etcdctl member remove 2be1eb8f84b7f63e 删除成员
etcdctl member list #查看集群列表
10 endpoint 查看节点状态
etcdctl endpoint status -w table #查看默认节点状态,table格式输出
etcdctl endpoint --cluster status -w table # 查看集群状态
etcdctl endpoint --cluster hashkv -w json #查看集群hashkv