etcd用于服務發現的基礎注冊和通知,功能類似于zk,通過注冊和監聽,實作基礎的服務發現。
etcd安裝非常簡單,可以用go自己編譯,etcd也提供了可以直接使用的二進制包(64位)。
具體的安裝提示頁面在github上,
直接按照上面的描述下載下傳即可。為了友善,把裡面的etcd相關的二進制檔案(etcd, etcdctl等)
複制到了/usr/local/bin中,友善後續使用。
首先嘗試單機版啟動,參照手冊先直接啟動,etcd預設監聽的是localhost,既隻監聽了lo裝置,
這樣會導緻啟動後叢集中的其他機器無法通路,是以在啟動的時候将預設的localhost改成0.0.0.0,
確定etcd監聽了所有網卡。
啟動之後可通過rest接口或者etcdctl執行指令:
輸出結果為:
{“releaseversion”:”2.0.0″,”internalversion”:”2″}
簡單寫入和讀取:
{“action”:”set”,”node”:{“key”:”/message”,”value”:”hello world”,”modifiedindex”:3,”createdindex”:3}}
</blockquote>
{“action”:”get”,”node”:{“key”:”/message”,”value”:”hello world”,”modifiedindex”:3,”createdindex”:3}}
之前的啟動方式為單機啟動,叢集建立官方給了很多種方式,這裡嘗試動态添加機器的方式。
啟動之後,通過member api,可以檢視到目前叢集:
{“members”:[{“id”:”f0b31008acf03099″,”name”:”node1″,”peerurls”:[“http://10.211.55.11:2380″,”http://10.211.55.11:7001″],”clienturls”:[“http://localhost:2379″,”http://localhost:4001″]}]}
向叢集中添加一台機器:
added member named node2 with id 6d345c68496f80fc to cluster etcd_name=”node2″ etcd_initial_cluster=”node2=http://10.211.55.12:2380,node1=http://10.211.55.11:2380,node1=http://10.211.55.11:7001″ etcd_initial_cluster_state=”existing”
這裡為了友善,沒有隻用rest接口,直接使用了etcdctl,該指令會提示在啟動新的節點時所需要配置的環境變量。
啟動新的節點:
啟動之後,通過member接口檢視目前叢集機器:
293ea5ba1d70f5f1: name=node2 peerurls=http://10.211.55.12:2380 clienturls=http://0.0.0.0:4001 bd93686a68a54c2d: name=node1 peerurls=http://10.211.55.11:2380 clienturls=http://localhost:2379,http://localhost:4001
同樣的方式再添加一台,成為基礎的3台機器叢集:
最終叢集為:
76610041ace6c4f8: name=node3 peerurls=http://10.211.55.13:2380 clienturls=http://0.0.0.0:4001
在node1節點機器上運作:
之後,在三台機器中執行:
都能夠正确的擷取這個key的值:hello。
轉載自:https://coolex.info/blog/481.html