- 1. etcdctl的安装
- 2. etcdctl V3
- 3. etcdctl 常用命令
- 3.1. 指定etcd集群
- 3.2. 增删改查
- 3.3. 集群状态
- 3.4. 集群成员
- 4. etcdctl get
etcdctl的v3版本与v2版本使用命令有所不同,本文介绍etcdctl v3版本的命令工具的使用方式。
1. etcdctl的安装
etcdctl的二进制文件可以在 github.com/coreos/etcd/releases 选择对应的版本下载,例如可以执行以下install_etcdctl.sh的脚本,修改其中的版本信息。
#!/bin/bashETCD_VER=v3.3.4ETCD_DIR=etcd-downloadDOWNLOAD_URL=https://github.com/coreos/etcd/releases/download# Downloadmkdir ${ETCD_DIR}cd ${ETCD_DIR}wget ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gztar -xzvf etcd-${ETCD_VER}-linux-amd64.tar.gz# installcd etcd-${ETCD_VER}-linux-amd64cp etcdctl /usr/local/bin/
2. etcdctl V3
使用etcdctlv3的版本时,需设置环境变量ETCDCTL_API=3。
export ETCDCTL_API=3或者在`/etc/profile`文件中添加环境变量vi /etc/profile...ETCDCTL_API=3...source /etc/profile
查看当前etcdctl的版本信息etcdctl version。
[root@k8s-dbg-master-1 etcd]# etcdctl versionetcdctl version: 3.3.4API version: 3.3
更多命令帮助可以查询etcdctl —help。
[root@k8s-dbg-master-1 etcd]# etcdctl --helpNAME:etcdctl - A simple command line client for etcd3.USAGE:etcdctlVERSION:3.3.4API VERSION:3.3COMMANDS:get Gets the key or a range of keysput Puts the given key into the storedel Removes the specified key or range of keys [key, range_end)txn Txn processes all the requests in one transactioncompaction Compacts the event history in etcdalarm disarm Disarms all alarmsalarm list Lists all alarmsdefrag Defragments the storage of the etcd members with given endpointsendpoint health Checks the healthiness of endpoints specified in `--endpoints` flagendpoint status Prints out the status of endpoints specified in `--endpoints` flagendpoint hashkv Prints the KV history hash for each endpoint in --endpointsmove-leader Transfers leadership to another etcd cluster member.watch Watches events stream on keys or prefixesversion Prints the version of etcdctllease grant Creates leaseslease revoke Revokes leaseslease timetolive Get lease informationlease list List all active leaseslease keep-alive Keeps leases alive (renew)member add Adds a member into the clustermember remove Removes a member from the clustermember update Updates a member in the clustermember list Lists all members in the clustersnapshot save Stores an etcd node backend snapshot to a given filesnapshot restore Restores an etcd member snapshot to an etcd directorysnapshot status Gets backend snapshot status of a given filemake-mirror Makes a mirror at the destination etcd clustermigrate Migrates keys in a v2 store to a mvcc storelock Acquires a named lockelect Observes and participates in leader electionauth enable Enables authenticationauth disable Disables authenticationuser add Adds a new useruser delete Deletes a useruser get Gets detailed information of a useruser list Lists all usersuser passwd Changes password of useruser grant-role Grants a role to a useruser revoke-role Revokes a role from a userrole add Adds a new rolerole delete Deletes a rolerole get Gets detailed information of a rolerole list Lists all rolesrole grant-permission Grants a key to a rolerole revoke-permission Revokes a key from a rolecheck perf Check the performance of the etcd clusterhelp Help about any commandOPTIONS:--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--endpoints=[127.0.0.1:2379] gRPC endpoints--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--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--user="" username[:password] for authentication (prompt if password is not supplied)-w, --write-out="simple" set the output format (fields, json, protobuf, simple, table)
3. etcdctl 常用命令
3.1. 指定etcd集群
HOST_1=10.240.0.17HOST_2=10.240.0.18HOST_3=10.240.0.19ENDPOINTS=$HOST_1:2379,$HOST_2:2379,$HOST_3:2379etcdctl --endpoints=$ENDPOINTS member list
3.2. 增删改查
1、增
etcdctl --endpoints=$ENDPOINTS put foo "Hello World!"
2、查
etcdctl --endpoints=$ENDPOINTS get fooetcdctl --endpoints=$ENDPOINTS --write-out="json" get foo
基于相同前缀查找
etcdctl --endpoints=$ENDPOINTS put web1 value1etcdctl --endpoints=$ENDPOINTS put web2 value2etcdctl --endpoints=$ENDPOINTS put web3 value3etcdctl --endpoints=$ENDPOINTS get web --prefix
列出所有的key
etcdctl --endpoints=$ENDPOINTS get / --prefix --keys-only
3、删**
etcdctl --endpoints=$ENDPOINTS put key myvalueetcdctl --endpoints=$ENDPOINTS del keyetcdctl --endpoints=$ENDPOINTS put k1 value1etcdctl --endpoints=$ENDPOINTS put k2 value2etcdctl --endpoints=$ENDPOINTS del k --prefix
3.3. 集群状态
集群状态主要是etcdctl endpoint status 和etcdctl endpoint health两条命令。
etcdctl --write-out=table --endpoints=$ENDPOINTS endpoint status+------------------+------------------+---------+---------+-----------+-----------+------------+| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | RAFT TERM | RAFT INDEX |+------------------+------------------+---------+---------+-----------+-----------+------------+| 10.240.0.17:2379 | 4917a7ab173fabe7 | 3.0.0 | 45 kB | true | 4 | 16726 || 10.240.0.18:2379 | 59796ba9cd1bcd72 | 3.0.0 | 45 kB | false | 4 | 16726 || 10.240.0.19:2379 | 94df724b66343e6c | 3.0.0 | 45 kB | false | 4 | 16726 |+------------------+------------------+---------+---------+-----------+-----------+------------+etcdctl --endpoints=$ENDPOINTS endpoint health10.240.0.17:2379 is healthy: successfully committed proposal: took = 3.345431ms10.240.0.19:2379 is healthy: successfully committed proposal: took = 3.767967ms10.240.0.18:2379 is healthy: successfully committed proposal: took = 4.025451ms
3.4. 集群成员
跟集群成员相关的命令如下:
member add Adds a member into the clustermember remove Removes a member from the clustermember update Updates a member in the clustermember list Lists all members in the cluster
例如 etcdctl member list列出集群成员的命令。
etcdctl --endpoints=http://172.16.5.4:12379 member list -w table+-----------------+---------+-------+------------------------+-----------------------------------------------+| ID | STATUS | NAME | PEER ADDRS | CLIENT ADDRS |+-----------------+---------+-------+------------------------+-----------------------------------------------+| c856d92a82ba66a | started | etcd0 | http://172.16.5.4:2380 | http://172.16.5.4:2379,http://172.16.5.4:4001 |+-----------------+---------+-------+------------------------+-----------------------------------------------+
4. etcdctl get
使用etcdctl {command} --help可以查看具体命令的帮助信息。
# etcdctl get --helpNAME:get - Gets the key or a range of keysUSAGE:etcdctl get [options] <key> [range_end]OPTIONS:--consistency="l" Linearizable(l) or Serializable(s)--from-key[=false] Get keys that are greater than or equal to the given key using byte compare--keys-only[=false] Get only the keys--limit=0 Maximum number of results--order="" Order of results; ASCEND or DESCEND (ASCEND by default)--prefix[=false] Get keys with matching prefix--print-value-only[=false] Only write values when using the "simple" output format--rev=0 Specify the kv revision--sort-by="" Sort target; CREATE, KEY, MODIFY, VALUE, or VERSIONGLOBAL 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)
文章参考:
https://coreos.com/etcd/docs/latest/demo.html
