1. 程式人生 > >zookeeper 安裝的三種模式

zookeeper 安裝的三種模式

Zookeeper安裝

zookeeper的安裝分為三種模式:單機模式、叢集模式和偽叢集模式。

  • 單機模式

    首先,從Apache官網下載一個Zookeeper穩定版本,本次教程採用的是zookeeper-3.4.9版本。

http://apache.fayea.com/zookeeper/zookeeper-3.4.9/

     然後解壓zookeeper-3.4.9.tar.gz檔案到安裝目錄下:

tar -zxvf zookeepre-3.4.9.tar.gz

  zookeeper要求Java執行環境,並且需要jdk版本1.6以上。為了以後的操作方便,可以對zookeeper的環境變數進行配置(該步驟可忽略)。方法如下,在/etc/profile檔案中加入以下內容:

#Set Zookeeper Environment

export ZOOKEEPER_HOME=/root/zookeeper-3.4.9
export PATH=$ZOOKEEPER_HOME/bin;$ZOOKEEPER_HOME/conf

   Zookeeper伺服器包含在單個jar檔案中(本環境下為 zookeeper-3.4.9.jar),安裝此服務需要使用者自己建立一個配置檔案。預設配置檔案路徑為 Zookeeper-3.4.9/conf/目錄下,檔名為zoo.cfg。進入conf/目錄下可以看到一個zoo_sample.cfg檔案,可供參考。通過以下程式碼在conf目錄下建立zoo.cfg檔案:

gedit zoo.cfg 

 在檔案中輸入以下內容並儲存

tickTime=2000dataDir=/home/jxwch/hadoop/data/zookeeper

dataLogDir=/home/jxwch/hadoop/dataLog/zookeeper

clientPort
=2181

  在這個檔案中,各個語句的含義:

    tickTime : 伺服器與客戶端之間互動的基本時間單元(ms)

    dataDir : 儲存zookeeper資料路徑

    dataLogDir : 儲存zookeeper日誌路徑,當此配置不存在時預設路徑與dataDir一致

    clientPort : 客戶端訪問zookeeper時經過伺服器端時的埠號

  使用單機模式時需要注意,在這種配置方式下,如果zookeeper伺服器出現故障,zookeeper服務將會停止。

  • 叢集模式

    zookeeper最主要的應用場景是叢集,下面介紹如何在一個叢集上部署一個zookeeper。只要叢集上的大多數zookeeper服務啟動了,那麼總的zookeeper服務便是可用的。另外,最好使用奇數臺伺服器。如歌zookeeper擁有5臺伺服器,那麼在最多2臺伺服器出現故障後,整個服務還可以正常使用。

    之後的操作和單機模式的安裝類似,我們同樣需要Java環境,下載最新版的zookeeper並配置相應的環境變數。不同之處在於每臺機器上的conf/zoo.cfg配置檔案的引數設定不同,使用者可以參考下面的配置:

tickTime=2000

initLimit=10

syncLimit=5

dataDir=/home/jxwch/server1/data

dataLogDir=/home/jxwch/server1/dataLog

clientPort=2181

server.1=zoo1:2888:3888

server.2=zoo2:2888:3888

server.3=zoo3:2888:3888

maxClientCnxns=60

    在這個配置檔案中,新出現的語句的含義:

      initLimit : 此配置表示允許follower連線並同步到leader的初始化時間,它以tickTime的倍數來表示。當超過設定倍數的tickTime時間,則連線失敗。

      syncLimit : Leader伺服器與follower伺服器之間資訊同步允許的最大時間間隔,如果超過次間隔,預設follower伺服器與leader伺服器之間斷開連結。

      maxClientCnxns : 限制連線到zookeeper伺服器客戶端的數量

      server.id=host:port:port : 表示了不同的zookeeper伺服器的自身標識,作為叢集的一部分,每一臺伺服器應該知道其他伺服器的資訊。使用者可以從“server.id=host:port:port” 中讀取到相關資訊。在伺服器的data(dataDir引數所指定的目錄)下建立一個檔名為myid的檔案,這個檔案的內容只有一行,指定的是自身的id值。比如,伺服器“1”應該在myid檔案中寫入“1”。這個id必須在叢集環境中伺服器標識中是唯一的,且大小在1~255之間。這一樣配置中,zoo1代表第一臺伺服器的IP地址。第一個埠號(port)是從follower連線到leader機器的埠,第二個埠是用來進行leader選舉時所用的埠。所以,在叢集配置過程中有三個非常重要的埠:clientPort:2181、port:2888、port:3888。

  • 偽叢集模式

    偽叢集模式就是在單機環境下模擬叢集的Zookeeper服務。

    在zookeeper叢集配置檔案中,clientPort引數用來設定客戶端連線zookeeper伺服器的埠。server.1=IP1:2888:3888中,IP1指的是組成Zookeeper伺服器的IP地址,2888為組成zookeeper伺服器之間的通訊埠,3888為用來選舉leader的埠。由於偽叢集模式中,我們使用的是同一臺伺服器,也就是說,需要在單臺機器上執行多個zookeeper例項,所以我們必須要保證多個zookeeper例項的配置檔案的client埠不能衝突。

    下面簡單介紹一下如何在單臺機器上建立偽叢集模式。首先將zookeeper-3.4.9.tar.gz分別解壓到server1,server2,server3目錄下:

