因为需要用到etcd,开发环境是wsl,所以要装一下 首先,安装docker,因为是windows10系统,直接安装docker Desktop
安装过程网上多的是,不再赘述。 安装完成后,要把docker调整成wsl
找右上角的齿轮按钮->选择Resources->选择WSL integration-> 设置Enable为wsl,打对号就行了
进入wsl,找一个目录,随便,你觉得舒服的目录就行,最好是空白的目录
比如我的是/root/work_space/etcd_cluster
执行命令创建n个目录,用于存放不同的节点
mkdir -p etcd-node{1,2,3,4,5}
因为我打算创建5个节点,如需要可以删减,最低不要低于3个,因为etcd要求是奇数个。
执行命令构建一个docker网络
docker network create --driver bridge --subnet 172.20.0.0/16 --gateway 172.20.0.1 etcd-cluster
构建docker-compose.yaml文件,注意,这个文件的名字默认最好是docker-compose.yaml,扩展名是yaml,如果你的docker有额外配置,或者指定了某个文件是另一说,如果没有,默认就是这个
version: '3'
networks:
etcd-cluster:
external: true
services:
etcd-node1:
image: quay.io/coreos/etcd:v3.5.5
container_name: etcd-node1
ports:
- "12379:2379"
- "12380:2380"
restart: always
volumes:
- /root/work_space/etcd_cluster/node1:/data/app/etcd
command: etcd --name etcd-node1 --data-dir /data/app/etcd/ --advertise-client-urls http://172.20.0.10:2379 --initial-advertise-peer-urls http://172.20.0.10:2380 --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 "etcd-node1=http://172.20.0.10:2380,etcd-node2=http://172.20.0.11:2380,etcd-node3=http://172.20.0.12:2380,etcd-node4=http://172.20.0.13:2380,etcd-node5=http://172.20.0.14:2380" --initial-cluster-state new
networks:
etcd-cluster:
ipv4_address: 172.20.0.10
etcd-node2:
image: quay.io/coreos/etcd:v3.5.5
container_name: etcd-node2
ports:
- "12381:2379"
- "12382:2380"
restart: always
volumes:
- /root/work_space/etcd_cluster/node2:/data/app/etcd
command: etcd --name etcd-node2 --data-dir /data/app/etcd/ --advertise-client-urls http://172.20.0.11:2379 --initial-advertise-peer-urls http://172.20.0.11:2380 --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 "etcd-node1=http://172.20.0.10:2380,etcd-node2=http://172.20.0.11:2380,etcd-node3=http://172.20.0.12:2380,etcd-node4=http://172.20.0.13:2380,etcd-node5=http://172.20.0.14:2380" --initial-cluster-state new
networks:
etcd-cluster:
ipv4_address: 172.20.0.11
etcd-node3:
image: quay.io/coreos/etcd:v3.5.5
container_name: etcd-node3
ports:
- "12383:2379"
- "12384:2380"
restart: always
volumes:
- /root/work_space/etcd_cluster/node3:/data/app/etcd
command: etcd --name etcd-node3 --data-dir /data/app/etcd/ --advertise-client-urls http://172.20.0.12:2379 --initial-advertise-peer-urls http://172.20.0.12:2380 --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 "etcd-node1=http://172.20.0.10:2380,etcd-node2=http://172.20.0.11:2380,etcd-node3=http://172.20.0.12:2380,etcd-node4=http://172.20.0.13:2380,etcd-node5=http://172.20.0.14:2380" --initial-cluster-state new
networks:
etcd-cluster:
ipv4_address: 172.20.0.12
etcd-node4:
image: quay.io/coreos/etcd:v3.5.5
container_name: etcd-node4
ports:
- "12385:2379"
- "12386:2380"
restart: always
volumes:
- /root/work_space/etcd_cluster/node4:/data/app/etcd
command: etcd --name etcd-node4 --data-dir /data/app/etcd/ --advertise-client-urls http://172.20.0.13:2379 --initial-advertise-peer-urls http://172.20.0.13:2380 --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 "etcd-node1=http://172.20.0.10:2380,etcd-node2=http://172.20.0.11:2380,etcd-node3=http://172.20.0.12:2380,etcd-node4=http://172.20.0.13:2380,etcd-node5=http://172.20.0.14:2380" --initial-cluster-state new
networks:
etcd-cluster:
ipv4_address: 172.20.0.13
etcd-node5:
image: quay.io/coreos/etcd:v3.5.5
container_name: etcd-node5
ports:
- "12387:2379"
- "12388:2380"
restart: always
volumes:
- /root/work_space/etcd_cluster/node5:/data/app/etcd
command: etcd --name etcd-node5 --data-dir /data/app/etcd/ --advertise-client-urls http://172.20.0.14:2379 --initial-advertise-peer-urls http://172.20.0.14:2380 --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 "etcd-node1=http://172.20.0.10:2380,etcd-node2=http://172.20.0.11:2380,etcd-node3=http://172.20.0.12:2380,etcd-node4=http://172.20.0.13:2380,etcd-node5=http://172.20.0.14:2380" --initial-cluster-state new
networks:
etcd-cluster:
ipv4_address: 172.20.0.14
这里用的是官方维护的自己的镜像 quay.io/coreos/etcd:v3.5.5 如果有需要可以换成其他的,这个无所谓
执行命令启动etcd集群
docker-compose up -d
第一次启动需要下载镜像,会慢一些
启动完成后,效果如下: