etcd v2文檔(1) -- 單體服務端,客戶端http請求api

git地址:

https://github.com/coreos/etcd

etcd v2 Documentation

etcd v3 Documentation

開啓一個服務端

也就是隻開啓一個服務端程序

./bin/etcd

什麼參數都不加,那麼etcd服務使用默認值。

IANA爲etcd分配的端口是2379用於客戶端通信2380用於服務器到服務器通信

獲得服務端版本信息

發送http 請求獲得版本信息

curl -L http://127.0.0.1:2379/version

set (key 空間操作)

etcd中的鍵是分層的,通常一個"/"分割一個節點(nodes),

設置數據存儲區中的第一個鍵值對。 在這種情況下,鍵是/message,值爲Hello world。

curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello world"
{
    "action": "set",
    "node": {
        "createdIndex": 2,
        "key": "/message",
        "modifiedIndex": 2,
        "value": "Hello world"
    }
}
  1. action : 剛纔要做什麼操作,該請求嘗試通過PUT HTTP請求修改node.value,從而設置操作的值。
  2. node.key : 發出請求的HTTP路徑. 我們設置 /message的值爲 hello world. 所以關鍵字段是/message。etcd使用類似文件系統的結構來表示鍵值對,因此所有鍵以/開始
  3. node.value : 值, 在上例中 請求成功設置爲 hello world
  4. node.createdIndex : 每個創建索引(createdIndex)是唯一的。單調遞增的整數。在此示例中,索引爲2,是發送到服務器的第一個請求。 這個可以用來做內部命令狀態使用,比如添加和同步服務器。
  5. node.modifiedIndex : 也是etcd的索引,當 set, delete, update, create, compareAndSwap, compareAndDelete 等操作的時候會改變這個索引的值。 由於 getwatch 命令不改變存儲中的狀態,所以它們不會改變node.modifiedIndex的值。

http 回覆頭信息(Response Headers)

etcd在響應中包含幾個HTTP頭,提供有關爲請求提供服務的etcd集羣的全局信息:

X-Etcd-Index: 35
X-Raft-Index: 5398
X-Raft-Term: 1
  1. X-Etcd-Index : 當前etcd的索引(index) 的值, 當請求watch命令查看 key空間,X-Etcd-Index爲當前 watch命令時的索引值。這意味着觀看的事件可能在X-Etcd-Index之後發生。
  2. X-Raft-Index : 類似索引,只是這個是在 raft protocol 協議下的索引值。
  3. X-Raft-Term : 是一個整數。當etcd master 變化的時候會增加。 如果這個數字迅速增加,您可能需要調整選舉超時。調整

調整

etcd中的默認設置應適用於平均網絡延遲較低的本地網絡上的安裝。 但是,當跨多個數據中心或通過高延遲的網絡使用etcd時,您可能需要調整心跳間隔和選擇超時設置。

網絡不是延遲的唯一來源。 每個請求和響應可能會受到領導者和跟隨者的慢磁盤的影響。 這些超時表示從另一臺機器的請求到成功響應的總時間。

時間參數

底層分佈式共識協議依賴於兩個單獨的時間參數,以確保節點可以在一個停頓或離線時切換領導(主分支)。第一個參數稱爲心跳間隔。這是領導者通知追隨者仍然是領導者的頻率。對於最佳實踐,參數應該圍繞成員之間的往返時間設置。默認情況下,etcd使用100ms的心跳間隔。

第二個參數是選舉超時。這個超時是追隨者節點在嘗試成爲領導者之前不會聽到心跳的時間。默認情況下,etcd使用1000ms的選舉超時。

調整這些值是一個折衷。心跳間隔的值建議在成員之間的平均往返時間(RTT)的最大值,通常爲往返時間的0.5-1.5倍。如果心跳間隔太小,etcd將發送不必要的消息,增加CPU和網絡資源的使用。另一方面,過高的心跳間隔導致選舉超時時間較長。更高的選舉超時需要更長的時間來檢測領導者的失敗。測量往返時間(RTT)的最簡單方法是使用PING實用程序。

