quay.io/coreos/etcd 基於Docker映象的叢集搭建

振宇要低調發表於2016-03-23

  etcd是一個高可用的鍵值儲存系統,主要用於共享配置和服務發現。etcd是由CoreOS開發並維護的,靈感來自於 ZooKeeper 和 Doozer,它使用Go語言編寫,並通過Raft一致性演算法處理日誌複製以保證強一致性。Raft是一個來自Stanford的新的一致性演算法,適用於分散式系統的日誌複製,Raft通過選舉的方式來實現一致性,在Raft中,任何一個節點都可能成為Leader。Google的容器叢集管理系統Kubernetes、開源PaaS平臺Cloud Foundry和CoreOS的Fleet都廣泛使用了etcd。etcd的特性如下:

    簡單: curl可訪問的使用者的API(HTTP+JSON)

    安全: 可選的SSL客戶端證書認證

    快速: 單例項每秒 1000 次寫操作

    可靠: 使用Raft保證一致性

  本次搭建的基礎環境:

底層OS:Centos7
docker版本:1.8.2-el7.centos
IP:
    伺服器A:192.168.7.168
    伺服器B:192.168.7.170
    伺服器C:192.168.7.172

  首先在各個伺服器上下載最新的etcd映象

# docker pull quay.io/coreos/etcd

接下來我採用了兩種方式來建立叢集:1、將三個伺服器挨個新增進叢集;2、將三個伺服器統一新增進叢集。以下命令標註A的代表在A機器上執行,同理B、C。

1、將伺服器挨個新增進叢集

  A  在伺服器A上執行一個ETCD例項,取名為qf2200-client0,注意其狀態為new,“-initial-cluster”中只有自己的IP

# docker run -d -p 2379:2379 -p 2380:2380 --name etcd quay.io/coreos/etcd -name qf2200-client0 -advertise-client-urls http://192.168.7.168:2379 -listen-client-urls http://0.0.0.0:2379 -initial-advertise-peer-urls http://192.168.7.168:2380 -listen-peer-urls http://0.0.0.0:2380 -initial-cluster-token etcd-cluster -initial-cluster "qf2200-client0=http://192.168.7.168:2380" -initial-cluster-state new

  

  A  在伺服器A的ETCD服務上,通過呼叫API新增一個新的節點:192.168.7.170

# curl http://127.0.0.1:2379/v2/members -XPOST -H "Content-Type: application/json" -d '{"peerURLs": ["http://192.168.7.170:2380"]}'

  

  B  在伺服器B上執行一個ETCD例項,取名為qf2200-client1,注意其狀態為existing,“-initial-cluster”中有前一個IP及自己的IP

# docker run -d -p 2379:2379 -p 2380:2380 --name etcd quay.io/coreos/etcd -name qf2200-client1 -advertise-client-urls http://192.168.7.170:2379 -listen-client-urls http://0.0.0.0:2379 -initial-advertise-peer-urls http://192.168.7.170:2380 -listen-peer-urls http://0.0.0.0:2380 -initial-cluster-token etcd-cluster -initial-cluster "qf2200-client0=http://192.168.7.168:2380,qf2200-client1=http://192.168.7.170:2380" -initial-cluster-state existing

  

  A  在伺服器A的ETCD服務上,通過呼叫API新增一個新的節點:192.168.7.172

# curl http://127.0.0.1:2379/v2/members -XPOST -H "Content-Type: application/json" -d '{"peerURLs": ["http://192.168.7.172:2380"]}'

  

  C 在伺服器C上執行一個ETCD例項,取名為qf2200-client2,注意其狀態為existing,“-initial-cluster”中有之前所有節點的IP及自己的IP

