其实关于集群网上说的方案已经很多了,尤其是官网,只是这里我个人只有一个虚拟机,在开发环境下建议用docker-compose来搭建etcd集群.
1.拉取etcd镜像
2.编辑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: - "12379: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: - "22379: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: - "32379:2379" - 2380 networks: - byfn
3.启动服务验证:
docker-compose up #docker-compose down curl -L http://127.0.0.1:12379/v2/members #查看etcd1的数据 curl -L http://127.0.0.1:22379/v2/members #查看etcd2的数据 curl -L http://127.0.0.1:32379/v2/members #查看etcd3的数据 #也可以用命令行工具etcdctl docker exec -t etcd1 etcdctl member list #我们往一个node上上传数据,在其他node上就能下载到 curl -L http://127.0.0.1:12379/v2/keys/foo -XPUT -d value="Hello foo" curl -L http://127.0.0.1:12379/v2/keys/foo1/foo1 -XPUT -d value="Hello foo1" curl -L http://127.0.0.1:12379/v2/keys/foo2/foo2 -XPUT -d value="Hello foo2" curl -L http://127.0.0.1:12379/v2/keys/foo2/foo21/foo21 -XPUT -d value="Hello foo21" curl -L http://127.0.0.1:22379/v2/keys/foo curl -L http://127.0.0.1:22379/v2/keys/foo2 curl -L http://127.0.0.1:22379/v2/keys/foo2?recursive=true
参考: