美文网首页
etcd入门之常用命令

etcd入门之常用命令

作者: 生饼 | 来源:发表于2022-08-19 10:44 被阅读0次

1 cluster

# 查看版本
$ ./bin/etcdctl version
etcdctl version: 3.5.0
API version: 3.5

# 通过endpoints参数指定连接的远端etcd节点
$ ./bin/etcdctl --endpoints=http://127.0.0.1:2379 version
etcdctl version: 3.5.0
API version: 3.5

# 查看集群成员
$ ./bin/etcdctl member list
8e9e05c52164694d, started, etcd-cnlab0, http://localhost:2380, http://localhost:2379, false

# 查看集群状态
$ ./bin/etcdctl endpoint status --cluster -w table
+-----------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
|       ENDPOINT        |        ID        | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+-----------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| http://localhost:2379 | 8e9e05c52164694d |   3.5.0 |   20 kB |      true |      false |         2 |          4 |                  4 |        |
+-----------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+

# 查看集群健康情况
$ ./bin/etcdctl endpoint health --cluster -w table
+-----------------------+--------+------------+-------+
|       ENDPOINT        | HEALTH |    TOOK    | ERROR |
+-----------------------+--------+------------+-------+
| http://localhost:2379 |   true | 2.059608ms |       |
+-----------------------+--------+------------+-------+

2 put

$ ./bin/etcdctl put /foo1 "hello world"
$ ./bin/etcdctl put /foo1 100
$ ./bin/etcdctl put /foo3 huhu

3 get

$ ./bin/etcdctl get /foo1
/foo1
hello world

# 只显示值,不显示key
$ ./bin/etcdctl get /foo1 --print-value-only
hello world

# 范围查询,左闭右开 [/foo1, /foo3)
$ ./bin/etcdctl get /foo1 /foo3 --print-value-only
hello world
100

# 范围查询,大于或等于指定key值
$ ./bin/etcdctl get --from-key /foo2 --print-value-only
100
huhu

# 前缀查询
$ ./bin/etcdctl get --prefix /foo --print-value-only
hello world
100
huhu

# 限制返回的结果数量
$ ./bin/etcdctl get --prefix --limit=2 /foo --print-value-only
hello world
100

# 查询key的详细信息。etcd维护了一个全局递增的revision值,每次修改某个key的值就会增加1. 从下面的信息可以看出,key /foo1的最新revision值是7,创建时的revision值是2,共有4个版本(表示写入了4次)
$ ./bin/etcdctl get /foo1 -w=json
{"header":{"cluster_id":14841639068965178418,"member_id":10276657743932975437,"revision":7,"raft_term":3},"kvs":[{"key":"L2ZvbzE=","create_revision":2,"mod_revision":7,"version":4,"value":"Z29vZCBpZGVh"}],"count":1}

# 查看指定revison版本的值
$ ./bin/etcdctl get /foo1 --rev=2 --print-value-only
hello world

4 del

删除命令与get基本相似

$ ./bin/etcdctl del /foo1
1

# 删除范围
$ ./bin/etcdctl del /foo1 /foo3

# 删除范围,大于或等于指定key值
$ ./bin/etcdctl del --from-key /foo1

# 根据前缀删除
$ ./bin/etcdctl del --prefix /foo1


# 返回删除前的值
$ ./bin/etcdctl del /foo1 --prev-kv
1
/foo1
hell

5 lease

# 创建租约,生命周期为120秒,租约id为694d7c20bfb1c716
$ $ ./bin/etcdctl lease grant 120
lease 694d7c20bfb1c716 granted with TTL(30s)

# 绑定租约
$ ./bin/etcdctl put /foo5 "hello world" --lease=694d82b0f0dfb037
OK

# 查看租约剩余的TTL
$ ./bin/etcdctl lease timetolive 694d82b0f0dfb037
lease 694d82b0f0dfb037 granted with TTL(120s), remaining(42s)

# 查看租约剩余的TTL,同时查看绑定的key
$ ./bin/etcdctl lease timetolive --keys 694d82b0f0dfb037
lease 694d82b0f0dfb037 granted with TTL(120s), remaining(32s), attached keys([/foo5])

# 持续续租,直到CTRL + C退出续租
$ ./bin/etcdctl lease keep-alive 694d82b0f0dfb037
lease 694d82b0f0dfb037 keepalived with TTL(120)
lease 694d82b0f0dfb037 keepalived with TTL(120)


# 撤销租约
$ ./bin/etcdctl revoke 694d7c20bfb1c716

# 租约到期后,绑定的key被删除
$ ./bin/etcdctl get /foo5

6 watch

在一个窗口监听key /foo10

$ ./bin/etcdctl watch /foo10

在另一个窗口修改key /foo10的值

-bash-4.1$ ./bin/etcdctl put /foo10 30
OK
-bash-4.1$ ./bin/etcdctl put /foo10 "hello world"
OK
-bash-4.1$ ./bin/etcdctl del /foo10
1
-bash-4.1$ ./bin/etcdctl lease grant 30
lease 694d82b0f0dfb033 granted with TTL(30s)
-bash-4.1$ ./bin/etcdctl put /foo10 123 --lease=694d82b0f0dfb033
OK

可以观察到监听窗口打印以下key值变化的过程

$ ./bin/etcdctl watch /foo10
PUT
/foo10
30
PUT
/foo10
hello world
DELETE
/foo10

PUT
/foo10
123

# 由租约到期删除key触发
DELETE
/foo10

# 监听范围
$ ./bin/etcdctl watch /foo1 /foo10

# 监听前缀
$ ./bin/etcdctl watch --prefix /foo

相关文章

  • etcd入门之常用命令

    1 cluster 2 put 3 get 4 del 删除命令与get基本相似 5 lease 6 watch ...

  • 每周阅读(1/28/2019)

    etcd 文章:第一篇入门,第二篇如标题所言对于应用场景和实现原理做了全方位解读。 etcd 使用入门 etcd:...

  • etcd入门之jetcd

    java项目可以通过jetcd客户端使用etcd提供的分布式协调服务,让java项目分布式协调组件除了zookee...

  • Etcd 使用入门

    转载: Etcd 使用入门 etcd简介 etcd是CoreOS团队于2013年6月发起的开源项目,它的目标是构建...

  • etcd入门之安全配置

    1 基于TLS的通信安全配置 etcd通过简单的配置就能支持基于https的安全通信。https通信需要准备相应的...

  • etcd入门之安装部署

    1 单点部署 1.1 安装 确认一下linux操作系统内核版本,版本号需要大于3.10 直接从官网下载相应的二进制...

  • ETCD快速入门-03 常用命令

  • etcd快速入门

    官方链接: etcd命令行 快速入门单机启动etcd本地集群启动使用goreman启动本地三节点Procfileg...

  • etcdctl 常用命令

    etcdctl 常用命令 1. 指定etcd集群 2. 增删查 2.1、增 2.2、查 基于相同前缀查找 2.3、...

  • ETCD入门实践

    什么是ETCD etcd是CoreOS团队于2013年6月发起的开源项目,它的目标是构建一个高可用的分布式键值(k...

网友评论

      本文标题:etcd入门之常用命令

      本文链接:https://www.haomeiwen.com/subject/ayrygrtx.html