tar -zxvf zookeeper-3.4.9.tar.gz  /home/jxwch/server1

tar -zxvf zookeeper-3.4.9.tar.gz /home/jxwch/server2

tar -zxvf zookeeper-3.4.9.tar.gz /home/jxwch/server3

    然後在server1/data/目錄下建立檔案myid檔案並寫入“1”,同樣在server2/data/,目錄下建立檔案myid並寫入“2”,server3進行同樣的操作。

    下面分別展示在server1/conf/、server2/conf/、server3/conf/目錄下的zoo.cfg檔案:

    server1/conf/zoo.cfg檔案

# Server 1
# The number of milliseconds of each tick
# 伺服器與客戶端之間互動的基本時間單元(ms)
tickTime=2000

# The number of ticks that the initial 
# synchronization phase can take
# 此配置表示允許follower連線並同步到leader的初始化時間,它以tickTime的倍數來表示。當超過設定倍數的tickTime時間,則連線失敗。
initLimit=10

# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
# Leader伺服器與follower伺服器之間資訊同步允許的最大時間間隔,如果超過次間隔,預設follower伺服器與leader伺服器之間斷開連結
syncLimit=5

# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
# 儲存zookeeper資料,日誌路徑
dataDir=/home/jxwch/server1/data
dataLogDir=/home/jxwch/server1/dataLog

# the port at which the clients will connect
# 客戶端與zookeeper相互互動的埠
clientPort=2181
server.1= 127.0.0.1:2888:3888
server.2= 127.0.0.1:2889:3889
server.3= 127.0.0.1:2890:3890

#server.A=B:C:D  其中A是一個數字,代表這是第幾號伺服器;B是伺服器的IP地址;C表示伺服器與群集中的“領導者”交換資訊的埠;當領導者失效後,D表示用來執行選舉時伺服器相互通訊的埠。

# the maximum number of client connections.
# increase this if you need to handle more clients
# 限制連線到zookeeper伺服器客戶端的數量
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

    server2/conf/zoo.cfg檔案

# Server 2
# The number of milliseconds of each tick
# 伺服器與客戶端之間互動的基本時間單元(ms)
tickTime=2000

# The number of ticks that the initial 
# synchronization phase can take
# zookeeper所能接受的客戶端數量
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.
# 儲存zookeeper資料,日誌路徑
dataDir=/home/jxwch/server2/data
dataLogDir=/home/jxwch/server2/dataLog

# the port at which the clients will connect
# 客戶端與zookeeper相互互動的埠
clientPort=2182
server.1= 127.0.0.1:2888:3888
server.2= 127.0.0.1:2889:3889
server.3= 127.0.0.1:2890:3890

#server.A=B:C:D  其中A是一個數字,代表這是第幾號伺服器;B是伺服器的IP地址;C表示伺服器與群集中的“領導者”交換資訊的埠;當領導者失效後,D表示用來執行選舉時伺服器相互通訊的埠。

# 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

    server3/conf/zoo.cfg檔案

# Server 3
# The number of milliseconds of each tick
# 伺服器與客戶端之間互動的基本時間單元(ms)
tickTime=2000

# The number of ticks that the initial 
# synchronization phase can take
# zookeeper所能接受的客戶端數量
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.
# 儲存zookeeper資料,日誌路徑
dataDir=/home/jxwch/server3/data
dataLogDir=/home/jxwch/server3/dataLog

