docker-compose安装ZooKeeper集群

docker-compose安装ZooKeeper集群

leo 1157 2021-04-10

docker-compose 配置文件

创建一个配置文件 docker-compose.yml ,内容如下:

version: '3.1'
services:
  zoo1:
    image: zookeeper
    restart: always
    hostname: zoo1
    ports:
      - 2181:2181
    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
    restart: always
    hostname: zoo2
    ports:
      - 2182:2181
    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
    restart: always
    hostname: zoo3
    ports:
      - 2183:2181
    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

启动

执行 docker-compose up -d 运行容器

查看状态

执行 docker ps 查看容器状态

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                                  NAMES
95f80f321850        zookeeper           "/docker-entrypoint.…"   3 minutes ago       Up 3 minutes        2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp, 8080/tcp   zookeeper_zoo1_1
baabad3ea4a5        zookeeper           "/docker-entrypoint.…"   3 minutes ago       Up 3 minutes        2888/tcp, 3888/tcp, 8080/tcp, 0.0.0.0:2182->2181/tcp   zookeeper_zoo2_1
dd11960d4623        zookeeper           "/docker-entrypoint.…"   3 minutes ago       Up 3 minutes        2888/tcp, 3888/tcp, 8080/tcp, 0.0.0.0:2183->2181/tcp   zookeeper_zoo3_1

交互式进入容器:

执行 docker exec -it zookeeper_zoo1_1 /bin/bash 命令,

继续执行 cd bin./zkServer.sh status

root@zoo1:/apache-zookeeper-3.6.2-bin# cd bin
root@zoo1:/apache-zookeeper-3.6.2-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

可以看到当前节点为follower,也就是从节点。同理可查看其它节点。

使用的配置文件是自动创建的/conf/zoo.cfg文件,可以用cat查看配置内容:

dataDir=/data
dataLogDir=/datalog
tickTime=2000
initLimit=5
syncLimit=2
autopurge.snapRetainCount=3
autopurge.purgeInterval=0
maxClientCnxns=60
standaloneEnabled=true
admin.enableServer=true
server.1=zoo1:2888:3888;2181
server.2=zoo2:2888:3888;2181
server.3=zoo3:2888:3888;2181