下载对应版本Zookeeper,这里我下载的版本3.4.14
。官方下载地址:https://archive.apache.org/dist/zookeeper/html
# wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
# tar -zxvf zookeeper-3.4.14.tar.gz
# vim /etc/profile
添加环境变量:git
export ZOOKEEPER_HOME=/usr/app/zookeeper-3.4.14 export PATH=$ZOOKEEPER_HOME/bin:$PATH
使得配置的环境变量生效:程序员
# source /etc/profile
进入安装目录的conf/
目录下,拷贝配置样本并进行修改:github
# cp zoo_sample.cfg zoo.cfg
指定数据存储目录和日志文件目录(目录不用预先建立,程序会自动建立),修改后完整配置以下:web
# 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=/usr/local/zookeeper/data dataLogDir=/usr/local/zookeeper/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 # # 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
配置参数说明:shell
- tickTime:用于计算的基础时间单元。好比session超时:N*tickTime;
- initLimit:用于集群,容许从节点链接并同步到 master节点的初始化链接时间,以tickTime的倍数来表示;
- syncLimit:用于集群, master主节点与从节点之间发送消息,请求和应答时间长度(心跳机制);
- dataDir:数据存储位置;
- dataLogDir:日志目录;
- clientPort:用于客户端链接的端口,默认2181
因为已经配置过环境变量,直接使用下面命令启动便可:apache
zkServer.sh start
使用JPS验证进程是否已经启动,出现QuorumPeerMain
则表明启动成功。vim
[root@hadoop001 bin]# jps 3814 QuorumPeerMain
为保证集群高可用,Zookeeper集群的节点数最好是奇数,最少有三个节点,因此这里演示搭建一个三个节点的集群。这里我使用三台主机进行搭建,主机名分别为hadoop001,hadoop002,hadoop003。服务器
解压一份zookeeper安装包,修改其配置文件zoo.cfg
,内容以下。以后使用scp命令将安装包分发到三台服务器上:session
tickTime=2000 initLimit=10 syncLimit=5 dataDir=/usr/local/zookeeper-cluster/data/ dataLogDir=/usr/local/zookeeper-cluster/log/ clientPort=2181 # server.1 这个1是服务器的标识,能够是任意有效数字,标识这是第几个服务器节点,这个标识要写到dataDir目录下面myid文件里 # 指名集群间通信端口和选举端口 server.1=hadoop001:2287:3387 server.2=hadoop002:2287:3387 server.3=hadoop003:2287:3387
分别在三台主机的dataDir
目录下新建myid
文件,并写入对应的节点标识。Zookeeper集群经过myid
文件识别集群节点,并经过上文配置的节点通讯端口和选举端口来进行节点通讯,选举出Leader节点。
建立存储目录:
# 三台主机均执行该命令 mkdir -vp /usr/local/zookeeper-cluster/data/
建立并写入节点标识到myid
文件:
# hadoop001主机 echo "1" > /usr/local/zookeeper-cluster/data/myid # hadoop002主机 echo "2" > /usr/local/zookeeper-cluster/data/myid # hadoop003主机 echo "3" > /usr/local/zookeeper-cluster/data/myid
分别在三台主机上,执行以下命令启动服务:
/usr/app/zookeeper-cluster/zookeeper/bin/zkServer.sh start
启动后使用zkServer.sh status
查看集群各个节点状态。如图所示:三个节点进程均启动成功,而且hadoop002为leader节点,hadoop001和hadoop003为follower节点。
更多大数据系列文章能够参见我的 GitHub 开源项目: 程序员大数据入门指南