#创建集群目录 mkdir /opt/cluster/zk cd /opt/cluster/zk #清理脏数据【可跳过】 docker stop zk-2181 docker stop zk-2182 docker stop zk-2183 docker rm zk-2181 docker rm zk-2182 docker rm zk-2183 docker ps -a #创建配置文件模板【vim zoo-cluster.tmpl】 cat >zoo-cluster.tmpl <<HERE clientPort=${PORT} dataDir=/data dataLogDir=/datalog tickTime=2000 initLimit=5 syncLimit=2 maxClientCnxns=60 server.1=localhost:2881:3881 server.2=localhost:2882:3882 server.3=localhost:2883:3883 HERE #生成conf和data目标,并生成配置信息 for port in `seq 2181 2183`; do mkdir -p ./${port}/conf && PORT=${port} envsubst < ./zoo-cluster.tmpl > ./${port}/conf/zoo.cfg && mkdir -p ./${port}/data && echo $[$port-2180] >./${port}/data/myid && mkdir -p ./${port}/datalog; done #创建3个zookeeper容器 for port in `seq 2181 2183`; do docker run -d --restart always -h zk${port} --name zk-${port} --net host -v /opt/cluster/zk/${port}/conf:/conf -v /opt/cluster/zk/${port}/data:/data -v /opt/cluster/zk/${port}/datalog:/datalog -p ${port}:${port} -p $[$port+700]:$[$port+700] -p $[$port+1700]:$[$port+1700] zookeeper ; done # 创建并运行临时zookeeper容器,zkCli客户端连接集群 docker run -it --rm zookeeper zkCli.sh -server 123.*.*.34:2181,123.*.*.34:2182,123.*.*.34:2183