下載對應版本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 開源項目: 程序員大數據入門指南