1. 安装zk
1.1 解压文件到安装目录
tar -xvf apache-zookeeper-3.6.3-bin.tar.gz -C /opt/zhoupu/
1.2 建立软连接
ln -s /opt/zhoupu/apache-zookeeper-3.6.3-bin /opt/zhoupu/zookeeper
1.3 创建数据文件目录
mkdir -p /srv/data/zookeeper
1.3 编辑zoo.cfg文件
cd /opt/zhoupu/zookeeper/etc/;cp zoo_sample.cfg zoo.cfg
vim zoo.cfg
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/srv/data/zookeeper
# the port at which the clients will connect
clientPort=2181
# servers configure 主机名称用具体IP(否则zkServer.sh查询状态会出问题)
server.0=hadoop1:2888:3888
server.1=hadoop2:2888:3888
server.2=hadoop3:2888:3888
1.4 创建myid文件
mkdir -p /srv/data/zookeeper
vim /srv/data/zookeeper/myid
myid里添加zookeeper编号:
mkdir -p /srv/data/zookeeper ;
touch /srv/data/zookeeper/myid ;
echo '0' > /srv/data/zookeeper/myid ;
1.5 配置环境变量
vim ~/.bash_profile
#set zookeeper environment
export ZK_HOME=/usr/local/software/zookeeper-3.3.6
export PATH=$PATH:$ZK_HOME/bin
export PATH
source ~/.bash_profile
1.6 zk常用命令
启动命令:
zkServer.sh start
停止命令:
zkServer.sh stop
重启命令:
zkServer.sh restart
查看集群节点状态:
zkServer.sh status
1.7 zk启停脚本
1.7.1 main
#!/bin/sh
function pre_check()
{
which nc >/dev/null
if [ $? -gt 0 ];then
echo "nc command can not found,please install it."
fi
}
function show_zk_status()
{
ip1=`echo ${serverip} | cut -d ',' -f 1`
ip2=`echo ${serverip} | cut -d ',' -f 2`
ip3=`echo ${serverip} | cut -d ',' -f 3`
status1=`echo stat | nc ${ip1} 2181 | grep "Mode" | cut -d ' ' -f 2 `
status2=`echo stat | nc ${ip2} 2181 | grep "Mode" | cut -d ' ' -f 2 `
status3=`echo stat | nc ${ip3} 2181 | grep "Mode" | cut -d ' ' -f 2 `
echo -e "${ip1}:${status1}"
echo -e "${ip2}:${status2}"
echo -e "${ip3}:${status3}"
}
function start_zk()
{
for ip in `echo ${serverip} | sed 's/,/ /g'`
do
if ssh -l hadoop ${ip} -o "StrictHostKeyChecking no" test -e ~/manager-node.sh;then
ssh -l hadoop ${ip} -o "StrictHostKeyChecking no" "sh ~/manager-node.sh start"
else
echo "manager-node.sh did not exists on ${ip}\'s home direction"
exit 1
fi
done
}
function stop_zk()
{
for ip in `echo ${serverip} | sed 's/,/ /g'`
do
if ssh -l hadoop ${ip} -o "StrictHostKeyChecking no" test -e ~/manager-node.sh;then
ssh -l hadoop ${ip} -o "StrictHostKeyChecking no" "sh ~/manager-node.sh stop"
else
echo "manager-node.sh did not exists on ${ip}\'s home direction"
exit 1
fi
done
}
function main()
{
if [ $# -lt 1 ];then
echo "参数错误"
echo -e "Usage: sh $0 [-options <start,stop,status>]"
exit 1
fi
pre_check;
case $1 in
"start")
start_zk
;;
"stop")
stop_zk
;;
"status")
show_zk_status
;;
?)
echo "未知参数"
exit 1
;;
esac
}
#sh ~/manager-node.sh
serverip=192.168.2.11,192.168.2.12,192.168.2.13
main $@
1.7.2 manager-node.sh
#!/bin/sh
source ~/.bash_profile
which java >/dev/null
if [ $? -gt 0 ];then
source /etc/profile
if [ $? -gt 0 ];then
echo "DO NOT SET JAVA ENV, SET JAVA ENV FIRST"
fi
fi
sh /opt/zhoupu/zookeeper/bin/zkServer.sh $1
result=$?
return ${result}
1.8 四字命令
1. echo stat | nc serverip 2181 ,来查看哪个节点被选择作为follower或者leader
2. echo ruok | nc serverip 2181 ,测试是否启动了该Server,若回复imok表示已经启动。
3. echo dump | nc serverip 2181 ,列出未经处理的会话和临时节点。
4. echo kill | nc serverip 2181 ,关掉server
5. echo conf | nc serverip 2181 ,输出相关服务配置的详细信息。
6. echo cons | nc serverip 2181 ,列出所有连接到服务器的客户端的完全的连接 / 会话的详细信息。
7. echo envi | nc serverip 2181 ,输出关于服务环境的详细信息(区别于 conf 命令)。
8. echo reqs | nc serverip 2181 ,列出未经处理的请求。
9. echo wchs | nc serverip 2181 ,列出服务器 watch 的详细信息。
10. echo wchc | nc serverip 2181 ,通过 session 列出服务器 watch 的详细信息,它的输出是一个与 watch 相关的会话的列表。
11. echo wchp | nc serverip 2181 ,通过路径列出服务器 watch 的详细信息。它输出一个与 session 相关的路径。
nc安装 yum install nc