Zookeeper集群通过Docker搭建

Docker搭建Zookeeper集群(伪)

第一种方式(在Docker中模拟在liunx单机)

第一步

首先找一个目录创建三个文件夹,分别放置三个zk的zoo.cfg配置文件,例如我放在了/home/zookeeper

/home/zookeeper/
|-- zoo1
|   `-- zoo.cfg
|-- zoo2
|   `-- zoo.cfg
`-- zoo3
    `-- zoo.cfg

由于是伪集群端口需要修改

这里的1是myid要和myid文件中的对应
localhost因为是伪集群就都在一台机器
2888是默认的集群通信交换数据的端口
3888是默认的选举端口
server.1=localhost:2888:3888

配置文件内容

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=/data
dataLogDir=/data
# the port at which the clients will connect
clientPort=2181 //控制台默认端口2181,所以每个配置文件要不一样否则会占用
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
admin.serverPort=9991 //控制台默认端口8080,所以每个配置文件要不一样否则会占用
server.1=127.0.0.1:2881:8881
server.2=127.0.0.1:2882:8882
server.3=127.0.0.1:2883:8883

第二步

然后myid同理,需要三个文件夹,为了清晰我都是分开放置的,默认是在/tmp/zookeeper下,这里我创建了三个文件夹,在每个文件夹创建一个myid文件分别写入1,2,3

第三步

拉取zookeeper镜像,分别挂载myid和zoo.cfg,默认会去找/conf/zoo.cfg,网络使用的host和主机对应,运行docker命令,启动集群

docker run -itd --network host -v /tmp/zookeeper1:/data -v /home/zookeeper/zoo1:/conf --name zoo1 zookeeper

docker run -itd --network host -v /tmp/zookeeper2:/data -v /home/zookeeper/zoo2:/conf --name zoo2 zookeeper

docker run -itd --network host -v /tmp/zookeeper3:/data -v /home/zookeeper/zoo3:/conf --name zoo3 zookeeper

进入容器进入bin目录运行命令查看集群是否成功部署,然后stop掉leader,另外两台会自动有一台升级为leader,当再一次启动,因为已经有master,所以不会发起投票,自动成为follower
在这里插入图片描述
在这里插入图片描述

第二种方式

使用docker-compose进行构建,只需要写好docker-compose.yml文件即可,由于这里没有使用host模式所以端口不会互相冲突

version: '3.1'
services:
                 zk1:
                         image: zookeeper
                         restart: always
                         container_name: zk1
                         ports:
                                 - 2181:2181
                         volumes:
                                 - /usr/local/docker/zookeeper/zk1/data:/data
                                 - /usr/local/docker/zookeeper/zk1/datalog:/datalog
                         environment:
                                 ZOO_MY_ID: 1
                                 ZOO_SERVERS: server.1=zk1:2888:3888;2181 server.2=zk2:2888:3888;2181 server.3=zk3:2888:3888;2181
                 zk2:
                         image: zookeeper
                         restart: always
                         container_name: zk2
                         ports:
                                 - 2182:2181
                         volumes:
                                 - /usr/local/docker/zookeeper/zk2/data:/data
                                 - /usr/local/docker/zookeeper/zk2/datalog:/datalog
                         environment:
                                 ZOO_MY_ID: 2
                                 ZOO_SERVERS: server.1=zk1:2888:3888;2181 server.2=zk2:2888:3888;2181 server.3=zk3:2888:3888;2181
                 zk3:
                         image: zookeeper
                         restart: always
                         container_name: zk3
                         ports:
                                 - 2183:2181
                         volumes:
                                 - /usr/local/docker/zookeeper/zk3/data:/dada
                                 - /usr/local/docker/zookeeper/zk3/datalog:/datalog

                         environment:
                                 ZOO_MY_ID: 3
                                 ZOO_SERVERS: server.1=zk1:2888:3888;2181 server.2=zk2:2888:3888;2181 server.3=zk3:2888:3888;2181

在配置文件目录下使用docker-compose up启动即可

第三种方式

参考了第二种方式,我们可以基于容器的隔离性做一些事情,虽然是伪集群缺又看起来像真的集群,我们利用docker自带的bridge网络进行通信,首先ifconfig查看docker0的ip,然后分给三个容器 (同样也可以使用自定义网络只不过启动要指定–network 还有要注意创建的网络的网段是多少,指定ip写配置更容易,默认网络无法指定ip)

和第一种的目录位置一样只是修改了一下配置文件
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/data
dataLogDir=/data
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
#admin.serverPort=9991
server.1=172.17.0.2:2881:8881
server.2=172.17.0.3:2881:8881
server.3=172.17.0.4:2881:8881
~                                 

创建容器的时候依次启动1 2 3,ip就会从172.17.0.2开始分配
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值