etcdctl使用

转载自胡伟煌的博客

使用etcdctl指令可以操作etcd集群,检查集群状态等

环境变量设置

使用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 version
etcdctl version: 3.3.4
API version: 3.3

更多命令帮助可以查询etcdctl —help。

[root@k8s-dbg-master-1 etcd]# etcdctl --help
NAME:
    etcdctl - A simple command line client for etcd3.

USAGE:
    etcdctl

VERSION:
    3.3.4

API VERSION:
    3.3


COMMANDS:
    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
    endpoint hashkv     Prints the KV history hash for each endpoint in --endpoints
    move-leader     Transfers leadership to another etcd cluster member.
    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 list      List all active leases
    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
  -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)

etcdctl 常用命令

指定etcd集群

HOST_1=10.240.0.17
HOST_2=10.240.0.18
HOST_3=10.240.0.19
ENDPOINTS=$HOST_1:2379,$HOST_2:2379,$HOST_3:2379

增删改查
1、增

etcdctl --endpoints=$ENDPOINTS put foo "Hello World!"

2、查

etcdctl --endpoints=$ENDPOINTS get foo
etcdctl --endpoints=$ENDPOINTS --write-out="json" get foo

基于相同前缀查找

etcdctl --endpoints=$ENDPOINTS put web1 value1
etcdctl --endpoints=$ENDPOINTS put web2 value2
etcdctl --endpoints=$ENDPOINTS put web3 value3
etcdctl --endpoints=$ENDPOINTS get web --prefix

3、删

etcdctl --endpoints=$ENDPOINTS put key myvalue
etcdctl --endpoints=$ENDPOINTS del key

etcdctl --endpoints=$ENDPOINTS put k1 value1
etcdctl --endpoints=$ENDPOINTS put k2 value2
etcdctl --endpoints=$ENDPOINTS del k --prefix

4、监听
watch
监测一个键值的变化,一旦键值发生更新,就会输出最新的值并退出。

例如,用户更新 testkey 键值为 Hello world。

$ etcdctl watch testkey
Hello world

支持的选项包括

  • –forever 一直监测,直到用户按 CTRL+C 退出
  • –after-index ‘0’ 在指定 index 之前一直监测
  • –recursive 返回所有的键值和子键值

exec-watch
监测一个键值的变化,一旦键值发生更新,就执行给定命令。

例如,用户更新 testkey 键值。

$etcdctl exec-watch testkey – sh -c ‘ls’
default.etcd
Documentation
etcd
etcdctl
etcd-migrate
README-etcdctl.md
README.md

支持的选项包括

  • –after-index ‘0’ 在指定 index 之前一直监测
  • –recursive 返回所有的键值和子键值

集群状态
集群状态主要是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 health

10.240.0.17:2379 is healthy: successfully committed proposal: took = 3.345431ms
10.240.0.19:2379 is healthy: successfully committed proposal: took = 3.767967ms
10.240.0.18:2379 is healthy: successfully committed proposal: took = 4.025451ms

集群成员
跟集群成员相关的命令如下:

    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

例如 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 |
+-----------------+---------+-------+------------------------+-----------------------------------------------+
————————————————
### 关于 `etcdctl` 命令行工具 #### 获取帮助信息 为了更好地理解如何使用 `etcdctl` 工具,可以先通过获取其内置的帮助文档来熟悉基本语法和选项。这可以通过执行以下命令完成: ```bash etcdctl --help ``` 此命令会显示所有可用子命令及其描述。 #### 查看集群成员列表 对于管理分布式键值存储系统而言,了解当前集群中的节点状态非常重要。为此目的设计了一个特定的子命令: ```bash etcdctl member list ``` 该指令返回有关Etcd集群中各个成员的信息[^2]。 #### 浏览 Etcd 中的数据结构 当需要探索存储在 Etcd 数据库内的条目时,可利用 `get` 或者更全面地遍历指定前缀路径下的所有键值对: ```bash # 单独检索某项记录 etcdctl get /path/to/key # 列举给定前缀下所有的键值对 etcdctl get /prefix/of/path --prefix ``` 这些操作允许管理员轻松访问保存于 Etcd 的配置或其他元数据。 #### 修改或删除现有条目 除了读取之外,有时也需要更新已有的设置或是清理不再使用的资源。这时就可以借助 put 和 del 子命令来进行相应处理: ```bash # 更新单个键对应的值 etcdctl put /key new_value # 删除指定位置上的项目 etcdctl del /key_to_remove ``` 上述方法提供了简单的方式去维护 Etcd 内部的状态变化。 #### 执行事务控制 Etcd 支持基于条件的操作,即只有满足某些前提条件下才会真正提交更改。这种机制被称为事务(txn),它有助于实现更加复杂的业务逻辑而不必担心并发冲突问题: ```bash etcdctl txn <<EOF IF cmp /some_key value old_val THEN put /some_key new_val ELSE echo "Condition not met" EOF ``` 这段脚本展示了如果 `/some_key` 当前所存的内容等于 `old_val` 就将其替换为 `new_val`; 否则打印提示消息并终止流程[^4]。 #### 监控变更活动 监视 Etcd 上发生的任何改动是一项重要的运维任务。Watch 功能使得应用程序能够实时接收到感兴趣区域的变化通知: ```bash etcdctl watch /interesting_path_or_prefix ``` 一旦目标地址范围内有任何增删改动作发生,则立即触发回调函数向监听端推送最新情况报告。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值