etcd 是 CoreOS 團隊發起的一個管理配置信息和服務發現(service discovery)的項目,在這一章裏面,咱們將介紹該項目的目標,安裝和使用,以及實現的技術。javascript
etcd 是 CoreOS 團隊於 2013 年 6 月發起的開源項目,它的目標是構建一個高可用的分佈式鍵值(key-value)數據庫,基於 Go 語言實現。咱們知道,在分佈式系統中,各類服務的配置信息的管理分享,服務的發現是一個很基本同時也是很重要的問題。CoreOS 項目就但願基於 etcd 來解決這一問題。java
etcd 目前在 github.com/coreos/etcd 進行維護,即將發佈 2.0.0 版本。node
受到 Apache ZooKeeper 項目和 doozer 項目的啓發,etcd 在設計的時候重點考慮了下面四個要素:linux
注:Apache ZooKeeper 是一套知名的分佈式系統中進行同步和一致性管理的工具。注:doozer 則是一個一致性分佈式數據庫。注:Raft 是一套經過選舉主節點來實現分佈式系統一致性的算法,相比於大名鼎鼎的 Paxos 算法,它的過程更容易被人理解,由 Stanford 大學的 Diego Ongaro 和 John Ousterhout 提出。更多細節能夠參考 raftconsensus.github.io。git
通常狀況下,用戶使用 etcd 能夠在多個節點上啓動多個實例,並添加它們爲一個集羣。同一個集羣中的 etcd 實例將會保持彼此信息的一致性。github
etcd 基於 Go 語言實現,所以,用戶能夠從 項目主頁 下載源代碼自行編譯,也能夠下載編譯好的二進制文件,甚至直接使用製做好的 Docker 鏡像文件來體驗。算法
編譯好的二進制文件都在 github.com/coreos/etcd/releases 頁面,用戶能夠選擇須要的版本,或經過下載工具下載。sql
例如,下面的命令使用 curl 工具下載壓縮包,並解壓。docker
curl -L https://github.com/coreos/etcd/releases/download/v2.0.0-rc.1/etcd-v2.0.0-rc.1-linux-amd64.tar.gz -o etcd-v2.0.0-rc.1-linux-amd64.tar.gz tar xzvf etcd-v2.0.0-rc.1-linux-amd64.tar.gz cd etcd-v2.0.0-rc.1-linux-amd64
解壓後,能夠看到文件包括數據庫
$ ls etcd etcdctl etcd-migrate README-etcdctl.md README.md
其中 etcd 是服務主文件,etcdctl 是提供給用戶的命令客戶端,etcd-migrate 負責進行遷移。
推薦經過下面的命令將三個文件都放到系統可執行目錄 /usr/local/bin/
或 /usr/bin/
。
$ sudo cp etcd* /usr/local/bin/
運行 etcd,將默認組建一個兩個節點的集羣。數據庫服務端默認監聽在 2379 和 4001 端口,etcd 實例監聽在 2380 和 7001 端口。顯示相似以下的信息:
$ ./etcd
2014/12/31 14:52:09 no data-dir provided, using default data-dir ./default.etcd
2014/12/31 14:52:09 etcd: listening for peers on http://localhost:2380
2014/12/31 14:52:09 etcd: listening for peers on http://localhost:7001
2014/12/31 14:52:09 etcd: listening for client requests on http://localhost:2379
2014/12/31 14:52:09 etcd: listening for client requests on http://localhost:4001
2014/12/31 14:52:09 etcdserver: name = default
2014/12/31 14:52:09 etcdserver: data dir = default.etcd
2014/12/31 14:52:09 etcdserver: snapshot count = 10000
2014/12/31 14:52:09 etcdserver: advertise client URLs = http://localhost:2379,http://localhost:4001
2014/12/31 14:52:09 etcdserver: initial advertise peer URLs = http://localhost:2380,http://localhost:7001
2014/12/31 14:52:09 etcdserver: initial cluster = default=http://localhost:2380,default=http://localhost:7001
2014/12/31 14:52:10 etcdserver: start member ce2a822cea30bfca in cluster 7e27652122e8b2ae 2014/12/31 14:52:10 raft: ce2a822cea30bfca became follower at term 0 2014/12/31 14:52:10 raft: newRaft ce2a822cea30bfca [peers: [], term: 0, commit: 0, lastindex: 0, lastterm: 0] 2014/12/31 14:52:10 raft: ce2a822cea30bfca became follower at term 1 2014/12/31 14:52:10 etcdserver: added local member ce2a822cea30bfca [http://localhost:2380 http://localhost:7001] to cluster 7e27652122e8b2ae 2014/12/31 14:52:11 raft: ce2a822cea30bfca is starting a new election at term 1 2014/12/31 14:52:11 raft: ce2a822cea30bfca became candidate at term 2 2014/12/31 14:52:11 raft: ce2a822cea30bfca received vote from ce2a822cea30bfca at term 2 2014/12/31 14:52:11 raft: ce2a822cea30bfca became leader at term 2 2014/12/31 14:52:11 raft.node: ce2a822cea30bfca elected leader ce2a822cea30bfca at term 2 2014/12/31 14:52:11 etcdserver: published {Name:default ClientURLs:[http://localhost:2379 http://localhost:4001]} to cluster 7e27652122e8b2ae
此時,可使用 etcdctl 命令進行測試,設置和獲取鍵值 testkey: "hello world"
,檢查 etcd 服務是否啓動成功:
$ ./etcdctl set testkey "hello world" hello world $ ./etcdctl get testkey hello world
說明 etcd 服務已經成功啓動了。
固然,也能夠經過 HTTP 訪問本地 2379 或 4001 端口的方式來進行操做,例如查看 testkey
的值:
$ curl -L http://localhost:4001/v2/keys/testkey {"action":"get","node":{"key":"/testkey","value":"hello world","modifiedIndex":3,"createdIndex":3}}
鏡像名稱爲 quay.io/coreos/etcd:v2.0.0_rc.1,能夠經過下面的命令啓動 etcd 服務監聽到 4001 端口。
$ sudo docker run -p 4001:4001 -v /etc/ssl/certs/:/etc/ssl/certs/ quay.io/coreos/etcd:v2.0.0_rc.1
etcdctl 是一個命令行客戶端,它能提供一些簡潔的命令,供用戶直接跟 etcd 服務打交道,而無需基於 HTTP API 方式。這在某些狀況下將很方便,例如用戶對服務進行測試或者手動修改數據庫內容。咱們也推薦在剛接觸 etcd 時經過 etcdctl 命令來熟悉相關的操做,這些操做跟 HTTP API 其實是對應的。
etcd 項目二進制發行包中已經包含了 etcdctl 工具,沒有的話,能夠從 github.com/coreos/etcd/releases 下載。
etcdctl 支持以下的命令,大致上分爲數據庫操做和非數據庫操做兩類,後面將分別進行解釋。
$ etcdctl -h
NAME:
etcdctl - A simple command line client for etcd.
USAGE:
etcdctl [global options] command [command options] [arguments...]
VERSION:
2.0.0-rc.1
COMMANDS:
backup backup an etcd directory mk make a new key with a given value mkdir make a new directory rm remove a key 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 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` or `json`) --peers, -C a comma-delimited list of machine addresses in the cluster (default: "127.0.0.1:4001") --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 --help, -h show help --version, -v print the version
數據庫操做圍繞對鍵值和目錄的 CRUD (符合 REST 風格的一套操做:Create)完整生命週期的管理。
etcd 在鍵的組織上採用了層次化的空間結構(相似於文件系統中目錄的概念),用戶指定的鍵能夠爲單獨的名字,如 testkey
,此時實際上放在根目錄 /
下面,也能夠爲指定目錄結構,如 cluster1/node2/testkey
,則將建立相應的目錄結構。
注:CRUD 即 Create, Read, Update, Delete,是符合 REST 風格的一套 API 操做。
指定某個鍵的值。例如
$ etcdctl set /testdir/testkey "Hello world" Hello world
支持的選項包括:
--ttl '0' 該鍵值的超時時間(單位爲秒),不配置(默認爲 0)則永不超時 --swap-with-value value 若該鍵如今的值是 value,則進行設置操做 --swap-with-index '0' 若該鍵如今的索引值是指定索引,則進行設置操做
獲取指定鍵的值。例如
$ etcdctl set testkey hello hello $ etcdctl update testkey world world
當鍵不存在時,則會報錯。例如
$ etcdctl get testkey2
Error: 100: Key not found (/testkey2) [1]
支持的選項爲
--sort 對結果進行排序 --consistent 將請求發給主節點,保證獲取內容的一致性
當鍵存在時,更新值內容。例如
$ etcdctl set testkey hello hello $ etcdctl update testkey world world
當鍵不存在時,則會報錯。例如
$ etcdctl update testkey2 world Error: 100: Key not found (/testkey2) [1]
支持的選項爲
--ttl '0' 超時時間(單位爲秒),不配置(默認爲 0)則永不超時
刪除某個鍵值。例如
$ etcdctl rm testkey
當鍵不存在時,則會報錯。例如
$ etcdctl rm testkey2
Error: 100: Key not found (/testkey2) [8]
支持的選項爲
--dir 若是鍵是個空目錄或者鍵值對則刪除 --recursive 刪除目錄和全部子鍵 --with-value 檢查現有的值是否匹配 --with-index '0' 檢查現有的 index 是否匹配
若是給定的鍵不存在,則建立一個新的鍵值。例如
$ etcdctl mk /testdir/testkey "Hello world" Hello world
當鍵存在的時候,執行該命令會報錯,例如
$ etcdctl set testkey "Hello world" Hello world $ ./etcdctl mk testkey "Hello world" Error: 105: Key already exists (/testkey) [2]
支持的選項爲
--ttl '0' 超時時間(單位爲秒),不配置(默認爲 0)則永不超時
若是給定的鍵目錄不存在,則建立一個新的鍵目錄。例如
$ etcdctl mkdir testdir
當鍵目錄存在的時候,執行該命令會報錯,例如
$ etcdctl mkdir testdir $ etcdctl mkdir testdir Error: 105: Key already exists (/testdir) [7]
支持的選項爲
--ttl '0' 超時時間(單位爲秒),不配置(默認爲 0)則永不超時
建立一個鍵目錄,不管存在與否。
支持的選項爲
--ttl '0' 超時時間(單位爲秒),不配置(默認爲 0)則永不超時
更新一個已經存在的目錄。 支持的選項爲
--ttl '0' 超時時間(單位爲秒),不配置(默認爲 0)則永不超時
刪除一個空目錄,或者鍵值對。
若目錄不空,會報錯
$ etcdctl set /dir/testkey hi hi $ etcdctl rmdir /dir Error: 108: Directory not empty (/dir) [13]
列出目錄(默認爲根目錄)下的鍵或者子目錄,默認不顯示子目錄中內容。
例如
$ ./etcdctl set testkey 'hi' hi $ ./etcdctl set dir/test 'hello' hello $ ./etcdctl ls /testkey /dir $ ./etcdctl ls dir /dir/test
支持的選項包括
--sort 將輸出結果排序 --recursive 若是目錄下有子目錄,則遞歸輸出其中的內容 -p 對於輸出爲目錄,在最後添加 `/` 進行區分
備份 etcd 的數據。
支持的選項包括
--data-dir etcd 的數據目錄 --backup-dir 備份到指定路徑
監測一個鍵值的變化,一旦鍵值發生更新,就會輸出最新的值並退出。
例如,用戶更新 testkey 鍵值爲 Hello world。
$ etcdctl watch testkey Hello world
支持的選項包括
--forever 一直監測,直到用戶按 `CTRL+C` 退出 --after-index '0' 在指定 index 以前一直監測 --recursive 返回全部的鍵值和子鍵值
監測一個鍵值的變化,一旦鍵值發生更新,就執行給定命令。
例如,用戶更新 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 返回全部的鍵值和子鍵值
經過 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
打印版本信息