一、單機(jī)環(huán)境搭建#
1.1 下載#
下載對應(yīng)版本 Zookeeper,這里我下載的版本 3.4.14。官方下載地址:https://archive.apache.org/dist/zookeeper/
# wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
1.2 解壓#
# tar -zxvf zookeeper-3.4.14.tar.gz
1.3 配置環(huán)境變量#
添加環(huán)境變量:
export ZOOKEEPER_HOME=/usr/app/zookeeper-3.4.14
export PATH=$ZOOKEEPER_HOME/bin:$PATH
使得配置的環(huán)境變量生效:
1.4 修改配置#
進(jìn)入安裝目錄的 conf/ 目錄下,拷貝配置樣本并進(jìn)行修改:
# cp zoo_sample.cfg zoo.cfg
指定數(shù)據(jù)存儲目錄和日志文件目錄(目錄不用預(yù)先創(chuàng)建,程序會自動創(chuàng)建),修改后完整配置如下:
# 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
配置參數(shù)說明:
•tickTime:用于計算的基礎(chǔ)時間單元。比如 session 超時:N*tickTime;
•initLimit:用于集群,允許從節(jié)點連接并同步到 master 節(jié)點的初始化連接時間,以 tickTime 的倍數(shù)來表示;
•syncLimit:用于集群, master 主節(jié)點與從節(jié)點之間發(fā)送消息,請求和應(yīng)答時間長度(心跳機(jī)制);
•dataDir:數(shù)據(jù)存儲位置;
•dataLogDir:日志目錄;
•clientPort:用于客戶端連接的端口,默認(rèn) 2181
1.5 啟動#
由于已經(jīng)配置過環(huán)境變量,直接使用下面命令啟動即可:
1.6 驗證#
使用 JPS 驗證進(jìn)程是否已經(jīng)啟動,出現(xiàn) QuorumPeerMain 則代表啟動成功。
[root@hadoop001 bin]# jps
3814 QuorumPeerMain
二、集群環(huán)境搭建#
為保證集群高可用,Zookeeper 集群的節(jié)點數(shù)最好是奇數(shù),最少有三個節(jié)點,所以這里演示搭建一個三個節(jié)點的集群。這里我使用三臺主機(jī)進(jìn)行搭建,主機(jī)名分別為 hadoop001,hadoop002,hadoop003。
2.1 修改配置#
解壓一份 zookeeper 安裝包,修改其配置文件 zoo.cfg,內(nèi)容如下。之后使用 scp 命令將安裝包分發(fā)到三臺服務(wù)器上:
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper-cluster/data/
dataLogDir=/usr/local/zookeeper-cluster/log/
clientPort=2181
# server.1 這個1是服務(wù)器的標(biāo)識,可以是任意有效數(shù)字,標(biāo)識這是第幾個服務(wù)器節(jié)點,這個標(biāo)識要寫到dataDir目錄下面myid文件里
# 指名集群間通訊端口和選舉端口
server.1=hadoop001:2287:3387
server.2=hadoop002:2287:3387
server.3=hadoop003:2287:3387
2.2 標(biāo)識節(jié)點#
分別在三臺主機(jī)的 dataDir 目錄下新建 myid 文件,并寫入對應(yīng)的節(jié)點標(biāo)識。Zookeeper 集群通過 myid 文件識別集群節(jié)點,并通過上文配置的節(jié)點通信端口和選舉端口來進(jìn)行節(jié)點通信,選舉出 Leader 節(jié)點。
創(chuàng)建存儲目錄:
# 三臺主機(jī)均執(zhí)行該命令
mkdir -vp /usr/local/zookeeper-cluster/data/
創(chuàng)建并寫入節(jié)點標(biāo)識到 myid 文件:
# hadoop001主機(jī)
echo "1" > /usr/local/zookeeper-cluster/data/myid
# hadoop002主機(jī)
echo "2" > /usr/local/zookeeper-cluster/data/myid
# hadoop003主機(jī)
echo "3" > /usr/local/zookeeper-cluster/data/myid
2.3 啟動集群#
分別在三臺主機(jī)上,執(zhí)行如下命令啟動服務(wù):
/usr/app/zookeeper-cluster/zookeeper/bin/zkServer.sh start
2.4 集群驗證#
啟動后使用 zkServer.sh status 查看集群各個節(jié)點狀態(tài)。如圖所示:三個節(jié)點進(jìn)程均啟動成功,并且 hadoop002 為 leader 節(jié)點,hadoop001 和 hadoop003 為 follower 節(jié)點。

更多大數(shù)據(jù)系列文章可以參見 GitHub 開源項目:大數(shù)據(jù)入門指南
總結(jié)
以上所述是小編給大家介紹的Zookeeper 單機(jī)環(huán)境和集群環(huán)境搭建,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!