实现功能:一键启动、关闭主从端3个节点上的zookeeper,附加查看启动状态
mkdir bin --新建文件夹
cd bin 跳转到bin文件夹里
touch zookeeperstart.sh --新建一键启动文件
touch zookeeperstop.sh --新建一键停止文件
zookeeperstart.sh添加内容:
#!/bin/bash
ZOOKEEPER_HOME=/home/hadoop/apache-zookeeper-3.5.5
if [ $ZOOKEEPER_HOME != "" ]; then
ZOOCFG=$ZOOKEEPER_HOME/conf/zoo.cfg
for s in 01 02 03 --循环启动
do
echo "INFO:starting zookeeper on ${s}"
ssh hadoop$s "source /etc/profile;$ZOOKEEPER_HOME/bin/zkServer.sh start" --利用ssh登陆对应的节点,执行当前对应启动命令
if [ $? != 0 ];
then
echo "Can not starting zookeeper server on host $s}"
exit 1
fi
done
for s in 01 02 03 --循环查看zookeeper状态
do
echo "INFO:starting zookeeper on ${s}"
ssh hadoop$s "source /etc/profile;$ZOOKEEPER_HOME/bin/zkServer.sh status" --显示各节点zookeeper的启动状态
if [ $? != 0 ];
then
echo "Can not starting zookeeper server on host $s}"
exit 1
fi
done
fi
zookeeperstopsh添加内容:
#!/bin/bash
ZOOKEEPER_HOME=/home/hadoop/apache-zookeeper-3.5.5
if [ $ZOOKEEPER_HOME != "" ]; then
ZOOCFG=$ZOOKEEPER_HOME/conf/zoo.cfg
for s in 01 02 03
do
echo "INFO:starting zookeeper on ${s}"
ssh hadoop$s "source /etc/profile;$ZOOKEEPER_HOME/bin/zkServer.sh stop"
if [ $? != 0 ];
then
echo "Can not starting zookeeper server on host $s}"
exit 1
fi
done
fi
启动脚本:
sh zookeeperstart.sh
sh zookeeperstop.sh
例:以我的linux虚拟机上配置zookeeper为例
[hadoop@hadoop01 bin]$ sh zookeeperstop.sh
INFO:starting zookeeper on 01
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /home/hadoop/apache-zookeeper-3.5.5/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
INFO:starting zookeeper on 02
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /home/hadoop/apache-zookeeper-3.5.5/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
INFO:starting zookeeper on 03
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /home/hadoop/apache-zookeeper-3.5.5/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
[hadoop@hadoop01 bin]$ sh zookeeperstart.sh
INFO:starting zookeeper on 01
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /home/hadoop/apache-zookeeper-3.5.5/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
INFO:starting zookeeper on 02
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /home/hadoop/apache-zookeeper-3.5.5/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
INFO:starting zookeeper on 03
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /home/hadoop/apache-zookeeper-3.5.5/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
INFO:starting zookeeper on 01
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /home/hadoop/apache-zookeeper-3.5.5/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
INFO:starting zookeeper on 02
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /home/hadoop/apache-zookeeper-3.5.5/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: leader
INFO:starting zookeeper on 03
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /home/hadoop/apache-zookeeper-3.5.5/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
最后,我们可以看见,系统给出的信息表明,有两个follower,一个leader,这是zookeeper的工作原理