一、安裝準備
下載地址:https://www.apache.org/dyn/closer.cgi/zookeeper/
官方文檔:https://zookeeper.apache.org/doc/r3.4.5/
二、解壓安裝
解壓文件
cd /usr/local/hadoop
tar zxpf zookeeper-3.4.5.tar.gz
創建軟連接
ln -s zookeeper-3.4.5 zookeeper
三、配置zoo.cfg
cd /usr/local/hadoop/zookeeper/conf
vim /usr/local/hadoop/zookeeper/conf/zoo.cfg
給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=/usr/local/hadoop/zookeeper/data
dataLogDir=/usr/local/hadoop/zookeeper/logs
# the port at which the clients will connect
clientPort=2181
#
# 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
server.1=hadoop001:2888:3888
server.2=hadoop002:2888:3888
server.3=hadoop003:2888:3888
配置文件說明
tickTime:心跳時間,默認2000毫秒即2秒
initLimit:初始化時follower和leader之間的最多心跳數
syncLimit:follower和leader之間請求響應的最多心跳數
dataDir:存放myid、版本信息和數據持久化的目錄
dataLogDir:zookeeper存放日誌的目錄
clientPort:zookeeper監聽的端口,默認2181
server.N=ip:2888:3888:server節點配置,N表示第N臺zk的節點
四、配置myid
zookeeper根據配置文件自動創建dataDir、dataLogDir目錄,需要自己手動創建
mkdir /usr/local/hadoop/zookeeper/data
mkdir /usr/local/hadoop/zookeeper/logs
給每臺zk節點配置myid,每個節點myid的值和zoo.cfg中的server.N一致
echo "1" > /usr/local/hadoop/zookeeper/data/myid
echo "2" > /usr/local/hadoop/zookeeper/data/myid
echo "3" > /usr/local/hadoop/zookeeper/data/myid
cat /usr/local/hadoop/zookeeper/data/myid
zookeeper的選舉機制和myid密切關聯,myid值大的相對更容易成爲leader,具體這裏就不詳細討論了。
如果不配置myid,會在 zookeeper.out 文件中拋出錯誤
[myid:] - ERROR [main:QuorumPeerMain@85] - Invalid config, exiting abnormally
org.apache.zookeeper.server.quorum.QuorumPeerConfig$ConfigException: Error processing /usr/local/hadoop/zookeeper/bin/../conf/zoo.cfg
五、配置環境變量
編輯 /etc/profile 文件
vim /etc/profile
添加一下內容
export ZOO_HOME=/usr/local/hadoop/zookeeper
export PATH=$PATH:$ZOO_HOME/bin
六、啓動zookeeper
zkServer.sh start
zkServer.sh status
jps
這裏第二個節點選舉成了leader