# docker run -d -p 2379:2379 -p 2380:2380 --name etcd quay.io/coreos/etcd -name qf2200-client2 -advertise-client-urls http://192.168.7.172:2379 -listen-client-urls http://0.0.0.0:2379 -initial-advertise-peer-urls http://192.168.7.172:2380 -listen-peer-urls http://0.0.0.0:2380 -initial-cluster-token etcd-cluster -initial-cluster "qf2200-client0=http://192.168.7.168:2380,qf2200-client1=http://192.168.7.170:2380,qf2200-client2=http://192.168.7.172:2380" -initial-cluster-state existing

 

2、將伺服器統一新增進叢集(“-initial-cluster”中包含所有節點的IP,狀態均為new)

   A上執行

# docker run -d -p 2379:2379 -p 2380:2380 --restart=always --log-driver=none --name etcd quay.io/coreos/etcd -name qf2200-client0 -advertise-client-urls http://192.168.7.168:2379 -listen-client-urls http://0.0.0.0:2379 -initial-advertise-peer-urls http://192.168.7.168:2380 -listen-peer-urls http://0.0.0.0:2380 -initial-cluster-token etcd-cluster -initial-cluster "qf2200-client0=http://192.168.7.168:2380,qf2200-client1=http://192.168.7.170:2380,qf2200-client2=http://192.168.7.172:2380" -initial-cluster-state new

  

  B上執行

# docker run -d -p 2379:2379 -p 2380:2380 --restart=always --log-driver=none --name etcd quay.io/coreos/etcd -name qf2200-client1 -advertise-client-urls http://192.168.7.170:2379 -listen-client-urls http://0.0.0.0:2379 -initial-advertise-peer-urls http://192.168.7.170:2380 -listen-peer-urls http://0.0.0.0:2380 -initial-cluster-token etcd-cluster -initial-cluster "qf2200-client0=http://192.168.7.168:2380,qf2200-client1=http://192.168.7.170:2380,qf2200-client2=http://192.168.7.172:2380" -initial-cluster-state new

  

  C上執行

# docker run -d -p 2379:2379 -p 2380:2380 --restart=always --log-driver=none --name etcd quay.io/coreos/etcd -name qf2200-client2 -advertise-client-urls http://192.168.7.172:2379 -listen-client-urls http://0.0.0.0:2379 -initial-advertise-peer-urls http://192.168.7.172:2380 -listen-peer-urls http://0.0.0.0:2380 -initial-cluster-token etcd-cluster -initial-cluster "qf2200-client0=http://192.168.7.168:2380,qf2200-client1=http://192.168.7.170:2380,qf2200-client2=http://192.168.7.172:2380" -initial-cluster-state new

 

叢集驗證。兩種方法建立的叢集可通過以下方式進行驗證

  1、驗證叢集members。在叢集中的每臺機器上檢視members,得出的結果應該是相同的

[root@localhost ~]# curl -L http://127.0.0.1:2379/v2/members
{"members":[{"id":"1a661f2b9997ba39","name":"qf2200-client0","peerURLs":["http://192.168.7.168:2380"],"clientURLs":["http://192.168.7.168:2379"]},{"id":"4932c8ea462e079c","name":"qf2200-client2","peerURLs":["http://192.168.7.172:2380"],"clientURLs":["http://192.168.7.172:2379"]},{"id":"c1dbdde07e61741e","name":"qf2200-client1","peerURLs":["http://192.168.7.170:2380"],"clientURLs":["http://192.168.7.170:2379"]}]}

  2、某臺機器上新增資料,其他機器上檢視資料,得出的結果應該是相同的
  A上執行

[root@localhost ~]# curl -L http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello zhenyuyaodidiao"
{"action":"set","node":{"key":"/message","value":"Hello zhenyuyaodidiao","modifiedIndex":13,"createdIndex":13},"prevNode":{"key":"/message","value":"Hello world1","modifiedIndex":11,"createdIndex":11}}

  

  B、C上執行

[root@localhost ~]#  curl -L http://127.0.0.1:2379/v2/keys/message
{"action":"get","node":{"key":"/message","value":"Hello zhenyuyaodidiao","modifiedIndex":13,"createdIndex":13}}

 

相關文章