- 首先下载sshpass为了免密
yum install sshpass
-
随后来到/home/用户名/bin中,没有就新建。
-
新建文件zk.sh,粘贴下列脚本
#!/bin/bash
case $1 in
"start"){
for i in hadoop128 hadoop129 hadoop130
do
echo --------------------------zookeeper $i 启动 -------------------
sshpass -p root ssh -o StrictHostKeyChecking=no $i "/opt/module/zookeeper-3.5.7/bin/zkServer.sh start"
done
}
;;
"stop"){
for i in hadoop128 hadoop129 hadoop130
do
echo --------------------------zookeeper $i 关闭 -------------------
sshpass -p root ssh -o StrictHostKeyChecking=no $i "/opt/module/zookeeper-3.5.7/bin/zkServer.sh stop"
done
}
;;
"status"){
for i in hadoop128 hadoop129 hadoop130
do
echo --------------------------zookeeper $i 状态 -------------------
sshpass -p root ssh -o StrictHostKeyChecking=no $i "/opt/module/zookeeper-3.5.7/bin/zkServer.sh status"
done
}
;;
esac
其中StrictHostKeyChecking=no使得密码提示去掉了,-p root(这是我的密码,修改为自己的密码)
保存退出
授权:
chmod 777 zk.sh
启动脚本运行zookeeper集群
./zk.sh start
出现下列内容
[root@localhost bin]# ./zk.sh start
--------------------------zookeeper hadoop128 启动 -------------------
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.5.7/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
--------------------------zookeeper hadoop129 启动 -------------------
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.5.7/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
--------------------------zookeeper hadoop130 启动 -------------------
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.5.7/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
查询状态:
[root@localhost bin]# ./zk.sh status
--------------------------zookeeper hadoop128 状态 -------------------
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.5.7/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
--------------------------zookeeper hadoop129 状态 -------------------
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.5.7/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: leader
--------------------------zookeeper hadoop130 状态 -------------------
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.5.7/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
关闭zookeeper:
[root@localhost bin]# ./zk.sh stop
--------------------------zookeeper hadoop128 关闭 -------------------
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.5.7/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
--------------------------zookeeper hadoop129 关闭 -------------------
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.5.7/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
--------------------------zookeeper hadoop130 关闭 -------------------
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.5.7/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED