最簡單的安裝方法是直接去etcd GitHub的Release頁下載預編譯好的二進制文件。etcd官方爲各個系統提供了不一樣的二進制文件,供開發者根據本身的系統去下載。git
下載地址:https://github.com/etcd-io/et...github
下載完成解壓後,目錄中有兩個二進制文件,etcd
以及etcdctl
。其中etcd
就是運行etcd服務的二進制文件,etcdctl
是官方提供的命令行etcd客戶端,使用etcdctl
能夠在命令行中訪問etcd服務。數據庫
將etcd
和etcdctl
這兩個文件軟鏈到系統環境變量$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 的稱呼)。code
啓動命令時比較重要的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
。索引
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
這些操做命令基本上分爲鍵值庫操做命令和行爲控制命令。
設置鍵(或者叫主題)的值
➜ 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' 若該鍵如今的索引值是指定索引,則進行設置操做
獲取給定鍵的值
➜ 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 將請求發給主節點,保證獲取內容的一致性
更新給定鍵中存儲的值
➜ 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)則永不超時
刪除給定的鍵,若是命令參數中給定的鍵不存在則會報錯
➜ 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 是否匹配
建立一個目錄,不管存在與否。
支持的選項爲
--ttl '0' 超時時間(單位爲秒),不配置(默認爲 0)則永不超時
更新一個已經存在的目錄。 支持的選項爲
--ttl '0' 超時時間(單位爲秒),不配置(默認爲 0)則永不超時
列出目錄(默認爲根目錄)下的鍵或者子目錄,默認不顯示子目錄中內容。
支持的選項包括
--sort 將輸出結果排序 --recursive 若是目錄下有子目錄,則遞歸輸出其中的內容 -p 對於輸出爲目錄,在最後添加 `/` 進行區分
備份 etcd 的數據。
支持的選項包括
--data-dir etcd 的數據目錄 --backup-dir 備份到指定路徑
監測一個鍵值的變化,一旦鍵值發生更新,就會輸出最新的值並退出。
例如
➜ 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 返回全部的鍵值和子鍵值
監測一個鍵值的變化,一旦鍵值發生更新,就執行給定命令。
例如,用戶更新 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 返回全部的鍵值和子鍵值
經過 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