# the port at which the clients will connect
# 客戶端與zookeeper相互互動的埠
clientPort=2183
server.1= 127.0.0.1:2888:3888
server.2= 127.0.0.1:2889:3889
server.3= 127.0.0.1:2890:3890

#server.A=B:C:D  其中A是一個數字,代表這是第幾號伺服器;B是伺服器的IP地址;C表示伺服器與群集中的“領導者”交換資訊的埠;當領導者失效後,D表示用來執行選舉時伺服器相互通訊的埠。

# 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

  從上述三個程式碼清單可以發現,除了clientPort不同之外,dataDir和dataLogDir也不同。另外,不要忘記dataDir所對應的目錄中建立的myid檔案來指定對應的zookeeper伺服器例項。

Zookeeper偽叢集模式執行

  當上述偽叢集環境安裝成功後就可以測試是否安裝成功啦,我們可以嚐嚐鮮:

  首先啟動server1伺服器:

cd server1/bin

bash zkServer.sh start

  此時出現以下提示資訊,表示啟動成功:

  開啟zookeeper.out檔案:

2017-02-23 16:17:46,419 [myid:] - INFO  [main:[email protected]] - Reading configuration from: /home/jxwch/server1/bin/../conf/zoo.cfg
2017-02-23 16:17:46,496 [myid:] - INFO  [main:[email protected]] - Resolved hostname: 127.0.0.1 to address: /127.0.0.1
2017-02-23 16:17:46,496 [myid:] - INFO  [main:[email protected]] - Resolved hostname: 127.0.0.1 to address: /127.0.0.1
2017-02-23 16:17:46,497 [myid:] - INFO  [main:[email protected]] - Resolved hostname: 127.0.0.1 to address: /127.0.0.1
2017-02-23 16:17:46,497 [myid:] - INFO  [main:[email protected]] - Defaulting to majority quorums
2017-02-23 16:17:46,511 [myid:1] - INFO  [main:[email protected]] - autopurge.snapRetainCount set to 3
2017-02-23 16:17:46,511 [myid:1] - INFO  [main:[email protected]] - autopurge.purgeInterval set to 0
2017-02-23 16:17:46,511 [myid:1] - INFO  [main:[email protected]] - Purge task is not scheduled.
2017-02-23 16:17:46,525 [myid:1] - INFO  [main:[email protected]] - Starting quorum peer
2017-02-23 16:17:46,631 [myid:1] - INFO  [main:[email protected]] - binding to port 0.0.0.0/0.0.0.0:2181
2017-02-23 16:17:46,664 [myid:1] - INFO  [main:[email protected]] - tickTime set to 2000
2017-02-23 16:17:46,664 [myid:1] - INFO  [main:[email protected]] - minSessionTimeout set to -1
2017-02-23 16:17:46,664 [myid:1] - INFO  [main:[email protected]] - maxSessionTimeout set to -1
2017-02-23 16:17:46,665 [myid:1] - INFO  [main:[email protected]] - initLimit set to 10
2017-02-23 16:17:46,771 [myid:1] - INFO  [main:[email protected]] - Reading snapshot /home/jxwch/server1/data/version-2/snapshot.400000015
2017-02-23 16:17:46,897 [myid:1] - INFO  [ListenerThread:[email protected]] - My election bind port: /127.0.0.1:3888
2017-02-23 16:17:46,913 [myid:1] - INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:[email protected]] - LOOKING
2017-02-23 16:17:46,915 [myid:1] - INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:[email protected]] - New election. My id =  1, proposed zxid=0x500000026
2017-02-23 16:17:46,922 [myid:1] - INFO  [WorkerReceiver[myid=1]:[email protected]] - Notification: 1 (message format version), 1 (n.leader), 0x500000026 (n.zxid), 0x1 (n.round), LOOKING (n.state), 1 (n.sid), 0x5 (n.peerEpoch) LOOKING (my state)
2017-02-23 16:17:46,940 [myid:1] - WARN  [WorkerSender[myid=1]:[email protected]] - Cannot open channel to 2 at election address /127.0.0.1:3889
java.net.ConnectException: 拒絕連線
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:381)
    at org.apache.zookeeper.server.quorum.QuorumCnxManager.toSend(QuorumCnxManager.java:354)
    at org.apache.zookeeper.server.quorum.FastLeaderElection$Messenger$WorkerSender.process(FastLeaderElection.java:452)
    at org.apache.zookeeper.server.quorum.FastLeaderElection$Messenger$WorkerSender.run(FastLeaderElection.java:433)
    at java.lang.Thread.run(Thread.java:745)