應根據心跳間隔和成員之間的平均往返時間設置選舉超時。選舉超時時間必須至少是往返時間的10倍,因此可以考慮網絡中的差異。例如,如果您的成員之間的往返時間是10ms,那麼您應該至少有100ms的選舉超時。

您還應將您的選舉超時設置爲心跳間隔的至少5到10倍,以解決前導複製中的差異。對於50ms的心跳間隔,您應將您的選舉超時設置爲至少250ms - 500ms。

選舉超時上限爲50000ms(50s),只能在部署全局分佈式的etcd集羣時使用。美國大陸合理的往返時間爲130ms,美日之間約爲350-400ms。如果您的網絡具有不均衡的性能或定期的數據包延遲/丟失,則可能需要進行幾次重試才能成功發送數據包。所以5s是全球往返時間的安全上限。由於選舉超時時間應大於廣播時間的一個數量級,所以在全球分佈式集羣約爲5秒的情況下,50秒成爲合理的最大值。

一個羣集中的所有成員的心跳間隔和選舉超時值應相同。爲etcd成員設置不同的值可能會破壞羣集的穩定性。

您可以覆蓋命令行上的默認值:

# Command line arguments:
$ etcd -heartbeat-interval=100 -election-timeout=500

# Environment variables:
$ ETCD_HEARTBEAT_INTERVAL=100 ETCD_ELECTION_TIMEOUT=500 etcd

這些值以毫秒爲單位指定。

快照

etcd將所有關鍵更改附加到日誌文件。 這個日誌永遠增長,是對鍵的每一個改變的完整的線性歷史。 一個完整的歷史記錄適用於輕度使用的羣集,但是大量使用的羣集將攜帶大型日誌。

爲了避免有一個巨大的日誌等待定期快照。 這些快照爲etcd通過保存系統的當前狀態和刪除舊日誌來提供壓縮日誌的方法。

快照調諧

創建快照可能是昂貴的,因此只能在對etcd進行一定數量的更改後創建。 默認情況下,將在每10,000次更改之後進行快照。 如果etcd的內存使用率和磁盤使用率太高,可以通過在命令行中設置以下命令來降低快照閾值:

# Command line arguments:
$ etcd -snapshot-count=5000

# Environment variables:
$ ETCD_SNAPSHOT_COUNT=5000 etcd

get (根據鍵獲得值)

get 請求獲得剛纔設置 /message 的值

curl http://127.0.0.1:2379/v2/keys/message
{
    "action": "get",
    "node": {
        "createdIndex": 2,
        "key": "/message",
        "modifiedIndex": 2,
        "value": "Hello world"
    }
}

put (根據鍵修改值)

put 修改 鍵 /message 的值 hello world 爲 hello etcd.

curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello etcd"
{
    "action": "set",
    "node": {
        "createdIndex": 3,
        "key": "/message",
        "modifiedIndex": 3,
        "value": "Hello etcd"
    },
    "prevNode": {
    	"createdIndex": 2,
    	"key": "/message",
    	"value": "Hello world",
    	"modifiedIndex": 2
    }
}

prevNode字段表示在請求之前節點的狀態。 prevNode字段格式與之前節點格式相同,並且在給定節點沒有先前狀態的情況下被省略。

delete (刪除鍵)

delete 請求刪除 /message

curl http://127.0.0.1:2379/v2/keys/message -XDELETE
{
    "action": "delete",
    "node": {
        "createdIndex": 3,
        "key": "/message",
        "modifiedIndex": 4
    },
    "prevNode": {
    	"key": "/message",
    	"value": "Hello etcd",
    	"modifiedIndex": 3,
    	"createdIndex": 3
    }
}

ttl (設置過期)

etc中的鍵可以設置爲在指定的秒數後過期。 您可以通過在發送PUT請求時在鍵上設置TTL(生存時間)來執行此操作:

