etcd常用操作介紹

安裝

最簡單的安裝方法是直接去etcd GitHub的Release頁下載預編譯好的二進制文件。etcd官方爲各個系統提供了不同的二進制文件,供開發者根據自己的系統去下載。

下載地址:https://github.com/etcd-io/et...

下載完成解壓後,目錄中有兩個二進制文件,etcd以及etcdctl。其中etcd就是運行etcd服務的二進制文件,etcdctl是官方提供的命令行etcd客戶端,使用etcdctl可以在命令行中訪問etcd服務。

etcdetcdctl這兩個文件軟鏈到系統環境變量$PATH對應的目錄下,方便服務啓動,當然試驗目的直接把工作目錄切換到剛纔下載的目錄直接運行兩個文件即可。

我從GitHub上下載了MacOS對應的etcd文件,執行下面的命令可以看到etcd的版本

➜  etcd-v3.3.17-darwin-amd64 ./etcd --version
etcd Version: 3.3.17
Git SHA: 6d8052314
Go Version: go1.12.9
Go OS/Arch: darwin/amd64
➜  etcd-v3.3.17-darwin-amd64 

運行

直接運行etcd指令在電腦上啓動和運行etcd服務

......
2019-10-22 13:15:32.244300 I | embed: listening for peers on http://localhost:2380
2019-10-22 13:15:32.244466 I | embed: listening for client requests on localhost:2379
......

通過啓動命令的輸出日誌中可以找到兩行關鍵的信息,etcd服務啓動後提供給外部客戶端通信的端口是2379,而etcd服務中成員間的通信端口是2380(Peer是對同一個 etcd 集羣中另外一個 Member 的稱呼)。

啓動命令時比較重要的options:

-name 節點名稱,默認是UUID
-data-dir 保存日誌和快照的目錄,默認爲當前工作目錄
-addr 公佈的ip地址和端口。 默認爲127.0.0.1:2379
-bind-addr 用於客戶端連接的監聽地址,默認爲-addr配置
-peers 集羣成員逗號分隔的列表,例如 127.0.0.1:2380,127.0.0.1:2381
-peer-addr 集羣服務通訊的公佈的IP地址,默認爲 127.0.0.1:2380.
-peer-bind-addr 集羣服務通訊的監聽地址,默認爲-peer-addr配置

上述配置也可以設置配置文件,默認爲/etc/etcd/etcd.conf

使用etcd

etcdctl是一個命令行的客戶端,它提供了一下簡潔的命令,可以方便我們在對服務進行測試或者手動修改數據庫內容。建議剛剛接觸etcd的同學可以先通過etcdctl來熟悉相關操作。這些操作跟etcd提供的HTTP API是對應的。

通過-h選項可以看到etcdctl支持的操作。

➜  etcd-v3.3.17-darwin-amd64 ./etcdctl -h
NAME:
   etcdctl - A simple command line client for etcd.
......

VERSION:
   3.3.17
   
COMMANDS:
     backup          backup an etcd directory
     cluster-health  check the health of the etcd cluster
     mk              make a new key with a given value
     mkdir           make a new directory
     rm              remove a key or a directory
     rmdir           removes the key if it is an empty directory or a key-value pair
     get             retrieve the value of a key
     ls              retrieve a directory
     set             set the value of a key
     setdir          create a new directory or update an existing directory TTL
     update          update an existing key with a given value
     updatedir       update an existing directory
     watch           watch a key for changes
     exec-watch      watch a key for changes and exec an executable
     member          member add, remove and list subcommands
     user            user add, grant and revoke subcommands
     role            role add, grant and revoke subcommands
     auth            overall auth controls
     help, h         Shows a list of commands or help for one command

這些操作命令基本上分爲鍵值庫操作命令和行爲控制命令。

鍵值庫操作

set

設置鍵(或者叫主題)的值

➜  etcd-v3.3.17-darwin-amd64 ./etcdctl set /root/test/keyOne "Hello etcd"
Hello etcd
➜  etcd-v3.3.17-darwin-amd64 

支持的選項包括:

--ttl '0'            該鍵值的超時時間(單位爲秒),不配置(默認爲 0)則永不超時
--swap-with-value value 若該鍵現在的值是 value,則進行設置操作
--swap-with-index '0'    若該鍵現在的索引值是指定索引,則進行設置操作

