ZooKeeper集羣環境搭建

zookeeper簡介

       ZooKeeper是一個開放源碼的分佈式應用程序協調服務,是Google Chubby的一個開源實現,是Hadoop和Hbase的重要組件。它是一個爲分佈式應用提供一致性服務的軟件,提供的功能包括:配置維護、域名服務、分佈式同步、組服務等。更多詳細的資料請自行了解。官方鏈接:http://zookeeper.apache.org/

       Zookeeper的安裝和配置十分簡單,既可以配置成單機模式,也可以配置成集羣模式。本文將簡單介紹集羣的配置步驟(單機模式煩請自行百度)

zookeeper集羣搭建前提

  1. 請確認至少有兩臺+主機(如果硬件不夠,可以試用虛擬機);
  2. 能夠 常用Linux命令

第一步:zookeeper下載

      下載地址:http://zookeeper.apache.org/releases.html,我選擇的版本是zookeeper-3.4.9(如果版本不一致可能存在差異)。

      下載下來壓縮包爲zookeeper-3.4.9.tar.gz。請使用tar -xzvf zookeeper-3.4.9.tar.gz解壓

第二步:zookeeper環境變量配置

      命令:vi /etc/profile
      增加zookeeper的環境變量ZOOKEEPER_HOME的本地路徑,並且增加到PATH中。如下圖:

export ZOOKEEPER_HOME=/opt/zkServer1/zookeeper-3.4.9
export PATH=$ZOOKEEPER_HOME/bin:$PATH
export PATH

      命令:source /etc/profile
      修改完成後使配置生效。

第三步:zookeeper配置文件配置

       進入zookeeper的conf子文件夾。/conf/zoo_sample.cfg文件複製一份,並更名爲zoo.cfg。配置dataDir、dataLogDir、增加server節點

# 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=/opt/zookeeper-3.4.9/data
dataLogDir=/opt/zookeeper-3.4.9/logs
# 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
server.1 = node1:2888:3888
server.2 = node2:2888:3888 
server.3 = node3:2888:3888

        server.A=B:C:D  其中 A 是一個數字,表示這個是第幾號服務器;B 是這個服務器的 IP 地址(如果做了域名映射爲域名);C 表示的是這個服務器與集羣中的 Leader 服務器交換信息的端口;D 表示的是萬一集羣中的 Leader 服務器掛了,需要一個端口來重新進行選舉,選出一個新的 Leader,而這個端口就是用來執行選舉時服務器相互通信的端口。

第四步:zookeeper識別文件創建。創建logs目錄、data目錄、myid文件

        在data目錄下創建一個myid文件,然後分別在myid文件中按照zoo.cfg文件的server.A中A的數值,在不同機器上的該文件中填寫相應的值。例如server.1 在當前機器的myid中就只寫上1就可以了。

第五步:Zookeeper配置拷貝到另外一臺機器

        在另外一臺機器上試用scp命令遠程拷貝當前Zookeeper文件夾。然後配置對應的環境變量。注意修改myid文件的值爲2或3。

第六步:Zookeeper啓動驗證

        進入bin目錄執行命令“zkServer.sh start”啓動Zookeeper。不同機器上的Zookeeper需要單獨啓動。執行命令“zkServer.sh stop”將會停止Zookeeper。

        

第七步:zookeeper當前狀態查看

        進入bin目錄執行命令“zkServer.sh status”。
               zookeeper領導者(leader):

               

               zookeeper跟隨者1(follower):

               

               zookeeper跟隨者2(follower):

               

        到此,完成Zookeeper集羣配置工作。

後話

        如果出現Error contacting service. It is probably not running的錯誤的時候,參考以下解決辦法:

       1、查看myid、server.A=B:C:D是否配置正確或者檢查是否由於防火牆開啓的原因;

       2、systemctl stop iptables.service

             systemctl disable iptables.service

       3、注意集羣服務器的啓動順序

        

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章