2017-02-23 16:17:46,943 [myid:1] - INFO  [WorkerSender[myid=1]:[email protected]] - Resolved hostname: 127.0.0.1 to address: /127.0.0.1
2017-02-23 16:17:46,944 [myid:1] - WARN  [WorkerSender[myid=1]:[email protected]] - Cannot open channel to 3 at election address /127.0.0.1:3890
java.net.ConnectException: 拒絕連線
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at org.apache.zookeeper.server.quorum.QuorumCnxManager.connectOne(QuorumCnxManager.java:381)
    at org.apache.zookeeper.server.quorum.QuorumCnxManager.toSend(QuorumCnxManager.java:354)
    at org.apache.zookeeper.server.quorum.FastLeaderElection$Messenger$WorkerSender.process(FastLeaderElection.java:452)
    at org.apache.zookeeper.server.quorum.FastLeaderElection$Messenger$WorkerSender.run(FastLeaderElection.java:433)
    at java.lang.Thread.run(Thread.java:745)
2017-02-23 16:17:46,944 [myid:1] - INFO  [WorkerSender[myid=1]:[email protected]] - Resolved hostname: 127.0.0.1 to address: /127.0.0.1

    產生上述兩條Waring資訊是因為zookeeper服務的每個例項都擁有全域性的配置資訊,他們在啟動的時候需要隨時隨地的進行leader選舉,此時server1就需要和其他兩個zookeeper例項進行通訊,但是,另外兩個zookeeper例項還沒有啟動起來,因此將會產生上述所示的提示資訊。當我們用同樣的方式啟動server2和server3後就不會再有這樣的警告資訊了。

    當三臺伺服器均成功啟動後切換至server1/bin/目錄下執行以下命令:

bash zkServer.sh status

    終端出現以下提示資訊:

    說明server1伺服器此時處於follower模式,同樣切換至server2/bin目錄下執行相同的命令,返回如下結果:

    說明server2被選舉為leader。

參考資料

相關推薦

zookeeper模式(單機模式,為分散式,完全分散式)

Zookeeper安裝   zookeeper的安裝分為三種模式:單機模式、叢集模式和偽叢集模式。 單機模式     首先,從Apache官網下載一個Zookeeper穩定版本,本次教程採用的是zookeeper-3.4.9版本。 http://a

zookeeper安裝模式

count 最好 profile quest 清單 XA cfg 可用 故障 zookeeper的安裝分為三種模式:單機模式、集群模式和偽集群模式。 1、單機模式 首先,從Apache官網下載一個Zookeeper穩定版本,本次教程采用的是zookeeper-3.4.9版本

zookeeper 安裝模式

Zookeeper安裝 zookeeper的安裝分為三種模式:單機模式、叢集模式和偽叢集模式。 單機模式     首先,從Apache官網下載一個Zookeeper穩定版本,本次教程採用的是zookeeper-3.4.9版本。 http://apache.fayea.com/zookeeper/zook

Hadoop安裝部署的模式

