已经安装好 JDK8+. 如果使用JDK8, 版本需要在211以上.
使用root用户
wget https://downloads.apache.org/zookeeper/stable/apache-zookeeper-3.6.3-bin.tar.gz tar xvf apache-zookeeper-3.6.3-bin.tar.gz cd /opt/ ll mkdir zookeeper cd zookeeper/ mv ~/backup/apache-zookeeper-3.6.3-bin . # 命名加 -1 , 因为后面还有两个节点 mv apache-zookeeper-3.6.3-bin/ apache-zookeeper-3.6.3-bin-1
使用root用户
cd apache-zookeeper-3.6.3-bin-1/conf cp zoo_sample.cfg zoo.cfg vi zoo.cfg # 增加环境变量配置 vi zookeeper-env.sh chmod +x zookeeper-env.sh # 产生另外两个节点 cd /opt/zookeeper cp -r apache-zookeeper-3.6.3-bin-1/ apache-zookeeper-3.6.3-bin-2 cp -r apache-zookeeper-3.6.3-bin-1/ apache-zookeeper-3.6.3-bin-3 # 修改另外两个节点配置 vi apache-zookeeper-3.6.3-bin-2/conf/zoo.cfg vi apache-zookeeper-3.6.3-bin-3/conf/zoo.cfg vi apache-zookeeper-3.6.3-bin-2/conf/zookeeper-env.sh vi apache-zookeeper-3.6.3-bin-3/conf/zookeeper-env.sh
对于每个节点的zoo.cfg, 需要修改以下几处
dataDir=/home/tomcat/zookeeper/data1 #修改, 各节点分别为data1, data2, data3 clientPort=2181 #修改, 各节点分别为 2181, 2182, 2183 ... autopurge.purgeInterval=1 #取消注释 server.1=127.0.0.1:10090:10091 #新增 各节点都是这三行 server.2=127.0.0.1:10092:10093 #新增 server.3=127.0.0.1:10094:10095 #新增
对于每个节点的 zookeeper-env.sh , 需要增加这两行
JAVA_HOME=/opt/jdk/latest #因为没有设置全局JAVA_HOME,需要在这里配置 ZOO_LOG_DIR=/home/tomcat/zookeeper/log1 #各节点分别为 log1, log2, log3
关于zookeeper-env.sh
这个文件会在zkEnv.sh中调用, 在检查zoo.cfg文件是否存在之前就会执行, 此时可以设置任何你想设置的环境变量
if [ -f "${ZOOCFGDIR}/zookeeper-env.sh" ]; then . "${ZOOCFGDIR}/zookeeper-env.sh" fi
使用 tomcat 用户
cd mkdir zookeeper cd zookeeper/ # 创建工作目录 mkdir data1 mkdir data2 mkdir data3 # 接入节点ID echo "1" > data1/myid echo "2" > data2/myid echo "3" > data3/myid # 创建日志目录 mkdir log1 mkdir log2 mkdir log3 # 创建启动脚本 vi startup.sh chmod u+x startup.sh # 创建关闭脚本 vi shutdown.sh chmod u+x shutdown.sh
启动脚本 zookeeper/startup.sh 的内容
/opt/zookeeper/apache-zookeeper-3.6.3-bin-1/bin/zkServer.sh start /opt/zookeeper/apache-zookeeper-3.6.3-bin-2/bin/zkServer.sh start /opt/zookeeper/apache-zookeeper-3.6.3-bin-3/bin/zkServer.sh start
关闭脚本 zookeeper/shudown.sh 的内容
/opt/zookeeper/apache-zookeeper-3.6.3-bin-1/bin/zkServer.sh stop /opt/zookeeper/apache-zookeeper-3.6.3-bin-2/bin/zkServer.sh stop /opt/zookeeper/apache-zookeeper-3.6.3-bin-3/bin/zkServer.sh stop