etcd命令

etcdctl支持下面列出來的命令,基本上能夠分爲數據庫操做和非數據庫操做,能夠查看etcdctl README.md來了解更多node

➜  ~  etcdctl -h
NAME:
etcdctl - A simple command line client for etcd.

USAGE:
etcdctl [global options] command [command options] [arguments...]

VERSION:
2.2.1

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 or existing directory
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
import import a snapshot to a cluster
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

GLOBAL OPTIONS:
--debug output cURL commands which can be used to reproduce the request
--no-sync don't synchronize cluster information before sending request
--output, -o 'simple' output response in the given format (`simple`, `extended` or `json`)
--discovery-srv, -D domain name to query for SRV records describing cluster endpoints
--peers, -C a comma-delimited list of machine addresses in the cluster (default: "http://127.0.0.1:4001,http://127.0.0.1:2379")
--endpoint a comma-delimited list of machine addresses in the cluster (default: "http://127.0.0.1:4001,http://127.0.0.1:2379")
--cert-file identify HTTPS client using this SSL certificate file
--key-file identify HTTPS client using this SSL key file
--ca-file verify certificates of HTTPS-enabled servers using this CA bundle
--username, -u provide username[:password] and prompt if password is not supplied.
--timeout '1s' connection timeout per request
--total-timeout '5s' timeout for the command execution (except watch)
--help, -h show help
--version, -v print the version

數據庫操做

數據庫操做圍繞對鍵值和目錄的 CRUD (符合 REST 風格的一套操做:Create)完整生命週期的管理。git

etcd 在鍵的組織上採用了層次化的空間結構(相似於文件系統中目錄的概念),用戶指定的鍵能夠爲單獨的名字,如 testkey,此時實際上放在根目錄 / 下面,也能夠爲指定目錄結構,如 cluster1/node2/testkey,則將建立相應的目錄結構。github

注:CRUD 即 Create, Read, Update, Delete,是符合 REST 風格的一套 API 操做。數據庫

set

指定某個鍵的值。例如json

➜  ~  etcdctl set /testdir/testkey "Hello world"
Hello world

支持的選項包括:ruby

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

get

獲取指定鍵的值。例如dom

➜  ~  etcdctl get /testdir/testkey
Hello world

當鍵不存在時,則會報錯。例如ide

➜  ~  etcdctl get /testdir/testkey2
Error: 100: Key not found (/testdir/testkey2) [18]

支持的選項爲ui

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

update

當鍵存在時,更新值內容。例如this

➜  ~  etcdctl update /testdir/testkey "Hello"
Hello

當鍵不存在時,則會報錯。例如

➜  ~  etcdctl update /testdir/testkey2 "Hello"
Error: 100: Key not found (/testdir/testkey2) [19]

支持的選項爲

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

rm

刪除某個鍵值。例如

➜  ~  etcdctl rm /testdir/testkey
PrevNode.Value: Hello

當鍵不存在時,則會報錯。例如

➜  ~  etcdctl rm /testdir/testkey
Error: 100: Key not found (/testdir/testkey) [20]

支持的選項爲

--dir 若是鍵是個空目錄或者鍵值對則刪除
--recursive 刪除目錄和全部子鍵
--with-value 檢查現有的值是否匹配
--with-index '0' 檢查現有的 index 是否匹配

mk

若是給定的鍵不存在,則建立一個新的鍵值。例如

➜  ~  etcdctl mk /testdir/testkey "Hello world"
Hello world

當鍵存在的時候,執行該命令會報錯,例如

➜  ~  etcdctl mk /testdir/testkey "Hello world"
Error: 105: Key already exists (/testdir/testkey) [21]

支持的選項爲

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

mkdir

若是給定的鍵目錄不存在,則建立一個新的鍵目錄。例如

➜  ~  etcdctl mkdir testdir2

當鍵目錄存在的時候,執行該命令會報錯,例如

➜  ~  etcdctl mkdir testdir2
Error: 105: Key already exists (/testdir2) [22]

支持的選項爲

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

setdir

建立一個鍵目錄,不管存在與否。

支持的選項爲

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

updatedir

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

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

rmdir

刪除一個空目錄,或者鍵值對。

➜  ~  etcdctl setdir dir1
➜ ~ etcdctl rmdir dir1

若目錄不空,會報錯

➜  ~  etcdctl set /dir/testkey hi
hi
➜ ~ etcdctl rmdir /dir
Error: 108: Directory not empty (/dir) [29]

ls

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

例如

➜  ~  etcdctl ls
/testdir
/testdir2
/dir
➜ ~ etcdctl ls dir
/dir/testkey

支持的選項包括

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

非數據庫操做

backup

備份 etcd 的數據。

支持的選項包括

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

watch

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

例如,用戶更新 testkey 鍵值爲 Hello watch。

➜  ~  etcdctl get /testdir/testkey
Hello world
➜ ~ etcdctl set /testdir/testkey "Hello watch"
Hello watch
➜  ~  etcdctl watch testdir/testkey
Hello watch

支持的選項包括

--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 返回全部的鍵值和子鍵值

member

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

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

$ etcdctl member list
ce2a822cea30bfca: name=default peerURLs=http://localhost:2380,http://localhost:7001 clientURLs=http://localhost:2379,http://localhost:4001

命令選項

--debug 輸出 cURL 命令,顯示執行命令的時候發起的請求
--no-sync 發出請求以前不一樣步集羣信息
--output, -o 'simple' 輸出內容的格式 (simple 爲原始信息,json 爲進行json格式解碼,易讀性好一些)
--peers, -C 指定集羣中的同伴信息,用逗號隔開 (默認爲: "127.0.0.1:4001")
--cert-file HTTPS 下客戶端使用的 SSL 證書文件
--key-file HTTPS 下客戶端使用的 SSL 密鑰文件
--ca-file 服務端使用 HTTPS 時,使用 CA 文件進行驗證
--help, -h 顯示幫助命令信息
--version, -v 打印版本信息
相關文章
相關標籤/搜索