curl http://127.0.0.1:2379/v2/keys/foo -XPUT -d value=bar -d ttl=5
{
    "action": "set",
    "node": {
        "createdIndex": 5,
        "expiration": "2013-12-04T12:01:21.874888581-08:00",
        "key": "/foo",
        "modifiedIndex": 5,
        "ttl": 5,
        "value": "bar"
    }
}
  1. expiration :到期時間是該鍵到期並被刪除的時間。
  2. ttl : 是指定的鍵生存時間,以秒爲單位。

注意:鍵只能由集羣管理員設置過期,因此如果成員從集羣中斷開連接,則其鍵將不會到期,直到它重新加入。

curl http://127.0.0.1:2379/v2/keys/foo

如果TTL已經過期,該密鑰將被刪除,您將返回100。

{
    "cause": "/foo",
    "errorCode": 100,
    "index": 6,
    "message": "Key not found"
}

TTL可以取消設置,以避免通過更新操作過期:(刪除過期時間)

curl http://127.0.0.1:2379/v2/keys/foo -XPUT -d value=bar -d ttl= -d prevExist=true
{
    "action": "update",
    "node": {
        "createdIndex": 5,
        "key": "/foo",
        "modifiedIndex": 6,
        "value": "bar"
    },
    "prevNode": {
        "createdIndex": 5,
        "expiration": "2013-12-04T12:01:21.874888581-08:00",
        "key": "/foo",
        "modifiedIndex": 5,
        "ttl": 3,
        "value": "bar"
    }
}

刷新ttl (重新設置過期)

etcd中的鍵可以刷新,而不通知當前觀察者。

這可以通過在更新TTL時將refresh設置爲true來實現。

刷新時,無法更新密鑰的值。

curl http://127.0.0.1:2379/v2/keys/foo -XPUT -d value=bar -d ttl=5
curl http://127.0.0.1:2379/v2/keys/foo -XPUT -d ttl=5 -d refresh=true -d prevExist=true
{
    "action": "set",
    "node": {
        "createdIndex": 5,
        "expiration": "2013-12-04T12:01:21.874888581-08:00",
        "key": "/foo",
        "modifiedIndex": 5,
        "ttl": 5,
        "value": "bar"
    }
}
{
   "action":"update",
   "node":{
       "key":"/foo",
       "value":"bar",
       "expiration": "2013-12-04T12:01:26.874888581-08:00",
       "ttl":5,
       "modifiedIndex":6,
       "createdIndex":5
    },
   "prevNode":{
       "key":"/foo",
       "value":"bar",
       "expiration":"2013-12-04T12:01:21.874888581-08:00",
       "ttl":3,
       "modifiedIndex":5,
       "createdIndex":5
     }
}

等待更改 (鍵更改進行通知)

我們可以觀看一個關鍵字的更改,並通過使用長輪詢接收通知。 這也可以通過在curl中傳遞recursive=true來實現。

curl http://127.0.0.1:2379/v2/keys/foo?wait=true

現在我們正在等待路徑/foo的任何更改。

在另一個終端,我們設置鍵/foo 值bar:

curl http://127.0.0.1:2379/v2/keys/foo -XPUT -d value=bar

第一個終端應該得到通知並返回與設置請求相同的響應:

{
    "action": "set",
    "node": {
        "createdIndex": 7,
        "key": "/foo",
        "modifiedIndex": 7,
        "value": "bar"
    },
    "prevNode": {
        "createdIndex": 6,
        "key": "/foo",
        "modifiedIndex": 6,
        "value": "bar"
    }
}

watch 命令可以做更多的事情。 利用索引, 我們可以看到過去發生的命令。這對於確保不要錯過watch命令之間的事件很有用。 通常,我們從我們獲得的節點的modifiedIndex + 1再次觀看。

試一下 index 爲 7的情況

curl 'http://127.0.0.1:2379/v2/keys/foo?wait=true&waitIndex=7'

watch 立刻返回和原來一樣的情況。

在試一下 8 的情況

