我找到了解决这个问题的有效方法。 ZooKeeper在启动时读取集合中的服务器列表,并查找其“自己的”条目。然后,它使用此条目来确定要侦听的端口和接口。
server.1=zookeeper-0.zookeeper-headless:2888:3888 server.2=zookeeper-1.zookeeper-headless:2888:3888 server.3=zookeeper-2.zookeeper-headless:2888:3888
由于提供的主机名将在此计算机上解析为127.0.0.1,因此ZooKeeper将侦听本地环回接口,因此不接受来自其他ZooKeeper服务器的连接。
server.1=0.0.0.0:2888:3888 server.2=zookeeper-1.zookeeper-headless:2888:3888 server.3=zookeeper-2.zookeeper-headless:2888:3888
为了自动化集群中的东西,我写了一个bash脚本,它将取代容器启动时的一个“自己的”条目。
的 编辑: 强> 正如评论中所说,这是我的 ENTRYPOINT 负责放置myid文件并为每个文件设置适当主机名的脚本 zoo.cfg :
ENTRYPOINT
zoo.cfg
#!/bin/bash # This script extracts the number out of the pod's hostname and sets it as zookeepers id. # Exact paths may vary according to your setup MYID_FILE="/var/lib/zookeeper/data/myid" ZOOCFG_FILE="/conf/zoo.cfg" # Create myid-file # Extract only numbers from hostname id=$(hostname | tr -d -c 0-9) echo $id > "${MYID_FILE}" # change own hostname to 0.0.0.0 # otherwise, the own hostname will resolve to 127.0.0.1 # https://stackoverflow.com/a/40750900/5764665 fullHostname="$(hostname).zookeeper-headless" sed -i -e "s/${fullHostname}/0.0.0.0/g" "${ZOOCFG_FILE}" echo "Executing $@" exec "$@"