get

獲取給定鍵的值

➜  etcd-v3.3.17-darwin-amd64 ./etcdctl get /root/test/keyOne          
Hello etcd
➜  etcd-v3.3.17-darwin-amd64 

當嘗試獲取不存的值時會報錯

➜  etcd-v3.3.17-darwin-amd64 ./etcdctl get /root/test/keyTwo
Error:  100: Key not found (/root/test/keyTwo) [11]
➜  etcd-v3.3.17-darwin-amd64 

支持的選項爲

--sort    對結果進行排序
--consistent 將請求發給主節點,保證獲取內容的一致性

update

更新給定鍵中存儲的值

➜  etcd-v3.3.17-darwin-amd64 ./etcdctl update /root/test/keyOne "Hello World"
Hello World
➜  etcd-v3.3.17-darwin-amd64 

同樣嘗試更新不存在的值時會報錯

➜  etcd-v3.3.17-darwin-amd64 ./etcdctl update /root/test/keyTwo "Hello World"
Error:  100: Key not found (/root/test/keyTwo) [11]

支持的選項爲

--ttl '0'    超時時間(單位爲秒),不配置(默認爲 0)則永不超時

rm

刪除給定的鍵,如果命令參數中給定的鍵不存在則會報錯

➜  etcd-v3.3.17-darwin-amd64 ./etcdctl rm /root/test/keyOne              
PrevNode.Value: Hello World
➜  etcd-v3.3.17-darwin-amd64 
--dir        如果鍵是個空目錄或者鍵值對則刪除
--recursive        刪除目錄和所有子鍵
--with-value     檢查現有的值是否匹配
--with-index '0'    檢查現有的 index 是否匹配

setdir

創建一個目錄,無論存在與否。

支持的選項爲

--ttl '0'    超時時間(單位爲秒),不配置(默認爲 0)則永不超時

updatedir

更新一個已經存在的目錄。 支持的選項爲

--ttl '0'    超時時間(單位爲秒),不配置(默認爲 0)則永不超時

ls

列出目錄(默認爲根目錄)下的鍵或者子目錄,默認不顯示子目錄中內容。

支持的選項包括

--sort    將輸出結果排序
--recursive    如果目錄下有子目錄,則遞歸輸出其中的內容
-p        對於輸出爲目錄,在最後添加 `/` 進行區分

行爲操作

backup

備份 etcd 的數據。

支持的選項包括

--data-dir         etcd 的數據目錄
--backup-dir     備份到指定路徑

watch

監測一個鍵值的變化,一旦鍵值發生更新,就會輸出最新的值並退出。

例如

➜  etcd-v3.3.17-darwin-amd64 ./etcdctl set root/test/KeyThree "Hello etcd"
Hello etcd // 設置root/test/KeyThree的值
// 監控root/test/KeyThree,在其他會話裏將它的值改爲"Hello World" 這裏就能收到更新後的結果
➜  etcd-v3.3.17-darwin-amd64 ./etcdctl watch  root/test/KeyThree --forever
Hello World

支持的選項包括

--forever        一直監測,直到用戶按 `CTRL+C` 退出
--after-index '0'    在指定 index 之前一直監測
--recursive        返回所有的鍵值和子鍵值

exec-watch

監測一個鍵值的變化,一旦鍵值發生更新,就執行給定命令。

例如,用戶更新 testkey 鍵值。

➜ etcd-v3.3.17-darwin-amd64 ./etcdctl exec-watch testkey -- sh -c 'ls'
default.etcd
Documentation
etcd
etcdctl
etcd-migrate
README-etcdctl.md
README.md

支持的選項包括

--after-index '0'    在指定 index 之前一直監測
--recursive        返回所有的鍵值和子鍵值

member

通過 list、add、remove 命令列出、添加、刪除 etcd 實例到 etcd 集羣中。

例如本地啓動一個 etcd 服務實例後,可以用如下命令進行查看。

➜  etcd-v3.3.17-darwin-amd64 ./etcdctl member list
8e9e05c52164694d: name=default peerURLs=http://localhost:2380 clientURLs=http://localhost:2379 isLeader=true
➜  etcd-v3.3.17-darwin-amd64 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章