curl 'http://127.0.0.1:2379/v2/keys/foo?wait=true&waitIndex=8'

不管9還是800, /foo 第一個返回的是8和當前索引之間的事件進行返回

注意:etcd只保留所有etcd鍵中最近1000個事件的響應。 建議將響應發送給另一個線程以立即處理,而不是在處理結果時阻止watch。

清除事件索引觀察

如果我們錯過了所有1000個事件,我們需要通過獲取恢復觀看密鑰空間的當前狀態,然後從X-Etcd-Index + 1開始觀察。

例如,我們將/other=“bar”設置爲2000次,並嘗試從索引8中等待。

curl 'http://127.0.0.1:2379/v2/keys/foo?wait=true&waitIndex=8'

我們得到的索引是過時的響應,因爲我們錯過了保存在etcd中的1000個事件。

{"errorCode":401,"message":"The event in requested index is outdated and cleared","cause":"the requested history has been cleared [1008/8]","index":2007}

要開始觀看,首先我們需要獲取當前鍵/foo狀態:

curl 'http://127.0.0.1:2379/v2/keys/foo' -vv
< HTTP/1.1 200 OK
< Content-Type: application/json
< X-Etcd-Cluster-Id: 7e27652122e8b2ae
< X-Etcd-Index: 2007
< X-Raft-Index: 2615
< X-Raft-Term: 2
< Date: Mon, 05 Jan 2015 18:54:43 GMT
< Transfer-Encoding: chunked
<
{"action":"get","node":{"key":"/foo","value":"bar","modifiedIndex":7,"createdIndex":7}}

與watch不同,我們使用X-Etcd-Index + 1作爲waitIndex,而不是節點的modifiedIndex + 1,原因有二:

  1. 當獲取鍵時,X-Etcd-Index始終大於或等於modifiedIndex,因爲X-Etcd-Index是當前的etcd索引,而modifiedIndex是已經存儲在etcd中的事件的索引。
  2. modifiedIndex和X-Etcd-Index之間的索引表示的事件都不會與正在獲取的密鑰相關。

使用modifiedIndex + 1在後續watch上功能上相當,但由於它小於X-Etcd-Index + 1,我們可能會立即收到401 EventIndexCleared錯誤。

所以第一次看後應該是:

curl 'http://127.0.0.1:2379/v2/keys/foo?wait=true&waitIndex=2008'
連接過早關閉

在發佈任何事件之前,服務器可能會關閉長輪詢連接。 這可能是由於超時或服務器關閉。 由於HTTP標頭在接受連接後立即發送,響應將被視爲空:200 OK和空體。 客戶應該準備好處理這種情況並重試 watch。

POST (包含目錄的key)

(也就是用這個可以創建多層表示的key, 如 /message/001 和 /message/002 互斥)

在目錄上 使用post 可以創建原子性的名稱。這可以用於各種有用的模式,例如實現需要以嚴格順序處理的key的隊列。 一個示例用例將確保客戶端可以公平地訪問互斥體。

curl http://127.0.0.1:2379/v2/keys/queue -XPOST -d value=Job1
{
    "action": "create",
    "node": {
        "createdIndex": 6,
        "key": "/queue/00000000000000000006",
        "modifiedIndex": 6,
        "value": "Job1"
    }
}

如果稍後再創建另一個條目,則保證鍵名大於之前的鍵。 還要注意鍵名使用全局etcd索引,所以下一個鍵可以超過以前的+ 1。

curl http://127.0.0.1:2379/v2/keys/queue -XPOST -d value=Job2
{
    "action": "create",
    "node": {
        "createdIndex": 29,
        "key": "/queue/00000000000000000029",
        "modifiedIndex": 29,
        "value": "Job2"
    }
}

要將按順序的鍵列舉爲排序列表,請使用“sorted”參數。

