ZooKeeper安裝和配置

軟件下載


寫(xiě)本文時(shí), ZooKeeper 最新穩(wěn)定版本是 3.4.8, 可以從 ZooKeeper官網(wǎng) 下載 zookeeper-3.4.8.tar.gz。將下載的 zookeeper-3.4.8.tar.gz 文件拷貝到 /opt 目錄下。

安裝和配置


安裝 ZooKeeper 之前需要先安裝 JDK, 關(guān)于 JDK 的安裝這里不再贅述。

  1. 解壓

    sudo tar -zxvf zookeeper-3.4.8.tar.gz
    
  2. 目錄重命名

    sudo mv zookeeper-3.4.8 zookeeper
    
  3. 修改用戶和用戶組

    sudo chown —R hadoop:hadoop zookeeper/
    
  4. 環(huán)境變量配置

編輯 .bashrc 文件, 在文件末尾添加以下環(huán)境變量配置:

   # ZooKeeper Env
   export ZOOKEEPER_HOME=/opt/zookeeper
   export PATH=$PATH:$ZOOKEEPER_HOME/bin

運(yùn)行以下命令使環(huán)境變量生效:

   source .bashrc

核心配置文件zoo.cfg


初次使用 ZooKeeper 時(shí), 需要將 $ZOOKEEPER_HOME/conf 目錄下的 zoo_sample.cfg 重命名為 zoo.cfg, zoo.cfg 默認(rèn)配置如下:

# The number of milliseconds of each 
ticktickTime=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=/tmp/zookeeper
# 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

配置項(xiàng)說(shuō)明如下:

  • tickTime: ZooKeeper 中使用的基本時(shí)間單元, 以毫秒為單位, 默認(rèn)值是 2000。它用來(lái)調(diào)節(jié)心跳和超時(shí)。例如, 默認(rèn)的會(huì)話超時(shí)時(shí)間是兩倍的 tickTime。

  • initLimit: 默認(rèn)值是 10, 即 tickTime 屬性值的 10 倍。它用于配置允許 followers 連接并同步到 leader 的最大時(shí)間。如果 ZooKeeper 管理的數(shù)據(jù)量很大的話可以增加這個(gè)值。

  • syncLimit: 默認(rèn)值是 5, 即 tickTime 屬性值的 5 倍。它用于配置leader 和 followers 間進(jìn)行心跳檢測(cè)的最大延遲時(shí)間。如果在設(shè)置的時(shí)間內(nèi) followers 無(wú)法與 leader 進(jìn)行通信, 那么 followers 將會(huì)被丟棄。

  • dataDir: ZooKeeper 用來(lái)存儲(chǔ)內(nèi)存數(shù)據(jù)庫(kù)快照的目錄, 并且除非指定其它目錄, 否則數(shù)據(jù)庫(kù)更新的事務(wù)日志也將會(huì)存儲(chǔ)在該目錄下。建議配置 dataLogDir 參數(shù)來(lái)指定 ZooKeeper 事務(wù)日志的存儲(chǔ)目錄。

  • clientPort: 服務(wù)器監(jiān)聽(tīng)客戶端連接的端口, 也即客戶端嘗試連接的端口, 默認(rèn)值是 2181。

  • maxClientCnxns: 在 socket 級(jí)別限制單個(gè)客戶端與單臺(tái)服務(wù)器之前的并發(fā)連接數(shù)量, 可以通過(guò) IP 地址來(lái)區(qū)分不同的客戶端。它用來(lái)防止某種類(lèi)型的 DoS 攻擊, 包括文件描述符耗盡。默認(rèn)值是 60。將其設(shè)置為 0 將完全移除并發(fā)連接數(shù)的限制。

  • autopurge.snapRetainCount: 配置 ZooKeeper 在自動(dòng)清理的時(shí)候需要保留的數(shù)據(jù)文件快照的數(shù)量和對(duì)應(yīng)的事務(wù)日志文件, 默認(rèn)值是 3。

  • autopurge.purgeInterval: 和參數(shù) autopurge.snapRetainCount 配套使用, 用于配置 ZooKeeper 自動(dòng)清理文件的頻率, 默認(rèn)值是 1, 即默認(rèn)開(kāi)啟自動(dòng)清理功能, 設(shè)置為 0 則表示禁用自動(dòng)清理功能。

