目录
一.容器搭建
1.拉取etcd的镜像
docker pull quay.io/coreos/etcd:v3.3.1
2.创建一个自定义网络
因为要设置容器的IP(默认的网络模式IP不固定)
docker network create --subnet 172.19.0.0/16 ots
容器一(172.19.0.110): 2379:2379,2380:2380
容器一(172.19.0.120): 2479:2379,2480:2380
容器一(172.19.0.130): 2579:2379,2580:2380
3.创建容器命令
第一台
docker run -d \
-p 2379:2379 -p 2380:2380 \
--name node1 --network=ots \
--ip 172.19.0.110 \
quay.io/coreos/etcd:v3.3.1 \
etcd \
-name node1 \
-advertise-client-urls http://172.19.0.110:2379 \
-initial-advertise-peer-urls http://172.19.0.110: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 "node1=http://172.19.0.110:2380,node2=http://172.19.0.120:2380,node3=http://172.19.0.130:2380" \
-initial-cluster-state new
第二台
docker run -d \
-p 2479:2379 -p 2480:2380 \
--name node2 --network=ots \
--ip 172.19.0.120 \
quay.io/coreos/etcd:v3.3.1 \
etcd \
-name node2 \
-advertise-client-urls http://172.19.0.120:2379 \
-initial-advertise-peer-urls http://172.19.0.120: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 "node1=http://172.19.0.110:2380,node2=http://172.19.0.120:2380,node3=http://172.19.0.130:2380" \
-initial-cluster-state new
第三台
docker run -d \
-p 2579:2379 -p 2580:2380 \
--name node3 --network=ots \
--ip 172.19.0.130 \
quay.io/coreos/etcd:v3.3.1 \
etcd \
-name node3 \
-advertise-client-urls http://172.19.0.130:2379 \
-initial-advertise-peer-urls http://172.19.0.130: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 "node1=http://172.19.0.110:2380,node2=http://172.19.0.120:2380,node3=http://172.19.0.130:2380" \
-initial-cluster-state new
这是标红参数的解释:
--listen-client-urls #监听 URL;用于与客户端通讯
--listen-peer-urls #监听 URL;用于与其他节点通讯
--initial-advertise-peer-urls #告知集群其他节点 URL
--advertise-client-urls #告知客户端 URL
--initial-cluster-token #集群的 ID
--initial-cluster #集群中所有节点
--initial-cluster-state new #表示从无到有搭建 etcd 集群
使用docker ps -a 查看所有容器都在运行状态
二.容器集群测试
1.进入其中两台容器(开两个窗口)
docker exec -it node1 sh
docker exec -it node2 sh
往node2里面添加一个数据
etcdctl set /aa/bb 123
在node1里面拿数据
etcdctl get /aa/bb
这表示它们两个已经完成同步了,也说明集群搭建好了
2.查看所有节点
etcdctl member list
可以看到node1最后面的true,这代表它是主节点 !
好啦 文章内容到这里就结束了 你学到了吗
觉得有用的记得关注点赞加收藏哦 我的etcd专栏还有其它关于etcd的技术点哦~
我敲BUG的etcd专栏:http://t.csdn.cn/OEEW8