curl -s 'http://127.0.0.1:2379/v2/keys/queue?recursive=true&sorted=true'
{
    "action": "get",
    "node": {
        "createdIndex": 2,
        "dir": true,
        "key": "/queue",
        "modifiedIndex": 2,
        "nodes": [
            {
                "createdIndex": 2,
                "key": "/queue/00000000000000000002",
                "modifiedIndex": 2,
                "value": "Job1"
            },
            {
                "createdIndex": 3,
                "key": "/queue/00000000000000000003",
                "modifiedIndex": 3,
                "value": "Job2"
            }
        ]
    }
}

目錄ttl

像鍵一樣,etcd中的目錄可以設置爲在指定的秒數之後到期。 您可以通過在使用PUT創建目錄時設置TTL(生存時間)來實現此目的:

curl http://127.0.0.1:2379/v2/keys/dir -XPUT -d ttl=30 -d dir=true
{
    "action": "set",
    "node": {
        "createdIndex": 17,
        "dir": true,
        "expiration": "2013-12-11T10:37:33.689275857-08:00",
        "key": "/dir",
        "modifiedIndex": 17,
        "ttl": 30
    }
}

目錄的TTL可以通過更新進行刷新。 您可以通過使用prevExist = true和新的TTL進行PUT來執行此操作。

curl http://127.0.0.1:2379/v2/keys/dir -XPUT -d ttl=30 -d dir=true -d prevExist=true

這個目錄下的鍵照常工作,但當目錄過期時,目錄下的鍵上的觀察者將獲得到期事件:

curl 'http://127.0.0.1:2379/v2/keys/dir?wait=true'
{
    "action": "expire",
    "node": {
        "createdIndex": 8,
        "key": "/dir",
        "modifiedIndex": 15
    },
    "prevNode": {
        "createdIndex": 8,
        "key": "/dir",
        "dir":true,
        "modifiedIndex": 17,
        "expiration": "2013-12-11T10:39:35.689275857-08:00"
    }
}

原子比較和交換

etcd可以用作集羣中的集中式協調服務,CompareAndSwap(CAS)是用於構建分佈式鎖服務的最基本的操作。

只有當客戶端提供的條件等於當前條件時,該命令纔會設置鍵的值。

請注意,CompareAndSwap不適用於目錄。 如果嘗試CompareAndSwap目錄,將返回102“不是文件”錯誤。

目前的可比較條件是:

  1. prevValue : 檢查鍵的上一個值。
  2. prevIndex : 檢查以前的modifyIndex的密鑰。
  3. prevExist : 檢查鍵的存在:如果prevExist爲真,則爲更新請求; 如果prevExist爲false,則爲創建請求。

我們先創建一個鍵值對:foo = 1。

curl http://127.0.0.1:2379/v2/keys/foo -XPUT -d value=one
{
    "action":"set",
    "node":{
        "key":"/foo",
        "value":"one",
        "modifiedIndex":4,
        "createdIndex":4
    }
}

指定noValueOnSuccess選項跳過返回節點作爲值。

curl http://127.0.0.1:2379/v2/keys/foo?noValueOnSuccess=true -XPUT -d value=one
# {"action":"set"}

現在讓我們嘗試一些無效的CompareAndSwap命令。

嘗試使用prevExist = false設置此現有鍵按預期方式失敗:

curl http://127.0.0.1:2379/v2/keys/foo?prevExist=false -XPUT -d value=three
{
    "cause": "/foo",
    "errorCode": 105,
    "index": 39776,
    "message": "Key already exists"
}

現在我們來提供一個prevValue參數:

curl http://127.0.0.1:2379/v2/keys/foo?prevValue=two -XPUT -d value=three

這將嘗試比較鍵的先前值和我們提供的先前值。 如果它們相等,則鍵值將變爲3。

{
    "cause": "[two != one]",
    "errorCode": 101,
    "index": 8,
    "message": "Compare failed"
}

這意味着CompareAndSwap失敗。 原因解釋了測試失敗的原因。 注意:條件prevIndex = 0始終通過。

讓我們嘗試一個有效的條件:

