Zookeeper的單機模式與偽分散式模式的搭建
一、Zookeeper的搭建方式
Zookeeper安裝方式有三種,單機模式和叢集模式以及偽叢集模式。
1.單機模式:Zookeeper只執行在一臺伺服器上,適合測試環境;
2.偽叢集模式:就是在一臺物理機上執行多個Zookeeper 例項。
3.叢集模式:Zookeeper運行於一個叢集上,適合生產環境,這個計算機叢集被稱為一個“集合體”(ensemble)。
Zookeeper通過複製來實現高可用性,只要集合體中半數以上的機器處於可用狀態,它就能夠保證服務繼續。為什麼一定要超過半數呢?這跟Zookeeper的複製策略有關:zookeeper確保對znode 樹的每一個修改都會被複制到集合體中超過半數的機器上。
單機模式搭建:
(1)下載zookeeper
(2)解壓安裝包
(3)修改配置檔案,即在根目錄下的conf下有一個zoo_sample.cfg檔案,將其複製一份到當前資料夾下並更改名稱為zoo.cfg;
(4)修改其中的配置
(5)在/etc/profile下新增zookeeper的配置資訊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/services/zookeeper/data 在根目錄建立資料夾data,用於存放資料 #日誌資料夾 dataLogDir=/usr/local/services/zookeeper/logs 在根目錄建立資料夾logs,用於存放日誌 # the port at which the clients will connect clientPort=2181
export ZOOKEEPER_HOME=/usr/local/services/zookeeper/zookeeper-3.4.9/
export PATH=$ZOOKEEPER_HOME/bin:$PATH
export PATH
此時單機模式的配置完成,在zookeeper根目錄下啟動
bin/zkServer.sh start 開啟
bin/zkServer.sh stop 關閉
偽分佈叢集模式建立資料夾
mkdir /usr/local/services/zookeeper/server1
mkdir /usr/local/services/zookeeper/server2
mkdir /usr/local/services/zookeeper/server3
將zookeeper解壓後分別複製到server1,server2,server3下,再在server1,server2,server3下建立data,log資料夾,在data資料夾下分別建立檔案 myid ,在裡面分別新增內容:1,2,3 對用與分散式伺服器中配置檔案 server.number=A:B:C 中的 number。接著修改server1,server2,server3下zookeeper的配置檔案,此時配置檔案根單機模式下不同,
server1下配置檔案:
# 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=/tmp/zookeeper
#資料資料夾
dataDir=/usr/local/services/zookeeper/server1/data
#dataDir=./data
#日誌資料夾
dataLogDir=/usr/local/services/zookeeper/server1/log
server.1=127.0.0.1:2222:2223
server.2=127.0.0.1:3333:3334
server.3=127.0.0.1:4444:4445
# the port at which the clients will connect
clientPort=2181 //由於是在同一臺機器上啟動多個伺服器,因此每個客戶端埠必須不同 server2中設定為2182 ,server3中設定為2183
# 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
server2下配置檔案:
# 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=/tmp/zookeeper
#資料資料夾
dataDir=/usr/local/services/zookeeper/server1/data
#dataDir=./data
#日誌資料夾
dataLogDir=/usr/local/services/zookeeper/server1/log
server.1=127.0.0.1:2222:2223
server.2=127.0.0.1:3333:3334
server.3=127.0.0.1:4444:4445
# the port at which the clients will connect
clientPort=2182
# 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配置檔案:
# 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=/tmp/zookeeper
#資料資料夾
dataDir=/usr/local/services/zookeeper/server1/data
#dataDir=./data
#日誌資料夾
dataLogDir=/usr/local/services/zookeeper/server1/log
server.1=127.0.0.1:2222:2223
server.2=127.0.0.1:3333:3334
server.3=127.0.0.1:4444:4445
# the port at which the clients will connect
clientPort=2183
# 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
注意事項
配置檔案中引數含義:
其中各配置項的含義,解釋如下:
1.tickTime:CS通訊心跳時間
Zookeeper 伺服器之間或客戶端與伺服器之間維持心跳的時間間隔,也就是每個 tickTime 時間就會發送一個心跳。tickTime以毫 秒為單位。tickTime=2000
2.initLimit:LF初始通訊時限
叢集中的follower伺服器(F)與leader伺服器(L)之間初始連線時能容忍的最多心跳數(tickTime的數量)。
initLimit=5
3.syncLimit:LF同步通訊時限
叢集中的follower伺服器與leader伺服器之間請求和應答之間能容忍的最多心跳數(tickTime的數量)。
syncLimit=2
4.dataDir:資料檔案目錄
Zookeeper儲存資料的目錄,預設情況下,Zookeeper將寫資料的日誌檔案也儲存在這個目錄裡。
dataDir=/home/michael/opt/zookeeper/data
5.clientPort:客戶端連線埠
客戶端連線 Zookeeper 伺服器的埠,Zookeeper 會監聽這個埠,接受客戶端的訪問請求。
clientPort=2181
6.伺服器名稱與地址:叢集資訊(伺服器編號,伺服器地址,LF通訊埠,選舉埠)
這個配置項的書寫格式比較特殊,規則如下:
server.N=YYY:A:B
在一臺機器上部署了3個server,需要注意的是在叢集為分散式模式下我們使用的每個配置文件模擬一臺機器,也就是說單臺機器及上執行多個Zookeeper例項。但是,必須保證每個配置文件的各個埠號不能衝突,除了clientPort不同之外,dataDir也不同。另外,還要在dataDir所對應的目錄中建立myid檔案來指定對應的Zookeeper伺服器例項。
(1)clientPort埠:如果在1臺機器上部署多個server,那麼每臺機器都要不同的 clientPort,比如 server1是2181,server2是2182,server3是2183,
(2)dataDir和dataLogDir:dataDir和dataLogDir也需要區分下,將資料檔案和日誌檔案分開存放,同時每個server的這兩變數所對應的路徑都是不同的。
(3)server.X和myid: server.X 這個數字就是對應,data/myid中的數字。在3個server的myid檔案中分別寫入了0,1,2,那麼每個server中的zoo.cfg都配 server.0 server.2,server.3就行了。因為在同一臺機器上,後面連著的2個埠,3個server都不要一樣,否則埠衝突。
啟動zookeeper叢集
啟動在叢集偽分散式下,我們只有一臺機器,按時要執行三個Zookeeper例項。此時,如果在使用單機模式的啟動命令是行不通的。此時,只要通過下面三條命令就能執行前面所配置的Zookeeper服務。
在每個server下面啟動各自的伺服器:
server1下:bin/zkServer.sh start
server2下:bin/zkServer.sh start
server3下:bin/zkServer.sh start
通過 jps 來檢視是否啟動成功:
[root@localhost server1]# jps -l
9121 org.apache.zookeeper.server.quorum.QuorumPeerMain
8963 org.apache.zookeeper.server.quorum.QuorumPeerMain
9064 org.apache.zookeeper.server.quorum.QuorumPeerMain
12107 sun.tools.jps.Jps
可以檢視節點執行狀態:
[root@localhost zookeeper-3.4.9]# bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/services/zookeeper/server1/zookeeper-3.4.9/bin/../conf/zoo.cfg
Mode: leader
[root@localhost zookeeper-3.4.9]# bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/services/zookeeper/server2/zookeeper-3.4.9/bin/../conf/zoo.cfg
Mode: follower