etcd 命令


etcdctl命令全局選項參數

參數 說明
--debug 輸出調試信息,顯示執行命令的時候發起的請求
--no-sync 發出請求錢不主動同步集群信息
--output, -o 'simple' 輸出響應消息的格式,可以為simple,json或extended
--discovery-srv, -D 通過域名查詢,來探測集群成員信息
--peers, -C 集群中成員地址列表,用逗號隔開
--endpoint 集群中成員地址列表
--cert-file 如果集群需要https認證,提供TLS的證書文件路徑
--key-file HTTPS 認證的證書文件路徑
--ca-file 域名相關的根證書文件路徑
--username, -u username[:password] 用戶名和密碼驗證信息
--timeout '1s' 請求的連接超時,默認為1s
--total-timeout '5s' 命令執行總超時,默認為5s
--version, -v 打印版本信息

etcd命令預覽

etcd支持的命令大體上可以分為兩類,數據類操作、非數據類操作

數據類操作

命令 說明
set 設置鍵對應的值
get 獲取鍵對應的值
update 更新鍵對應的值
mk 創建新的值
rm 刪除鍵值或目錄
watch 監控某鍵值的變化
exec-watch 監控到某鍵值變化時,執行的命令
ls 列出目錄下的內容
mkdir 創建新的目錄
rmdir 刪除空目錄或者一個鍵值
setdir 創建目錄(允許目錄已經存在)
updatedir 更新已經存在的目錄

非數據類操作

命令 說明
backup 備份指定的目錄
cluster-health 檢查etcd集群健康情況
member 添加、刪除或列出成員,需要帶具體的子命令
import 導入快照
user 用戶添加、權限管理,需要帶具體子命令
role 角色添加、權限管理,需要帶具體子命令
auth 全局認證管理

數據類操作

1. set

[root@node2 ~]# etcdctl set /testdir/testkey "helloworld"
helloworld

2. get

[root@node2 ~]# etcdctl get /testdir/testkey
helloworld
[root@node2 ~]# 

支持--sort,對返回結果進行排序

3. update

[root@node2 ~]# etcdctl update /testdir/testkey hello
hello
[root@node2 ~]# etcdctl get /testdir/testkey
hello
[root@node2 ~]# 

4. mk

如果給定的鍵不存在,則創建,否則報錯

[root@node2 ~]# etcdctl mk /testdir/testkey2 hello
hello
[root@node2 ~]#

[root@node2 ~]# etcdctl mk /testdir/testkey hello
Error:  105: Key already exists (/testdir/testkey) [16]

【set與mk區別】 當鍵存在是set會修改鍵的值,mk則會報錯

5. rm

刪除某個鍵,鍵不存在就報錯
[root@node2 ~]# etcdctl rm /testdir/testkey2
PrevNode.Value: hello

--dir 如果鍵是空目錄或者是鍵值對則刪除
--recursive  刪除目錄和所有子鍵

6. watch

監控一個鍵值的變化,發現鍵值更新后就回退出

--forever 一直監測,知道用戶按CTRL+C退出

7. exec-watch

監測一個鍵值的變化,一旦鍵值發生更新,執行指定的命令
[root@node2 ~]# etcdctl exec-watch /testdir/testkey -- sh -c 'ls /'
bin  boot  dev	etc  home  lib	lib64  media  mnt  opt	proc  root  run  sbin  srv  sys  tmp  usr  var

該命令會一直執行下去,不會像watch一樣,監測到一次更新后就退出

8. mkdir

如果給定的目錄不存在則創建,存在會報錯

[root@node2 ~]# etcdctl mkdir /test/testc --ttl 10 
--ttl 超時時間單位s,10s后目錄會被刪除

9. rmdir

刪除一個空目錄,或鍵值對。若目錄不為空,會報錯。

[root@node2 ~]# etcdctl mkdir /testa
[root@node2 ~]# etcdctl rmdir /testa/
[root@node2 ~]#

[root@node2 ~]# etcdctl ls /testdir/
/testdir/testkey
/testdir/testkey2
[root@node2 ~]# etcdctl rmdir /testdir/
Error:  108: Directory not empty (/testdir) [28]
[root@node2 ~]# 

11. setdir

創建一個空目錄,如果目錄已經存在會報錯

[root@node2 ~]# etcdctl setdir /etc/testb
[root@node2 ~]# etcdctl ls /etc/testb
[root@node2 ~]# etcdctl setdir /etc/testb
Error:  102: Not a file (/etc/testb) [34]
[root@node2 ~]# 

12. updatedir

用於更新已經存在的目錄屬性(目前只有存活時間)
[root@node2 ~]# etcdctl mkdir /test/testc --ttl 10
[root@node2 ~]# etcdctl mkdir /test/testc --ttl 30
[root@node2 ~]# etcdctl updatedir /test/testc --ttl 300
[root@node2 ~]# 

非數據類操作