curl http://127.0.0.1:2379/v2/keys/foo?prevValue=one -XPUT -d value=two
{
    "action": "compareAndSwap",
    "node": {
        "createdIndex": 8,
        "key": "/foo",
        "modifiedIndex": 9,
        "value": "two"
    },
    "prevNode": {
    	"createdIndex": 8,
    	"key": "/foo",
    	"modifiedIndex": 8,
    	"value": "one"
    }
}

自從我們給出了正確的上一個值之後,我們成功地將值從“one”更改爲“two”。

原子比較和刪除

只有當客戶端提供的條件等於當前條件時,該命令纔會刪除一個鍵。

請注意,CompareAndDelete不適用於目錄。 如果嘗試CompareAndDelete目錄,將返回102“不是文件”錯誤。

目前的可比較條件是:

  1. prevValue - 檢查鍵的上一個值。
  2. prevIndex - 檢查以前的modifyIndex的密鑰。

我們先創建一個鍵:foo = 1。

curl http://127.0.0.1:2379/v2/keys/foo -XPUT -d value=one

現在我們來試一下CompareAndDelete命令。

嘗試刪除與prevValue = 2的鍵失敗如預期:

curl http://127.0.0.1:2379/v2/keys/foo?prevValue=two -XDELETE
{
	"errorCode": 101,
	"message": "Compare failed",
	"cause": "[two != one]",
	"index": 8
}

與具有不匹配的prevIndex的CompareAndDelete一樣:

curl http://127.0.0.1:2379/v2/keys/foo?prevIndex=1 -XDELETE
{
	"errorCode": 101,
	"message": "Compare failed",
	"cause": "[1 != 8]",
	"index": 8
}

現在有一個有效的prevValue條件:

curl http://127.0.0.1:2379/v2/keys/foo?prevValue=one -XDELETE
{
	"action": "compareAndDelete",
	"node": {
		"key": "/foo",
		"modifiedIndex": 9,
		"createdIndex": 8
	},
	"prevNode": {
		"key": "/foo",
		"value": "one",
		"modifiedIndex": 8,
		"createdIndex": 8
	}
}

目錄

創建目錄

在大多數情況下,鍵的目錄是自動創建的。但有些情況下,您需要創建一個目錄或刪除一個目錄。

創建目錄就像一個鍵,除了不能提供一個值,必須添加dir = true參數。

curl http://127.0.0.1:2379/v2/keys/dir -XPUT -d dir=true
{
    "action": "set",
    "node": {
        "createdIndex": 30,
        "dir": true,
        "key": "/dir",
        "modifiedIndex": 30
    }
}

查看目錄

在etcd中,我們可以存儲兩種類型的東西:鍵和目錄。 鍵存儲單個字符串值。 目錄存儲一組鍵和/或其他目錄。

在這個例子中,我們先創建一些鍵:

我們已經有/ foo = 2,所以現在我們將創建另一個名爲/ foo_dir / foo,值爲bar:

curl http://127.0.0.1:2379/v2/keys/foo_dir/foo -XPUT -d value=bar
{
    "action": "set",
    "node": {
        "createdIndex": 2,
        "key": "/foo_dir/foo",
        "modifiedIndex": 2,
        "value": "bar"
    }
}

現在我們可以在root / 下列出鍵:

curl http://127.0.0.1:2379/v2/keys/
{
    "action": "get",
    "node": {
        "key": "/",
        "dir": true,
        "nodes": [
            {
                "key": "/foo_dir",
                "dir": true,
                "modifiedIndex": 2,
                "createdIndex": 2
            },
            {
                "key": "/foo",
                "value": "two",
                "modifiedIndex": 1,
                "createdIndex": 1
            }
        ]
    }
}

這裏我們可以看到/foo是一個鍵值對,在/ and /foo_dir是一個目錄。 我們還可以通過添加recursive = true來遞歸地獲取目錄下的所有內容。

