集群安装
首先集群的机器都安装了 JDK (参考数据存储的Hadoop安装前期准备进行配置)
安装集群
- 解压安装包
tar -zxvf apache-zookeeper-3.5.7-bin.tar.gz -C ../module/
- 修改配置文件
cp /home/bigdata/module/apache-zookeeper-3.5.7-bin/conf/zoo_sample.cfg /home/bigdata/module/apache-zookeeper-3.5.7-bin/conf/zoo.cfg
vi /home/bigdata/module/apache-zookeeper-3.5.7-bin/conf/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=/home/bigdata/module/apache-zookeeper-3.5.7-bin/data
dataLogDir=/home/bigdata/module/apache-zookeeper-3.5.7-bin/log
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
server.1=master1:2888:3888
server.2=master2:2888:3888
server.3=node1:2888:3888
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
配置文件对应的配置说明:
- tickTime: tick的中文意思是"嘀的一声",tickTime指的是滴答一声的时间长度。在ZooKeeper中,它是所有涉及到时间长度的单元,单位为毫秒,就相当于时钟里的秒单元一样。例如,
tickTime=2000;initLimit=5
,表示initLimit的时间为"嘀嗒"5次,长度为2000*5=10秒。tickTime隐含了心跳时间(即心跳时间为tickTime),还隐含了客户端和服务器之间保持的会话的最小和最大超时时间(最小2倍tickTime,最大20倍tickTime)。 - initLimit: 初始化时间,集群中的服务器连接到Leader的初始化时间。
- syncLimit: Leader与Follower之间请求和应答时间。
- dataDir: 数据存放地址。
- dataLogDir: 日志存放地址。
- clientPort: 客户端连接端口。
- server.index=IP:A:B : X代表自定义的各服务器序号(对应写在各自机器的myid中),IP即各服务器IP,A表示集群服务器之间通讯组件的端口,B表示选举组件的端口。
创建对应的数据文件和日志文件
mkdir /home/bigdata/module/apache-zookeeper-3.5.7-bin/data
mkdir /home/bigdata/module/apache-zookeeper-3.5.7-bin/log
在集群的机器上创建自己的myid(在dataDir=/home/bigdata/module/apache-zookeeper-3.5.7-bin/data
下创建一个myid里面是配置文件里面对应的server.1=master1:2888:3888
),那么对应机器下的myid就是1,如果server.2
,那么对应data目录下的myid就是2,以此类推。
<!-- 其他机器分别执行 -->
echo "1" > /home/bigdata/module/apache-zookeeper-3.5.7-bin/data/myid
echo "2" > /home/bigdata/module/apache-zookeeper-3.5.7-bin/data/myid
echo "3" > /home/bigdata/module/apache-zookeeper-3.5.7-bin/data/myid
配置完以后同步安装包到其他的集群。
/home/bigdata/shell/xsync.sh /home/bigdata/module/apache-zookeeper-3.5.7-bin
启动集群的shell脚本
vi /home/bigdata/shell/zk_cluster_shell.sh
#!/bin/bash
case $1 in
"start"){
for i in master1 master2 node1
do
echo " --------启动 $i zookeeper-------"
ssh $i "/home/bigdata/module/apache-zookeeper-3.5.7-bin/bin/zkServer.sh start "
done
};;
"status"){
for i in master1 master2 node1
do
echo " --------启动 $i zookeeper-------"
ssh $i "/home/bigdata/module/apache-zookeeper-3.5.7-bin/bin/zkServer.sh status "
done
};;
"stop"){
for i in master1 master2 node1
do
echo " --------停止 $i zookeeper-------"
ssh $i "/home/bigdata/module/apache-zookeeper-3.5.7-bin/bin/zkServer.sh stop"
done
};;
esac
chmod 777 /home/bigdata/shell/zk_cluster_shell.sh
启动集群
/home/bigdata/shell/zk_cluster_shell.sh start
/home/bigdata/shell/zk_cluster_shell.sh stop
/home/bigdata/shell/zk_cluster_shell.sh status