1. backup

   --data-dir value        數據目錄路徑
   --wal-dir value         Path to the etcd wal dir
   --backup-dir value      備份數據到指定目錄
   --backup-wal-dir value  Path to the backup wal dir
   --with-v3               Backup v3 backend data

[root@node2 ~]# cat /etc/etcd/etcd.conf |grep data-dir
data-dir: "/opt/etcd_data"
[root@node2 ~]# ls /opt/etcd_data
member
[root@node2 ~]# etcdctl backup --data-dir /opt/etcd_data --backup-dir /tmp/
2019-06-05 22:37:20.757947 I | ignoring EntryConfChange raft entry
2019-06-05 22:37:20.758148 I | ignoring EntryConfChange raft entry
2019-06-05 22:37:20.758170 I | ignoring EntryConfChange raft entry
2019-06-05 22:37:20.758314 I | ignoring member attribute update on /0/members/cf5538a4dab95927/attributes
2019-06-05 22:37:20.758356 I | ignoring member attribute update on /0/members/a3d1fb0d28ed2953/attributes
2019-06-05 22:37:20.758387 I | ignoring member attribute update on /0/members/8816eaa680e63c73/attributes
2019-06-05 22:37:20.758455 I | ignoring v3 raft entry
2019-06-05 22:37:20.758484 I | ignoring member attribute update on /0/members/cf5538a4dab95927/attributes
2019-06-05 22:37:20.758509 I | ignoring member attribute update on /0/members/a3d1fb0d28ed2953/attributes
2019-06-05 22:37:20.758534 I | ignoring member attribute update on /0/members/8816eaa680e63c73/attributes
2019-06-05 22:37:20.758575 I | ignoring member attribute update on /0/members/8816eaa680e63c73/attributes
2019-06-05 22:37:20.758608 I | ignoring member attribute update on /0/members/a3d1fb0d28ed2953/attributes
2019-06-05 22:37:20.758633 I | ignoring member attribute update on /0/members/cf5538a4dab95927/attributes
2019-06-05 22:37:20.758720 I | ignoring v3 raft entry
2019-06-05 22:37:20.758870 I | ignoring member attribute update on /0/members/cf5538a4dab95927/attributes
2019-06-05 22:37:20.758891 I | ignoring member attribute update on /0/members/a3d1fb0d28ed2953/attributes
2019-06-05 22:37:20.758902 I | ignoring member attribute update on /0/members/8816eaa680e63c73/attributes

[root@node2 ~]# ls /tmp/member/
snap  wal
[root@node2 ~]# 
snap 快照目錄,保存節點狀態快照文件
wal 保存了數據庫預寫日志信息

【預寫日志】預寫日志要求數據庫在發生實際提交前必須先將操作寫入日志,可以保障系統在崩潰后根據日志狀態恢復狀態。

2. cluster-health

3. member

4. role

add 添加一個角色
get 查詢角色細節
list 列出所有用戶角色
remove 刪除用戶角色
grant 添加路徑到角色控制 ,r:w:rw
revoke 刪除某路徑的用戶角色信息
默認帶有root,guest兩種角色,前者為最高權限用戶,后者為不帶驗證情況下的用戶

添加一個角色
[root@node2 ~]# etcdctl role add read_testdir
Role read_testdir created
[root@node2 ~]#

設置該角色具有對/testdir/目錄有讀權限
[root@node2 ~]# etcdctl role grant read_testdir -path '/testdir/*' -read
Role read_testdir updated
[root@node2 ~]# 

5. user

對用戶進行管理,
add 添加一個用戶
get 查詢用戶細節
list  列出所有用戶
remove 刪除用戶
grant 添加用戶到j角色
revoke 刪除用戶角色
passwd 修改用戶的密碼
默認情況下,需要先創建(啟用)root用戶作為etcd集群的最高權限管理員

[root@node2 ~]# etcdctl user add root
New password: 
User root created
[root@node2 ~]# etcdctl user add test1
New password: 
User test1 created
[root@node2 ~]# 

分配已有角色給用戶
[root@node2 ~]# etcdctl user grant test1 -roles read_testdir
User test1 updated
[root@node2 ~]#

查看用戶細節
[root@node2 ~]# etcdctl user get test1
User: test1
Roles:  read_testdir
-
[root@node2 ~]# etcdctl user get test1  #用戶具有多種角色
User: test1
Roles:   read_testdir root

刪除用戶角色
[root@node2 ~]# etcdctl user revoke test1 -roles read_testdir
User test1 updated
[root@node2 ~]# 

6. auth

是否啟用訪問驗證。enable為啟用,disable為禁用。

[root@node2 ~]# etcdctl user list
Insufficient credentials
[root@node2 ~]# etcdctl --username root:123 user list   使用--username  用戶名:密碼訪問
root
test1
[root@node2 ~]#

參考
《Docker技術入門與實戰》 第二版
 https://blog.csdn.net/fnatic_/article/details/84307332


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM