搭建
docker-compose.yml
version: '3'
networks:
etcd-net:
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:
- etcd-net
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:
- etcd-net
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:
- etcd-net
集群参数介绍
data-dir 指定节点的数据存储目录,这些数据包括节点ID,集群ID,集群初始化配置,Snapshot文件,若未指定—wal-dir,还会存储WAL文件;
wal-dir 指定节点的was文件的存储目录,若指定了该参数,wal文件会和其他数据文件分开存储。
name 节点名称
initial-advertise-peer-urls 告知集群其他节点url.
listen-peer-urls 监听URL,用于与其他节点通讯
advertise-client-urls 告知客户端url, 也就是服务的url
initial-cluster-token 集群的ID
initial-cluster 集群中所有节点
initial-cluster-state 监听客户端状态
listen-client-urls 监听客户端地址
initial-cluster-state new 初始化集群 为新节点
启动服务
docker-compose up -d
测试
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