使用docker-compose搭建etcd集群

1. 下载ectd镜像

docker pull quay.io/coreos/etcd

2. 编辑docker-compose.yaml文件

version: '3'
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:
      - "23791:2379"
      - "23801: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:
      - "23792:2379"
      - "23802: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:
      - "23793:2379"
      - "23803:2380"
    networks:
      - byfn

3. 启动服务

 docker-compose up

4. 查看集群状态

$ docker ps
792cd737a147        quay.io/coreos/etcd                           "etcd -name etcd3 -a…"   14 hours ago        Up 34 minutes       0.0.0.0:23793->2379/tcp, 0.0.0.0:23803->2380/tcp   etcd3
fb3be391c3c3        quay.io/coreos/etcd                           "etcd -name etcd1 -a…"   14 hours ago        Up 34 minutes       0.0.0.0:23791->2379/tcp, 0.0.0.0:23801->2380/tcp   etcd1
1c4361e3983b        quay.io/coreos/etcd                           "etcd -name etcd2 -a…"   14 hours ago        Up 34 minutes       0.0.0.0:23792->2379/tcp, 0.0.0.0:23802->2380/tcp   etcd2

5. 使用集群: 从一个节点添加数据,另一个节点获取数据

curl http://127.0.0.1:23791/v2/keys/foo  -XPUT -d value="hello foo"
curl http://127.0.0.1:23791/v2/keys/foo1/foo1 -XPUT -d value="hello foo1"


curl http://127.0.0.1:23792/v2/keys/foo
curl http://127.0.0.1:23792/v2/keys/foo1

你可能感兴趣的:(使用docker-compose搭建etcd集群)