【ZooKeeper系列】1.ZooKeeper單機版、偽叢集和叢集環境搭建
ZooKeeper安裝模式主要有3種:
- 單機版(Standalone模式)模式:僅有一個ZooKeeper服務
- 偽叢集模式:單機多個ZooKeeper服務
- 叢集模式:多機多ZooKeeper服務
1 單機版(Standalone模式)安裝
ZooKeeper官網下載地址:http://zookeeper.apache.org/releases.html#download
如圖所示進行操作:
注意一點,如果不想當小白鼠,請務必下穩定版(stable release),非穩定版安裝時可能出各種未知的異常。
以3.4.14
版本為例,在Centos系統
下進行安裝,之前寫一些軟體的安裝教程時,有人留言說希望把安裝的步驟儘量詳細化,包括安裝路徑也要帶上,做到可以照著教程複製操作。這個要求有點,呵呵,滿足你!
1.1 下載安裝包
輸入如下命令:
wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
如下圖:
1.2 解壓安裝包
tar -zxvf apache-zookeeper-3.4.14.tar.gz
解壓完成後,將解壓包移動到/usr目錄:
mv apache-zookeeper-3.4.14 /usr/
並將apache-zookeeper-3.4.14重新命名為zookeeper-3.4.14。
至此可以看到ZooKeeper的目錄結構如下:
[root@instance-e5cf5719 zookeeper-3.4.14]# ls bin data ivy.xml logs README.md zookeeper-3.4.14.jar zookeeper-3.4.14.jar.sha1 zookeeper-docs zookeeper-recipes build.xml dist-maven lib NOTICE.txt README_packaging.txt zookeeper-3.4.14.jar.asc zookeeper-client zookeeper-it zookeeper-server conf ivysettings.xml LICENSE.txt pom.xml src zookeeper-3.4.14.jar.md5 zookeeper-contrib zookeeper-jute
- bin目錄——zk的可執行指令碼目錄,包括zk服務程序,zk客戶端,等指令碼。其中,.sh是Linux環境下的指令碼,.cmd是Windows環境下的指令碼。
- conf目錄——配置檔案目錄。zoo_sample.cfg為樣例配置檔案,需要修改為自己的名稱,一般為zoo.cfg。log4j.properties為日誌配置檔案。
1.3 設定zoo.cfg
進入/usr/zookeeper-3.4.14/conf目錄,可以看到zoo_sample.cfg,這是樣例配置檔案,需要修改為自己的,一般命令為zoo.cfg 。
cp zoo_sample.cfg zoo.cfg
可以看看zoo.cfg檔案裡的內容:
# 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
# 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
看著好複雜的感覺,其實去掉註釋後,就只有幾行而已:
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zookeeper
clientPort=2181
- tickTime=2000 :通俗點叫
滴答時間
,就是心跳間隔,預設是2000毫秒,即每隔兩秒心跳一次。- tickTime用於客戶端與伺服器或伺服器與伺服器之間維持心跳的時間度量單位,即每隔tickTime會發送一次心跳。
- 心跳的作用:
- 監聽機器的工作狀態。
- 通過心跳來控制follower跟leader的通訊時間,預設情況下他們(follower和leader)的會話時長是心跳間隔的兩倍,即2 * tickTime。
- initLimit=10:follower在啟動過程中,會從leader同步所有最新資料,然後確定自己能夠對外服務的起始狀態,leader允許follower在initLimit時間內完成工作。預設值是10,即10*tickTime。預設情況下不需要修改該配置項,隨著ZooKeeper叢集管理的數量不斷增大,follower節點在啟動的時候,從leader節點進行資料同步的時間也會相應變長,於是無法在較短的時間內完成資料同步,在這種情況下,需要適當調大這個引數。
- syncLimit=5:leader節點和follower節點進行心跳檢測的最大延遲時間。在ZooKeeper叢集中,leader節點會與所有的follower節點進行心跳檢測來確認節點是否存活。預設值為5,即5*tickTime。
- dataDir=/tmp/zookeeper:ZooKeeper伺服器儲存快照檔案的預設目錄。/tmp目錄下的檔案可能被自動刪除,容易丟失,需要修改存放目錄。
- clientPort=2181: 客戶端連線ZooKeeper伺服器的埠。ZooKeeper會監聽這個埠,接收客戶端的訪問請求。
溫馨提示
:大家一定要學會看官方文件,去接收第一手資料。雖然是英文,但用詞和語法都比較簡單,很容易看懂。
官網介紹如下:
- tickTime : the basic time unit in milliseconds used by ZooKeeper. It is used to do heartbeats and the minimum session timeout will be twice the tickTime.
- dataDir : the location to store the in-memory database snapshots and, unless specified otherwise, the transaction log of updates to the database.
- clientPort : the port to listen for client connections
在zookeeper-3.4.14目錄下建立data和logs檔案,如下:
[root@instance-e5cf5719 zookeeper-3.4.14]# mkdir data
[root@instance-e5cf5719 zookeeper-3.4.14]# mkdir logs
官方文件也有對此進行說明,指出在生產環境中ZooKeeper是會長期執行的,ZooKeeper的儲存就需要專門的檔案位置進行儲存dataDir和logs。
data資料夾用於存放記憶體資料庫快照,叢集的myid檔案也是存放在這個資料夾下。
For long running production systems ZooKeeper storage must be managed externally (dataDir and logs).
修改後的zoo.cfg如下:
# 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/zookeeper-3.4.14/data
# 日誌資料夾
dataLogDir=/usr/zookeeper-3.4.14/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
1.4 啟動
進入ZooKeeper的bin目錄:
[root@instance-e5cf5719 zookeeper-3.4.14]# cd bin/
[root@instance-e5cf5719 bin]# ls
README.txt zkCleanup.sh zkCli.cmd zkCli.sh zkEnv.cmd zkEnv.sh zkServer.cmd zkServer.sh zkTxnLogToolkit.cmd zkTxnLogToolkit.sh zookeeper.out
- zkCleanup.sh :用於清理ZooKeeper的歷史資料,包括事務日誌檔案與快照資料檔案
- zkCli.sh:連線ZooKeeper伺服器的命令列客戶端
- zkEnv.sh:設定環境變數
- zkServer.sh:啟動ZooKeeper伺服器
啟動ZooKeeper:
./zkServer.sh start
成功啟動如下圖所示:
可以檢視ZooKeeper的狀態:
./zkServer.sh status
狀態資訊如下圖所示:
可以通過help
看看./zkServer.sh下的命令
[root@instance-e5cf5719 bin]# ./zkServer.sh help
ZooKeeper JMX enabled by default
Using config: /usr/zookeeper-3.4.14/bin/../conf/zoo.cfg
Usage: ./zkServer.sh {start|start-foreground|stop|restart|status|upgrade|print-cmd}
- start: 啟動,用於後臺啟動ZooKeeper伺服器
- start-foreground: 前臺啟動伺服器
- stop:停止
- restart:重啟
- status:獲取狀態
- upgrade:升級
- print-cmd : 列印ZooKeeper程式命令列及相關引數
1.5 連線ZooKeeper客戶端
進行連線:
./zkCli.sh -server 127.0.0.1:2181
即
./zkCli.sh -server <ip>:<port>
結果如下:
可以通過help獲取更多的相關命令:
[zk: 127.0.0.1:2181(CONNECTED) 0] help
ZooKeeper -server host:port cmd args
stat path [watch]
set path data [version]
ls path [watch]
delquota [-n|-b] path
ls2 path [watch]
setAcl path acl
setquota -n|-b val path
history
redo cmdno
printwatches on|off
delete path [version]
sync path
listquota path
rmr path
get path [watch]
create [-s] [-e] path data acl
addauth scheme auth
quit
getAcl path
close
connect host:port
命令 | 描述 |
---|---|
help | 顯示所有操作命令 |
stat | 檢視節點狀態,即判斷節點是否存在 |
set | 更新節點資料 |
get | 獲取節點資料 |
ls path [watch] | 使用 ls 命令來檢視當前znode的內容 |
create | 普通建立 ; -s 含有序列;-e 臨時(重啟或者超時消失) |
delete | 刪除節點 |
rmr | 遞迴刪除節點 |
可以對相關的命令進行一些簡單的測試,先建立一個新znode(執行create
/zk_test my_data ),裡面附帶的資訊為“my_data”.
[zk: 127.0.0.1:2181(CONNECTED) 1] create /zk_test my_data
Created /zk_test
[zk: 127.0.0.1:2181(CONNECTED) 2] ls /
[zookeeper, zk_test]
可以看到zk_test建立成功了。可以通過get
命令看看zk_test節點裡的資訊:
[zk: 127.0.0.1:2181(CONNECTED) 3] get /zk_test
my_data
cZxid = 0x7
ctime = Thu Dec 05 16:32:20 CST 2019
mZxid = 0x7
mtime = Thu Dec 05 16:32:20 CST 2019
pZxid = 0x7
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0
通過set
可以修改zk_test裡的資訊。
[zk: 127.0.0.1:2181(CONNECTED) 4] set /zk_test junk
cZxid = 0x7
ctime = Thu Dec 05 16:32:20 CST 2019
mZxid = 0x8
mtime = Thu Dec 05 16:37:03 CST 2019
pZxid = 0x7
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
[zk: 127.0.0.1:2181(CONNECTED) 5] get /zk_test
junk
cZxid = 0x7
ctime = Thu Dec 05 16:32:20 CST 2019
mZxid = 0x8
mtime = Thu Dec 05 16:37:03 CST 2019
pZxid = 0x7
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
通過delete
可以刪除節點。
[zk: 127.0.0.1:2181(CONNECTED) 6] delete /zk_test
[zk: 127.0.0.1:2181(CONNECTED) 7] ls /
[zookeeper]
2 偽叢集搭建
我們搭建3個ZooKeeper來構建偽叢集。上面我們已經搭建了zookeeper-3.4.14,現在將它複製兩份,命名為zookeeper-3.4.14-1,zookeeper-3.4.14-2。
[root@instance-e5cf5719 usr]# cp -r zookeeper-3.4.14 zookeeper-3.4.14-1
[root@instance-e5cf5719 usr]# cp -r zookeeper-3.4.14 zookeeper-3.4.14-2
此時3個ZooKeeper檔案是一模一樣的,構建偽叢集需要對每個ZooKeeper的配置檔案做一點小修改。
對3個ZooKeeper中/conf/zoo.cfg進行修改,主要是修改3個位置:埠號
、日誌路徑
、叢集配置
。
在zoo.cfg配置中,添加了一組server配置,表示ZooKeeper叢集中有3個節點,server的配置格式如下:
server.<myid>=<IP>:<Port1>:<Port2>
myid
:是節點的編號,該編號的取值範圍是1-255之間的整數,且在叢集中必須唯一。IP
:表示節點所在的IP地址,如在本地環境為127.0.0.1或localhost。Port1
:leader節點與follower節點進行心跳檢測與資料同步時所使用的埠。Port2
:在進行leader選舉的過程中,用於投票通訊的埠。
如果是偽叢集的配置方式,由於 ip 都是一樣,所以不同的 Zookeeper 例項通訊埠號不能一樣,要給它們分配不同的埠號。
在每個ZooKeeper檔案的/data
目錄下分別建立一個myid
檔案,myid檔案裡只需有伺服器編號(如1,2, 3)。
分別啟動三個ZooKeeper服務(開啟3個視窗來啟動服務)。
結果如下:
- zookeeper-3.4.14
[root@instance-e5cf5719 bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/zookeeper-3.4.14/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@instance-e5cf5719 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: follower
- zookeeper-3.4.14-1
[root@instance-e5cf5719 bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/zookeeper-3.4.14-1/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@instance-e5cf5719 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/zookeeper-3.4.14-1/bin/../conf/zoo.cfg
Mode: leader
- zookeeper-3.4.14-2
[root@instance-e5cf5719 bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/zookeeper-3.4.14-2/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@instance-e5cf5719 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/zookeeper-3.4.14-2/bin/../conf/zoo.cfg
Mode: follower
通過檢視狀態可以看到zookeeper-3.4.14-1是leader
,zookeeper-3.4.14和zookeeper-3.4.14-2是follower
。
可以參考官網的架構圖來輔助理解。
將zookeeper-3.4.14-1停掉,來觀察下leader的重新選舉。
[root@instance-e5cf5719 bin]# ./zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /usr/zookeeper-3.4.14-1/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
分別檢視zookeeper-3.4.14和zookeeper-3.4.14-2的狀態。
[root@instance-e5cf5719 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: follower
[root@instance-e5cf5719 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/zookeeper-3.4.14-2/bin/../conf/zoo.cfg
Mode: leader
可以看到zookeeper-3.4.14-2成為了leader。
3 叢集模式搭建
叢集模式搭建跟偽叢集非常相似,只是叢集的ZooKeeper是部署在不同的機器,偽叢集的ZooKeeper是部署在同一臺機器,在對/conf/zoo.cfg進行修改時,因為是不同的機器(ip不同),可以不用修改埠號。除了這一點差別外,其它的搭建方式跟偽叢集一模一樣,就不做多介紹了。
4 總結
至此我們完成ZooKeeper單機版、偽叢集和叢集環境的搭建。在生產環境上為了確保ZooKeeper的高可用,務必要搭建叢集環境