etcd命令大全

发布于:2024-12-18 ⋅ 阅读:(93) ⋅ 点赞:(0)

默认安装自带etcdctl 命令行客户端,分两个版本ETCDCTL_API=2和ETCDCTL_API=3,两个版本不一样,操作的数据也不相容。

本文以v3 为例。

使用之前需要先设置:export ETCDCTL_API=3。

etcd配置

配置环境变量

为了简化命令行操作,建议配置环境变量,以便 etcdctl 能够自动识别 etcd 服务器的地址和相关参数。

export ETCDCTL_API=3
export ETCDCTL_ENDPOINTS="http://192.168.5.41:2379,http://192.168.5.45:2379,http://192.168.5.46:2379"

参数说明

  • ETCDCTL_API:指定使用 etcd API 的版本,当前推荐使用版本 3。
  • ETCDCTL_ENDPOINTS:指定 etcd 服务器的客户端访问地址,可以配置多个端点以实现高可用性。

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.16

API VERSION:
	3.5


COMMANDS:
	alarm disarm		Disarms all alarms
	alarm list		Lists all alarms
	auth disable		Disables authentication
	auth enable		Enables authentication
	auth status		Returns authentication status
	check datascale		Check the memory usage of holding data for different workloads on a given server endpoint.
	check perf		Check the performance of the etcd cluster
	compaction		Compacts the event history in etcd
	defrag			Defragments the storage of the etcd members with given endpoints
	del			Removes the specified key or range of keys [key, range_end)
	elect			Observes and participates in leader election
	endpoint hashkv		Prints the KV history hash for each endpoint in --endpoints
	endpoint health		Checks the healthiness of endpoints specified in `--endpoints` flag
	endpoint status		Prints out the status of endpoints specified in `--endpoints` flag
	get			Gets the key or a range of keys
	help			Help about any command
	lease grant		Creates leases
	lease keep-alive	Keeps leases alive (renew)
	lease list		List all active leases
	lease revoke		Revokes leases
	lease timetolive	Get lease information
	lock			Acquires a named lock
	make-mirror		Makes a mirror at the destination etcd cluster
	member add		Adds a member into the cluster
	member list		Lists all members in the cluster
	member promote		Promotes a non-voting member in the cluster
	member remove		Removes a member from the cluster
	member update		Updates a member in the cluster
	move-leader		Transfers leadership to another etcd cluster member.
	put			Puts the given key into the store
	role add		Adds a new role
	role delete		Deletes a role
	role get		Gets detailed information of a role
	role grant-permission	Grants a key to a role
	role list		Lists all roles
	role revoke-permission	Revokes a key from a role
	snapshot restore	Restores an etcd member snapshot to an etcd directory
	snapshot save		Stores an etcd node backend snapshot to a given file
	snapshot status		[deprecated] Gets backend snapshot status of a given file
	txn			Txn processes all the requests in one transaction
	user add		Adds a new user
	user delete		Deletes a user
	user get		Gets detailed information of a user
	user grant-role		Grants a role to a user
	user list		Lists all users
	user passwd		Changes password of user
	user revoke-role	Revokes a role from a user
	version			Prints the version of etcdctl
	watch			Watches events stream on keys or prefixes

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
  -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:12345

Member ced000fda4d05edf added to cluster 8c4281cc65c7b112

ETCD_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

常用使用命令

  1. 存储键值对

    etcdctl put mykey "myvalue"
    
  2. 读取键值对

    etcdctl get mykey
    
  3. 列出所有键值对

    etcdctl get --prefix ""
    
  4. 删除键值对

    etcdctl del mykey
    
  5. 查看 etcd 集群状态

    etcdctl endpoint status
    
  6. 备份 etcd 数据

    etcdctl snapshot save backup.db
    
  7. 恢复 etcd 数据

    etcdctl snapshot restore backup.db --data-dir /path/to/etcd/data-dir
    


网站公告

今日签到

点亮在社区的每一天
去签到