hadoop安裝部署有以下三種模式: 本地模式 偽分佈模式 全分佈模式 安裝之前操作: 1.修改主機名,設定好IP 2.設定hadoop的環境變數: 命令:vi ~/.bash_profile ``` ### add for ha

mysql binlog日誌的模式

base 新版 產生 日誌模式 出現 行數據 原本 兩種模式 可能 1、statement level模式 每一條會修改數據的sql都會記錄到master的bin-log中。slave在復制的時候sql進程會解析成和原來master端執行過的相同的sql來再次執行。優點:s

VMware網絡的模式

vmware1.Bridged模式2.NAT模式3.Host-only模式VMware網絡的三種模式

第十三節: EF的模式() 之 來自數據庫的CodeFirst模式

三種 相同 blog size 好的 不一致 mil 簡介 pan 一. 簡介   【來自數據庫的Code First模式】實質上並不是CodeFirst模式,而是DBFirst模式的輕量級版本,在該模式中取消了edmx模型和T4模板,直接生成了EF上下文和相應的類,該模

Vi編輯器的模式

linux vi編輯器的三種模式 Vi編輯器的三種模式1)一般模式 (光標移動、復制、粘貼、刪除)2)編輯模式 (編輯文本)3)命令行模式 (查找和替換)ESC:返回鍵vi 文件名查找字符串,使用/加上要查找的字符串,如:/abc輸入/後,就進入命令行模式一般模式,輸入: 或 \ 或 ?就進

LVS模式配置及優點缺點比較

111LVS三種模式配置LVS 三種工作模式的優缺點比較LVS三種模式配置LVS三種(LVS-DR,LVS-NAT,LVS-TUN)模式的簡要配置LVS是什麽:http://www.linuxvirtualserver.org/VS-NAT.htmlhttp://www.linuxvirtualserver.

VMware網絡連接模式

vmware 網絡連接VMWARE裏面有三種網絡連接模式,分別是橋接模式、NAT模式、Host-Only模式,推薦使用NAT模式,可以分配更多的IP地址給虛擬機使用。下面分別介紹一下這三種模式。橋接模式:橋接模式就是將主機網卡與虛擬機虛擬的網卡利用虛擬網橋進行通信。在橋接的作用下,類似於把物理主機虛擬為一個交

oop思維意識,類 模塊命名空間,類擴展之繼承 、組合、mixin模式

經驗 .cn 第四版 分享圖片 實例 pytho 模塊 組合 為什麽 python的書都是講怎麽創建類怎麽實例化對象,一般會用使用了,但還不具備這種編程意識。這是從python學習手冊第四版節選出來的,書中說oop不僅是一種技術,更是一種經驗。學習大神的看法,為什麽需

Linux中vim的模式以及基本命令

body 指定 col -s global 使用 全局 oba .com 在Linux中vim的三種模式分別為:命令模式、末行模式、編輯模式。以下是三者的關系圖: 三種模式的彼此切換: 命令模式是vim中的默認模式。 命令模式切換至末行模式: 使用英文冒號(:)。 末行模

VMware下網絡配置的模式

tcp/ip ati 相對 導致 默認 互聯 外部網絡 網絡連接 外部 目錄 一 網絡配置中出現的錯誤及解決方案二 VMware下網絡配置的三種模式簡介1、橋接模式(Bridged)2、網絡地址轉化模式(NAT)3、僅主機模式(host-only) 網絡配置中出現的錯誤及解

nginx虛擬主機模式的簡單實現

_for nod send nop request user 模式 -s hit main配置段: user nginx; #指定用於運行worker進程的用戶和組 worker_processes 4; #worker的進程數;通常應該為CPU的核心數或核心數減1

應用負載均衡之LVS(一):基本概念和模式

保存 訪問 方式 video big key vhdl cisc vid 網站架構中,負載均衡技術是實現網站架構伸縮性的主要手段之一。所謂"伸縮性",是指可以不斷向集群中添加新的服務器來提升性能、緩解不斷增加的並發用戶訪問壓力。通俗地講,就是一頭牛拉不動時,就用兩頭、三

使用nmcli 實現 bond0 網絡組 網橋模式

基礎使用nmcli 實現 bond 網絡組 網橋模式 bond0(負載均衡) step1:創建一個bond0的主屬 nmcli connection add con-name bond0 type bond ifname bond0 mode active-backup 之所以不為綠色是因為還

EF的模式

更新 cnblogs 工程 html model tar 逆向 base exist 1.DateBase First(數據庫優先) 2.Model First(模型優先) 3.Code First(代碼優先) 當然,如果把Code First模式的兩種具體方式獨立出來,那

簡單區別存儲與主機連接的模式DAS/SAN/NAS

nbsp -a 應用服務 多臺 cifs 操作系統。 並發訪問 分享圖片 類別 一般來說,存儲(Storage)與主機(Host)連接模式有三種:SAN(Storage Area Network)存儲區域網絡,DAS(Direct-Attached Storage)

VM Ware中網絡適配器的模式介紹

sha 獨立計算 mage 需要 只需要 獨立 隨機生成 管理 網絡地址轉換 大家在安裝完虛擬機後,默認安裝了兩個虛擬網卡,VMnet1和VMnet8,如下圖:其中VMnet1是host網卡,用於host方式連接網絡的。VMnet8是NAT網卡,用於NAT方式連接網絡的。它

MySQL binlog日誌模式選擇及配置

ble 文件 eve 圖解 nag 一行 人的 三種模式 jpg 在講解binlog日誌三種模式前,先了解一下解析binlog日誌的命令工mysqlbinlog。mysqlbinlog工具的作用是解析mysql的二進制binlog日誌內容,把二進制日誌解析成可以在MySQL