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

 

posted @ 2016-03-23 17:44  振宇要低调  阅读(8633)  评论(0编辑  收藏  举报