要启动并运行一个ZooKeeper Docker集群,您需要遵循以下步骤:
准备环境: 确保您已经安装了Docker和Docker Compose。如果没有,请访问Docker官网(https://www.docker.com/)下载并安装适合您操作系统的Docker版本。Docker Compose是一个用于定义和运行多容器Docker应用程序的工具,您可以从Docker官网(https://docs.docker.com/compose/install/)下载并安装它。
创建Docker Compose文件:
在您的项目目录中创建一个名为docker-compose.yml
的文件,然后添加以下内容:
version: '3'
services:
zookeeper1:
image: zookeeper:3.7.0
container_name: zookeeper1
ports:
- "2181:2181"
environment:
ZOO_MY_ID: 1
ZOO_SERVERS: server.1=zookeeper1:2888:3888
zookeeper2:
image: zookeeper:3.7.0
container_name: zookeeper2
ports:
- "2182:2181"
environment:
ZOO_MY_ID: 2
ZOO_SERVERS: server.1=zookeeper1:2888:3888,server.2=zookeeper2:2888:3888
zookeeper3:
image: zookeeper:3.7.0
container_name: zookeeper3
ports:
- "2183:2181"
environment:
ZOO_MY_ID: 3
ZOO_SERVERS: server.1=zookeeper1:2888:3888,server.2=zookeeper2:2888:3888,server.3=zookeeper3:2888:3888
这个文件定义了一个包含三个ZooKeeper容器的Docker Compose应用程序。每个容器都使用zookeeper:3.7.0
镜像,并映射到主机的不同端口。环境变量ZOO_MY_ID
用于设置每个节点的ID,而ZOO_SERVERS
用于配置服务器之间的连接。
docker-compose.yml
文件的目录中,打开终端或命令提示符,运行以下命令:docker-compose up -d
这将启动并运行ZooKeeper集群。-d
标志表示以分离模式运行容器。
docker logs zookeeper1
您应该看到类似以下的输出,表明ZooKeeper已正确启动并运行:
2021-XX-XX 12:34:56,789 [myid:1] - INFO zookeeper.server.quorum.QuorumPeerMain - Starting quorum peer
2021-XX-XX 12:34:56,790 [myid:1] - INFO zookeeper.server.quorum.QuorumPeerConfig - Reading configuration from file:/conf/zoo.cfg
2021-XX-XX 12:34:56,791 [myid:1] - WARN zookeeper.server.quorum.QuorumPeerMain - Either no config file was specified, or the specified config file does not exist.
2021-XX-XX 12:34:56,791 [myid:1] - INFO zookeeper.server.quorum.QuorumPeerMain - Configuring server to run in standalone mode
2021-XX-XX 12:34:56,792 [myid:1] - INFO zookeeper.server.quorum.QuorumPeerMain - Starting server
现在您已经成功启动并运行了一个ZooKeeper Docker集群。您可以开始使用它来存储和管理分布式应用程序的元数据。