微服务自动化之etcd的集群搭建(容器搭建)

目录

一.容器搭建

1.拉取etcd的镜像

2.创建一个自定义网络

3.创建容器命令

第一台

第二台

第三台

二.容器集群测试

1.进入其中两台容器(开两个窗口)

往node2里面添加一个数据

在node1里面拿数据

 这表示它们两个已经完成同步了,也说明集群搭建好了

2.查看所有节点 


一.容器搭建

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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值