更多 ZooKeeper 配置后面會(huì)寫(xiě)文章詳細(xì)描述。

單機(jī)模式


  1. zoo.cfg配置

    ticketTime=2000
    clientPort=2181
    dataDir=/opt/zookeeper/data
    dataLogDir=/opt/zookeeper/logs
    
  2. 啟動(dòng) ZooKeeper 服務(wù)

可以使用如下命令來(lái)啟動(dòng) ZooKeeper 服務(wù)

   zkServer.sh start

服務(wù)啟動(dòng)信息如下:


  1. 驗(yàn)證 ZooKeeper 服務(wù)

服務(wù)啟動(dòng)完成后, 可以使用 telnet 和 stat 命令驗(yàn)證服務(wù)器啟動(dòng)是否正常:


在單機(jī)模式中, Mode 的值是 "standalone"。

  1. 停止 ZooKeeper 服務(wù)

想要停止 ZooKeeper 服務(wù), 可以使用如下命令:

   zkServer.sh stop

服務(wù)停止信息如下:


集群模式


  1. zoo.cfg配置

在 master 機(jī)器上, 在單機(jī)模式的配置文件下增加了最后 5 行配置:

   ticketTime=2000
   clientPort=2181
   dataDir=/opt/zookeeper/data
   dataLogDir=/opt/zookeeper/logs
   initLimit=10
   syncLimit=5
   server.1=master:2888:3888
   server.2=slave01:2888:3888
   server.3=slave02:2888:3888

相關(guān)配置說(shuō)明:

  • 集群模式中, 集群中的每臺(tái)機(jī)器都需要感知其它機(jī)器, 在 zoo.cfg 配置文件中, 可以按照如下格式進(jìn)行配置, 每一行代表一臺(tái)服務(wù)器配置:

     server.id=host:port:port
    

    id 被稱(chēng)為 Server ID, 用來(lái)標(biāo)識(shí)服務(wù)器在集群中的序號(hào)。同時(shí)每臺(tái) ZooKeeper 服務(wù)器上, 都需要在數(shù)據(jù)目錄(即 dataDir 指定的目錄) 下創(chuàng)建一個(gè) myid 文件, 該文件只有一行內(nèi)容, 即對(duì)應(yīng)于每臺(tái)服務(wù)器的Server ID。

  • ZooKeeper 集群中, 每臺(tái)服務(wù)器上的 zoo.cfg 配置文件內(nèi)容一致。

  • server.1 的 myid 文件內(nèi)容就是 "1"。每個(gè)服務(wù)器的 myid 內(nèi)容都不同, 且需要保證和自己的 zoo.cfg 配置文件中 "server.id=host:port:port" 的 id 值一致。

  • id 的范圍是 1 ~ 255。

  1. 創(chuàng)建myid文件

在 dataDir 指定的目錄下 (即 /opt/zookeeper/data 目錄) 創(chuàng)建名為 myid 的文件, 文件內(nèi)容和 zoo.cfg 中當(dāng)前機(jī)器的 id 一致。根據(jù)上述配置, master 的 myid 文件內(nèi)容為 1。

  1. slave配置

按照相同步驟, 為 slave01 和 slave02 配置 zoo.cfg 和 myid 文件。zoo.cfg文件內(nèi)容相同, slave01 的 myid 文件內(nèi)容為 2, slave02 的 myid 文件內(nèi)容為 3。

  1. 集群?jiǎn)?dòng)

在集群中的每臺(tái)機(jī)器上執(zhí)行以下啟動(dòng)命令:

   zkServer.sh start

啟動(dòng)信息如下:


master 和 slave01 兩臺(tái)服務(wù)器的 Mode 值均為 follower, 表明它們?cè)诩褐械慕巧珵?Follower。


slave02 服務(wù)器的 Mode 值為 leader, 表明它在集群中的角色為 Leader。

參考資料

http://zookeeper.apache.org/doc/current/zookeeperStarted.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容