Loading... # docker-compose搭建zookeeper伪集群(1主2从) --- ## 1.安装docker-compose <div class="tip inlineBlock success"> 安装手册可见docker-compose官网 [docker-compose官网](https://docs.docker.com/compose/install/ "参考资料") </div> 运行此命令以下载 Docker Compose 的当前稳定版本: ```shell sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose ``` 对二进制文件应用可执行权限: ```shell sudo chmod +x /usr/local/bin/docker-compose ``` ## 2.准备docker-compose.yml文件 #### 2.1创建目录存放zookeeper的data和datalog ```shell mkdir zookeeper ``` #### 2.2创建docker-compose.yml文件 ```shell vim docker-compose.yml ``` #### 2.3编写docker-compose.yml ```yaml version: '3.1' services: zoo1: image: zookeeper:3.7 hostname: zoo1 container_name: zoo1 ports: - 2004:2181 volumes: - $PWD/zk1/data:/data - $PWD/zk1/datalog:/datalog environment: ZOO_MY_ID: 1 ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181 zoo2: image: zookeeper:3.7 hostname: zoo2 container_name: zoo2 ports: - 2005:2181 volumes: - $PWD/zk2/data:/data - $PWD/zk2/datalog:/datalog environment: ZOO_MY_ID: 2 ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181 zoo3: image: zookeeper:3.7 hostname: zoo3 container_name: zoo3 ports: - 2006:2181 volumes: - $PWD/zk3/data:/data - $PWD/zk3/datalog:/datalog environment: ZOO_MY_ID: 3 ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181 ``` ## 3.启动zookeeper集群 #### 3.1启动命令 ```shell $ docker-compose -f zookeeper-compose.yml up -d ``` #### 3.2查看zookeeper状况 ```shell //进入zoo1容器 docker exec -it zoo1 /bin/bash //进入bin目录 cd bin //查看zookeeper启动情况 ./zkServer.sh status ``` zoo1 ```shell root@zoo1:/apache-zookeeper-3.7.0-bin/bin# zkServer.sh status ZooKeeper JMX enabled by default Using config: /conf/zoo.cfg Client port found: 2181. Client address: localhost. Client SSL: false. Mode: follower ``` zoo2 ```shell root@zoo2:/apache-zookeeper-3.7.0-bin# cd bin root@zoo2:/apache-zookeeper-3.7.0-bin/bin# zkServer.sh status ZooKeeper JMX enabled by default Using config: /conf/zoo.cfg Client port found: 2181. Client address: localhost. Client SSL: false. Mode: follower ``` zoo3 ```shell root@zoo3:/apache-zookeeper-3.7.0-bin# cd bin root@zoo3:/apache-zookeeper-3.7.0-bin/bin# zkServer.sh status ZooKeeper JMX enabled by default Using config: /conf/zoo.cfg Client port found: 2181. Client address: localhost. Client SSL: false. Mode: leader ``` 由此可见三个服务都启动起来了,zoo3为主节点,zoo1和zoo2为从节点 最后修改:2021 年 11 月 15 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 0 如果觉得我的文章对你有用,请随意赞赏