curl http://127.0.0.1:2379/v2/keys/?recursive=true
{
    "action": "get",
    "node": {
        "key": "/",
        "dir": true,
        "nodes": [
            {
                "key": "/foo_dir",
                "dir": true,
                "nodes": [
                    {
                        "key": "/foo_dir/foo",
                        "value": "bar",
                        "modifiedIndex": 2,
                        "createdIndex": 2
                    }
                ],
                "modifiedIndex": 2,
                "createdIndex": 2
            },
            {
                "key": "/foo",
                "value": "two",
                "modifiedIndex": 1,
                "createdIndex": 1
            }
        ]
    }
}

刪除目錄

現在我們試試刪除目錄/ foo_dir。

您可以使用DELETE動詞和dir = true參數刪除空目錄。

curl 'http://127.0.0.1:2379/v2/keys/foo_dir?dir=true' -XDELETE
{
    "action": "delete",
    "node": {
        "createdIndex": 30,
        "dir": true,
        "key": "/foo_dir",
        "modifiedIndex": 31
    },
    "prevNode": {
    	"createdIndex": 30,
    	"key": "/foo_dir",
    	"dir": true,
    	"modifiedIndex": 30
    }
}

要刪除保存鍵的目錄,必須添加recursive = true。

curl http://127.0.0.1:2379/v2/keys/dir?recursive=true -XDELETE
{
    "action": "delete",
    "node": {
        "createdIndex": 10,
        "dir": true,
        "key": "/dir",
        "modifiedIndex": 11
    },
    "prevNode": {
    	"createdIndex": 10,
    	"dir": true,
    	"key": "/dir",
    	"modifiedIndex": 10
    }
}

創建一個隱藏節點

我們可以通過添加_前綴來創建一個隱藏的鍵值對或目錄。 當發送目錄的GET請求時,隱藏的項目將不被列出。

首先我們將添加一個名爲/_message的隱藏鍵:

curl http://127.0.0.1:2379/v2/keys/_message -XPUT -d value="Hello hidden world"
{
    "action": "set",
    "node": {
        "createdIndex": 3,
        "key": "/_message",
        "modifiedIndex": 3,
        "value": "Hello hidden world"
    }
}

現在讓我們嘗試獲得根目錄下的鍵列表,/:

curl http://127.0.0.1:2379/v2/keys/
{
    "action": "get",
    "node": {
        "dir": true,
        "key": "/",
        "nodes": [
            {
                "createdIndex": 2,
                "dir": true,
                "key": "/foo_dir",
                "modifiedIndex": 2
            },
            {
                "createdIndex": 4,
                "key": "/message",
                "modifiedIndex": 4,
                "value": "Hello world"
            }
        ]
    }
}

這裏我們看到/message鍵,但我們隱藏的/_message鍵不會被返回。

從文件設置一個鍵

您還可以使用etcd直接存儲小型配置文件,JSON文檔,XML文檔等。 例如,您可以使用curl上傳一個簡單的文本文件並進行編碼:

echo "Hello\nWorld" > afile.txt
curl http://127.0.0.1:2379/v2/keys/afile -XPUT --data-urlencode [email protected]
{
    "action": "get",
    "node": {
        "createdIndex": 2,
        "key": "/afile",
        "modifiedIndex": 2,
        "value": "Hello\nWorld\n"
    }
}

線性化讀取 (沒理解)

如果你想要一個完全線性化的閱讀,你可以使用一個quorum = true GET。 讀取將採取非常類似的寫入路徑,並將具有相似的速度。 如果您不確定是否需要此功能,請隨時發送電子郵件至etcd-dev以獲取建議。

統計

一個etcd集羣跟蹤一些統計數據,包括延遲,帶寬和正常運行時間。 這些通過統計端點公開,以瞭解集羣的內部運行狀況。

Leader 統計

Leader可以看到整個集羣,並跟蹤兩個有趣的統計信息:集羣中每個對等體的延遲以及失敗和成功的Raft RPC請求數。 您可以從/v2/stats/leader端點獲取這些統計信息:

curl http://127.0.0.1:2379/v2/stats/leader

