其他分享
首页 > 其他分享> > Docker(十一): 安装zookeeper

Docker(十一): 安装zookeeper

作者:互联网

docker-compose安装zookeeper

编写docker-compose.yml文件

version: '3.1'

services:
  zoo1:
    container_name: zoo1
    image: zookeeper
    hostname: zoo1
    ports:
      - 2181:2181
    networks:
      - zookeeper-network
    volumes:
        - /mydata/zookeeper/zoo1/data:/data
        - /mydata/zookeeper/zoo1/conf/zoo.cfg:/conf/zoo.cfg
        - /mydata/zookeeper/zoo1/logs:/datalog
    restart: always
    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:
    container_name: zoo2
    image: zookeeper
    hostname: zoo2
    ports:
      - 2182:2181
    networks:
      - zookeeper-network
    volumes:
      - /mydata/zookeeper/zoo2/data:/data
      - /mydata/zookeeper/zoo2/conf/zoo.cfg:/conf/zoo.cfg
      - /mydata/zookeeper/zoo2/logs:/datalog
    restart: always
    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:
    container_name: zoo3
    image: zookeeper
    hostname: zoo3
    ports:
      - 2183:2181
    networks:
      - zookeeper-network
    volumes:
      - /mydata/zookeeper/zoo3/data:/data
      - /mydata/zookeeper/zoo3/conf/zoo.cfg:/conf/zoo.cfg
      - /mydata/zookeeper/zoo3/logs:/datalog
    restart: always
    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

networks:
  zookeeper-network:
    driver: bridge

注意: 此时zookeeper集群是启动不起来的,查看日志/docker-entrypoint.sh: line 43: /conf/zoo.cfg: Is a directory

解决容器启动失败

在挂载文件夹/mydata/zookeeper/zoo{}/conf创建zoo.cfg文件, 赋权755

cd /mydata/zookeeper/zoo1/conf
rm -rf zoo.cfg/
touch zoo.cfg
chmod 755 zoo.cfg 

将另外两个节点按照此命令修改。

此时启动依然启动不起来, 异常为:

Attaching to zoo3, zoo2, zoo1
zoo1    | ZooKeeper JMX enabled by default
zoo1    | Using config: /conf/zoo.cfg
zoo1    | mkdir: cannot create directory ‘’: No such file or directory
zoo1    | 2022-07-03 08:43:05,403 [myid:] - INFO  [main:QuorumPeerConfig@174] - Reading configuration from: /conf/zoo.cfg
zoo1    | Invalid config, exiting abnormally
zoo1    | 2022-07-03 08:43:05,408 [myid:] - ERROR [main:QuorumPeerMain@99] - Invalid config, exiting abnormally
zoo1    | org.apache.zookeeper.server.quorum.QuorumPeerConfig$ConfigException: Error processing /conf/zoo.cfg
zoo1    |       at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parse(QuorumPeerConfig.java:198)
zoo1    |       at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(QuorumPeerMain.java:125)
zoo1    |       at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:91)
zoo1    | Caused by: java.lang.IllegalArgumentException: dataDir is not set
zoo1    |       at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parseProperties(QuorumPeerConfig.java:428)
zoo1    |       at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parse(QuorumPeerConfig.java:194)
zoo1    |       ... 2 more
zoo1    | 2022-07-03 08:43:05,411 [myid:] - INFO  [main:ZKAuditProvider@42] - ZooKeeper audit is disabled.
zoo1    | 2022-07-03 08:43:05,413 [myid:] - ERROR [main:ServiceUtils@42] - Exiting JVM with code 2
zoo1    | ZooKeeper JMX enabled by default
zoo1    | Using config: /conf/zoo.cfg
zoo1    | mkdir: cannot create directory ‘’: No such file or directory
zoo1    | 2022-07-03 08:43:06,614 [myid:] - INFO  [main:QuorumPeerConfig@174] - Reading configuration from: /conf/zoo.cfg
zoo1    | Invalid config, exiting abnormally
zoo1    | 2022-07-03 08:43:06,617 [myid:] - ERROR [main:QuorumPeerMain@99] - Invalid config, exiting abnormally
zoo1    | org.apache.zookeeper.server.quorum.QuorumPeerConfig$ConfigException: Error processing /conf/zoo.cfg
zoo1    |       at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parse(QuorumPeerConfig.java:198)
zoo1    |       at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(QuorumPeerMain.java:125)
zoo1    |       at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:91)
zoo1    | Caused by: java.lang.IllegalArgumentException: dataDir is not set
zoo1    |       at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parseProperties(QuorumPeerConfig.java:428)
zoo1    |       at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parse(QuorumPeerConfig.java:194)
zoo1    |       ... 2 more
zoo1    | 2022-07-03 08:43:06,618 [myid:] - INFO  [main:ZKAuditProvider@42] - ZooKeeper audit is disabled.
zoo1    | 2022-07-03 08:43:06,620 [myid:] - ERROR [main:ServiceUtils@42] - Exiting JVM with code 2
zoo1    | ZooKeeper JMX enabled by default
zoo1    | Using config: /conf/zoo.cfg
zoo1    | mkdir: cannot create directory ‘’: No such file or directory
zoo1    | 2022-07-03 08:43:07,763 [myid:] - INFO  [main:QuorumPeerConfig@174] - Reading configuration from: /conf/zoo.cfg
zoo1    | 2022-07-03 08:43:07,766 [myid:] - ERROR [main:QuorumPeerMain@99] - Invalid config, exiting abnormally
zoo1    | org.apache.zookeeper.server.quorum.QuorumPeerConfig$ConfigException: Error processing /conf/zoo.cfg
zoo1    |       at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parse(QuorumPeerConfig.java:198)
zoo1    |       at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(QuorumPeerMain.java:125)
zoo1    |       at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:91)
zoo1    | Caused by: java.lang.IllegalArgumentException: dataDir is not set
zoo1    |       at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parseProperties(QuorumPeerConfig.java:428)
zoo1    |       at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parse(QuorumPeerConfig.java:194)
zoo1    |       ... 2 more
zoo1    | 2022-07-03 08:43:07,770 [myid:] - INFO  [main:ZKAuditProvider@42] - ZooKeeper audit is disabled.
zoo1    | Invalid config, exiting abnormally
zoo1    | 2022-07-03 08:43:07,771 [myid:] - ERROR [main:ServiceUtils@42] - Exiting JVM with code 2

分析原因为找不到myid文件。

将zoo.cfg文件修改, dataDir=/data myid文件存放在/data目录下

# 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
# 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.
#
# https://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

## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpHost=0.0.0.0
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true

启动zookeeper集群

docker-compose -f docker-compose.yml up -d

在使用zookeeper3.6之后版本,zookeeper启动时会启用jetty容器,占用8080端口, 可在配置文件中admin.serverPort=8888进行修改

标签:十一,cfg,zookeeper,zoo,zoo1,QuorumPeerConfig,org,Docker
来源: https://www.cnblogs.com/chinda/p/16440143.html