使用docker-compose搭建etcd集群环境

使用docker-compose搭建etcd集群环境

etcd是一个集群环境,用来管理微服务架构下面的配置管理功能。
A distributed, reliable key-value store for the most critical data of a distributed system.

这篇文章是一个基础步骤如何搭建etcd的docker集群环境。
我们使用docker-compose来搭建如下的etcd集群环境:

  1. 集群包含三个node:etcd1, etcd2, etcd3
  1. 下载consul docker image
$ docker pull quay.io/coreos/etcd
  1. 编辑docker-compose.yaml文件
$ cat docker-compose.yaml
version: '2'
networks:
  byfn:

services:
  etcd1:
    image: quay.io/coreos/etcd
    container_name: etcd1
    command: etcd -name etcd1 -advertise-client-urls http://0.0.0.0:2379 -listen-client-urls http://0.0.0.0:2379 -listen-peer-urls http://0.0.0.0:2380 -initial-cluster-token etcd-cluster -initial-cluster "etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380" -initial-cluster-state new
    ports:
      - 2379
      - 2380
    networks:
      - byfn

  etcd2:
    image: quay.io/coreos/etcd
    container_name: etcd2
    command: etcd -name etcd2 -advertise-client-urls http://0.0.0.0:2379 -listen-client-urls http://0.0.0.0:2379 -listen-peer-urls http://0.0.0.0:2380 -initial-cluster-token etcd-cluster -initial-cluster "etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380" -initial-cluster-state new
    ports:
      - 2379
      - 2380
    networks:
      - byfn

  etcd3:
    image: quay.io/coreos/etcd
    container_name: etcd3
    command: etcd -name etcd3 -advertise-client-urls http://0.0.0.0:2379 -listen-client-urls http://0.0.0.0:2379 -listen-peer-urls http://0.0.0.0:2380 -initial-cluster-token etcd-cluster -initial-cluster "etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380" -initial-cluster-state new
    ports:
      - 2379
      - 2380
    networks:
      - byfn
  1. 启动服务
$ docker-compose up
  1. 验证集群的状态

验证从三个node返回的v2/members数据是一样的值。

$ docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                                              NAMES
33785a959d95        quay.io/coreos/etcd   "etcd -name etcd1 ..."   2 hours ago         Up 2 hours          0.0.0.0:32791->2379/tcp, 0.0.0.0:32790->2380/tcp   etcd1
106ba12b1c25        quay.io/coreos/etcd   "etcd -name etcd2 ..."   2 hours ago         Up 2 hours          0.0.0.0:32789->2379/tcp, 0.0.0.0:32788->2380/tcp   etcd2
76cd127439a3        quay.io/coreos/etcd   "etcd -name etcd3 ..."   2 hours ago         Up 2 hours          0.0.0.0:32787->2379/tcp, 0.0.0.0:32786->2380/tcp   etcd3

$ curl -L http://127.0.0.1:32787/v2/members
{"members":[{"id":"ade526d28b1f92f7","name":"etcd1","peerURLs":["http://etcd1:2380"],"clientURLs":["http://0.0.0.0:2379"]},{"id":"bd388e7810915853","name":"etcd3","peerURLs":["http://etcd3:2380"],"clientURLs":["http://0.0.0.0:2379"]},{"id":"d282ac2ce600c1ce","name":"etcd2","peerURLs":["http://etcd2:2380"],"clientURLs":["http://0.0.0.0:2379"]}]}

$ curl -L http://127.0.0.1:32789/v2/members
{"members":[{"id":"ade526d28b1f92f7","name":"etcd1","peerURLs":["http://etcd1:2380"],"clientURLs":["http://0.0.0.0:2379"]},{"id":"bd388e7810915853","name":"etcd3","peerURLs":["http://etcd3:2380"],"clientURLs":["http://0.0.0.0:2379"]},{"id":"d282ac2ce600c1ce","name":"etcd2","peerURLs":["http://etcd2:2380"],"clientURLs":["http://0.0.0.0:2379"]}]}

$ curl -L http://127.0.0.1:32791/v2/members
{"members":[{"id":"ade526d28b1f92f7","name":"etcd1","peerURLs":["http://etcd1:2380"],"clientURLs":["http://0.0.0.0:2379"]},{"id":"bd388e7810915853","name":"etcd3","peerURLs":["http://etcd3:2380"],"clientURLs":["http://0.0.0.0:2379"]},{"id":"d282ac2ce600c1ce","name":"etcd2","peerURLs":["http://etcd2:2380"],"clientURLs":["http://0.0.0.0:2379"]}]}

也可以用命令行工具etcdctl:

$ docker exec -t etcd1 etcdctl member list
ade526d28b1f92f7: name=etcd1 peerURLs=http://etcd1:2380 clientURLs=http://0.0.0.0:2379 isLeader=false
bd388e7810915853: name=etcd3 peerURLs=http://etcd3:2380 clientURLs=http://0.0.0.0:2379 isLeader=false
d282ac2ce600c1ce: name=etcd2 peerURLs=http://etcd2:2380 clientURLs=http://0.0.0.0:2379 isLeader=true

$ docker exec -t etcd3 etcdctl -C http://etcd1:2379,http://etcd2:2379,http://etcd3:2379 member list
ade526d28b1f92f7: name=etcd1 peerURLs=http://etcd1:2380 clientURLs=http://0.0.0.0:2379 isLeader=false
bd388e7810915853: name=etcd3 peerURLs=http://etcd3:2380 clientURLs=http://0.0.0.0:2379 isLeader=false
d282ac2ce600c1ce: name=etcd2 peerURLs=http://etcd2:2380 clientURLs=http://0.0.0.0:2379 isLeader=true
  1. 集群的使用

我们往一个node上上传数据,在其他node上就能下载到。

curl -L http://127.0.0.1:32789/v2/keys/foo -XPUT -d value="Hello foo"
curl -L http://127.0.0.1:32789/v2/keys/foo1/foo1 -XPUT -d value="Hello foo1"
curl -L http://127.0.0.1:32789/v2/keys/foo2/foo2 -XPUT -d value="Hello foo2"
curl -L http://127.0.0.1:32789/v2/keys/foo2/foo21/foo21 -XPUT -d value="Hello foo21"

curl -L http://127.0.0.1:32787/v2/keys/foo
curl -L http://127.0.0.1:32787/v2/keys/foo2
curl -L http://127.0.0.1:32787/v2/keys/foo2?recursive=true

etcd的详细API请参考相关文档。

推荐阅读更多精彩内容

  • 在互联网应用领域,服务的动态性需求十分常见,这就对服务的自动发现和可动态扩展提出了很高的要求。 微服务系统动辄上万...
    Liberalman阅读 6,502评论 23 81
  • 货币与区块链探索比特币的设计细节聊聊共识的逻辑和应用场景去伪存真“去中心化”代币经济学ICO,尚未完成的超级金融创...
    墨矩阅读 26评论 0 0
  • 人生就是一场修行。对于一个女性来说,人生是更为艰难的一场修行。 首先,作为一个独立的个人,我们需要有独立生活的能力...
    田小丫_Stella阅读 124评论 0 2
  • 凌晨四点半,窗外下着小雨,我被设定的闹铃叫醒。今天早上有一个任务,送黑大哥去机场。 四点五十,洗漱完毕,开车去酒店...
    BIEVO阅读 49评论 0 0
  • 平时什么事都自己做的女汉子,即使再逞强也要坚持着自己做完,只因为不想让别人帮忙。 可是生病的时候真的是有心无力,难...
    einzeln阅读 38评论 0 0