{
    "followers": {
        "6e3bd23ae5f1eae0": {
            "counts": {
                "fail": 0,
                "success": 745
            },
            "latency": {
                "average": 0.017039507382550306,
                "current": 0.000138,
                "maximum": 1.007649,
                "minimum": 0,
                "standardDeviation": 0.05289178277920594
            }
        },
        "a8266ecf031671f3": {
            "counts": {
                "fail": 0,
                "success": 735
            },
            "latency": {
                "average": 0.012124141496598642,
                "current": 0.000559,
                "maximum": 0.791547,
                "minimum": 0,
                "standardDeviation": 0.04187900156583733
            }
        }
    },
    "leader": "924e2e83e93f2560"
}

自我統計

  1. 領導者(follower)
  2. 追隨者(follower)

每個節點保留一些內部統計信息:

  • id:成員的唯一標識符
  • leaderInfo.leader:當前領導成員的ID
  • leaderInfo.uptime:領導者領導的時間
  • name:這個成員的名字
  • recvAppendRequestCnt:此節點已處理的附加請求數
  • recvBandwidthRate:此節點接收的每秒字節數(僅限隨機數)
  • recvPkgRate:此節點接收的每秒請求數(僅跟隨者)
  • sendAppendRequestCnt:此節點發送的請求數
  • sendBandwidthRate:此節點發送的每秒字節數(僅限於引導者)。 該值在單個成員集羣上未定義。
  • sendPkgRate:此節點發送的每秒請求數(僅限於引導者)。 該值在單個成員集羣上未定義。
  • state:任何領導者或追隨者
  • startTime:此節點啓動的時間

這是來自跟隨者成員的示例響應:

curl http://127.0.0.1:2379/v2/stats/self
{
    "id": "eca0338f4ea31566",
    "leaderInfo": {
        "leader": "8a69d5f6b7814500",
        "startTime": "2014-10-24T13:15:51.186620747-07:00",
        "uptime": "10m59.322358947s"
    },
    "name": "node3",
    "recvAppendRequestCnt": 5944,
    "recvBandwidthRate": 570.6254930219969,
    "recvPkgRate": 9.00892789741075,
    "sendAppendRequestCnt": 0,
    "startTime": "2014-10-24T13:15:50.072007085-07:00",
    "state": "StateFollower"
}

這是領導成員的一個例子:

curl http://127.0.0.1:2379/v2/stats/self
{
    "id": "924e2e83e93f2560",
    "leaderInfo": {
        "leader": "924e2e83e93f2560",
        "startTime": "2015-02-09T11:38:30.177534688-08:00",
        "uptime": "9m33.891343412s"
    },
    "name": "infra3",
    "recvAppendRequestCnt": 0,
    "sendAppendRequestCnt": 6535,
    "sendBandwidthRate": 824.1758351191694,
    "sendPkgRate": 11.111234716807138,
    "startTime": "2015-02-09T11:38:28.972034204-08:00",
    "state": "StateLeader"
}

存儲統計

存儲統計信息包括有關此節點處理的操作的信息。 請注意,v2存儲統計信息存儲在內存中。 當成員停止時,重新啓動時將重新存儲統計信息。

整個羣集都會看到修改商店狀態(如創建,刪除,設置和更新)的操作,所有節點上的數量都會增加。 像get和watch這樣的操作是本地節點,只能在這個節點上看到。

curl http://127.0.0.1:2379/v2/stats/store
{
    "compareAndSwapFail": 0,
    "compareAndSwapSuccess": 0,
    "createFail": 0,
    "createSuccess": 2,
    "deleteFail": 0,
    "deleteSuccess": 0,
    "expireCount": 0,
    "getsFail": 4,
    "getsSuccess": 75,
    "setsFail": 2,
    "setsSuccess": 4,
    "updateFail": 0,
    "updateSuccess": 0,
    "watchers": 0
}

後面繼續補充

PS: 覺得不錯的請點個贊吧!! (ง •̀_•́)ง

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章