etcd入門之常用命令

1 cluster

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

# 通過endpoints參數(shù)指定連接的遠(yuǎn)端etcd節(jié)點(diǎn)
$ ./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

# 查看集群狀態(tài)
$ ./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

# 限制返回的結(jié)果數(shù)量
$ ./bin/etcdctl get --prefix --limit=2 /foo --print-value-only
hello world
100

# 查詢key的詳細(xì)信息。etcd維護(hù)了一個(gè)全局遞增的revision值,每次修改某個(gè)key的值就會(huì)增加1. 從下面的信息可以看出,key /foo1的最新revision值是7,創(chuàng)建時(shí)的revision值是2,共有4個(gè)版本(表示寫入了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

# 根據(jù)前綴刪除
$ ./bin/etcdctl del --prefix /foo1


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

5 lease

# 創(chuàng)建租約,生命周期為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,同時(shí)查看綁定的key
$ ./bin/etcdctl lease timetolive --keys 694d82b0f0dfb037
lease 694d82b0f0dfb037 granted with TTL(120s), remaining(32s), attached keys([/foo5])

# 持續(xù)續(xù)租,直到CTRL + C退出續(xù)租
$ ./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

在一個(gè)窗口監(jiān)聽key /foo10

$ ./bin/etcdctl watch /foo10

在另一個(gè)窗口修改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

可以觀察到監(jiān)聽窗口打印以下key值變化的過程

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

PUT
/foo10
123

# 由租約到期刪除key觸發(fā)
DELETE
/foo10

# 監(jiān)聽范圍
$ ./bin/etcdctl watch /foo1 /foo10

# 監(jiān)聽前綴
$ ./bin/etcdctl watch --prefix /foo
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 官方鏈接: etcd命令行 快速入門單機(jī)啟動(dòng)etcd本地集群?jiǎn)?dòng)使用goreman啟動(dòng)本地三節(jié)點(diǎn)Procfileg...
    撈月亮的阿湯哥閱讀 2,113評(píng)論 0 2
  • 0 專輯概述 etcd 是云原生架構(gòu)中重要的基礎(chǔ)組件,由 CNCF 孵化托管。etcd 在微服務(wù)和 Kuberna...
    aoho閱讀 20,591評(píng)論 0 2
  • 引言 目前,可實(shí)現(xiàn)分布式鎖的開源軟件還是比較多的,其中應(yīng)用最廣泛、大家最熟悉的應(yīng)該就是 ZooKeeper,此外還...
    愛情小傻蛋閱讀 8,991評(píng)論 1 19
  • 一、什么是ETCD etcd是CoreOS團(tuán)隊(duì)于2013年6月發(fā)起的開源項(xiàng)目,它的目標(biāo)是構(gòu)建一個(gè)高可用的分布式鍵值...
    宏勢(shì)閱讀 3,376評(píng)論 1 3
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,699評(píng)論 19 139

友情鏈接更多精彩內(nèi)容