quay.io/coreos/etcd 基於Docker鏡像的集群搭建


  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}}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM