ZooKeeper集群介紹
最典型集群模式: Master/Slave 模式(主備模式)。在這種模式中,通常 Master服務(wù)器作為主服務(wù)器提供寫(xiě)服務(wù),其他的 Slave 服務(wù)器從服務(wù)器通過(guò)異步復(fù)制的方式獲取 Master 服務(wù)器最新的數(shù)據(jù)提供讀服務(wù)。
但是,在 ZooKeeper 中沒(méi)有選擇傳統(tǒng)的 Master/Slave 概念,而是引入了Leader、Follower 和 Observer 三種角色。如下圖所示:

ZooKeeper 集群中的所有機(jī)器通過(guò)一個(gè) Leader 選舉過(guò)程來(lái)選定一臺(tái)稱(chēng)為 “Leader” 的機(jī)器,Leader 既可以為客戶(hù)端提供寫(xiě)服務(wù)又能提供讀服務(wù)。除了 Leader 外,F(xiàn)ollower 和 Observer 都只能提供讀服務(wù)。Follower 和 Observer 唯一的區(qū)別在于 Observer 機(jī)器不參與 Leader 的選舉過(guò)程,也不參與寫(xiě)操作的“過(guò)半寫(xiě)成功”策略,因此 Observer 機(jī)器可以在不影響寫(xiě)性能的情況下提升集群的讀性能。

ZooKeeper集群搭建
為了獲得可靠的 ZooKeeper 服務(wù),用戶(hù)應(yīng)該在一個(gè)集群上部署 ZooKeeper 。只要集群上大多數(shù)的ZooKeeper 服務(wù)啟動(dòng)了,那么總的 ZooKeeper 服務(wù)將是可用的。另外,最好使用奇數(shù)臺(tái)機(jī)器, 因?yàn)閦ookeeper集群是以宕機(jī)個(gè)數(shù)過(guò)半才會(huì)讓整個(gè)集群宕機(jī)的。 如果 zookeeper擁有 5 臺(tái)機(jī)器,那么它就能處理 2 臺(tái)機(jī)器的故障了。
【注意】 搭建zookeeper集群時(shí),一定要先停止已經(jīng)啟動(dòng)的zookeeper節(jié)點(diǎn)。
Step1:配置zookeeper
# 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=/etc/zookeeper/data
dataLogDir=/etc/zookeeper/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
minSessionTimeout=2
maxSessionTimeout=20
server.1=192.168.1.127:2888:3888
server.2=192.168.1.128:2888:3888
server.3=192.168.1.139:2888:3888
server.4=192.168.1.130:2888:3888:observer
server.5=192.168.1.131:2888:3888:observer
autopurge配置
客戶(hù)端在與zookeeper交互過(guò)程中會(huì)產(chǎn)生非常多的日志,而且zookeeper也會(huì)將內(nèi)存中的數(shù)據(jù)作為snapshot保存下來(lái),這些數(shù)據(jù)是不會(huì)被自動(dòng)刪除的,這樣磁盤(pán)中這樣的數(shù)據(jù)就會(huì)越來(lái)越多。不過(guò)可以通過(guò)這兩個(gè)參數(shù)來(lái)設(shè)置,讓zookeeper自動(dòng)刪除數(shù)據(jù)。
autopurge.purgeInterval:指定自動(dòng)清理快照文件和事務(wù)日志文件的時(shí)間,單位為h,默認(rèn)為0表示不自動(dòng)清理,這個(gè)時(shí)候可以使用腳本zkCleanup.sh手動(dòng)清理。如果不清理則磁盤(pán)空間占用越來(lái)越大。
autopurge.snapRetainCount:用于指定保留快照文件和事務(wù)日志文件的個(gè)數(shù),默認(rèn)為3。
不過(guò)如果你的集群是一個(gè)非常繁忙的集群,然后又碰上這個(gè)刪除操作,可能會(huì)影響zookeeper集群的性能,所以一般會(huì)讓這個(gè)過(guò)程在訪(fǎng)問(wèn)低谷的時(shí)候進(jìn)行,但是遺憾的是zookeeper并沒(méi)有設(shè)置在哪個(gè)時(shí)間點(diǎn)運(yùn)行的設(shè)置,所以有的時(shí)候我們會(huì)禁用這個(gè)自動(dòng)刪除的功能,而做一些其他手段,比如手動(dòng)或者自動(dòng)地在凌晨做清理工作。
Server.ID配置
server.id=IP/Host : port1 : port2
id:用來(lái)配置ZK集群中的各節(jié)點(diǎn),并建議id的值和myid保持一致,【注意】下文會(huì)講myid的配置。
IP/Host: 服務(wù)器的 IP 或者是與 IP 地址做了映射的主機(jī)名
port1:Leader和Follower或Observer交換數(shù)據(jù)使用
port2:用于Leader選舉
minSessionTimeout, maxSessionTimeout
一般,客戶(hù)端連接zookeeper的時(shí)候,都會(huì)設(shè)置一個(gè)session timeout,如果超過(guò)這個(gè)時(shí)間client沒(méi)有與zookeeper server有聯(lián)系,則這個(gè)session會(huì)被設(shè)置為過(guò)期(如果這個(gè)session上有臨時(shí)節(jié)點(diǎn),則會(huì)被全部刪除,這就是實(shí)現(xiàn)集群感知的基礎(chǔ))。但是這個(gè)時(shí)間不是客戶(hù)端可以無(wú)限制設(shè)置的,服務(wù)器可以設(shè)置這兩個(gè)參數(shù)來(lái)限制客戶(hù)端設(shè)置的范圍。
Step2: 創(chuàng)建myid文件
上文我們?cè)诿總€(gè)zookeeper服務(wù)器的配置文件里配置了這樣的信息:
server.1=192.168.1.127:2888:3888
server.2=192.168.1.128:2888:3888
server.3=192.168.1.139:2888:3888
server.4=192.168.1.130:2888:3888:observer
server.5=192.168.1.131:2888:3888:observer
server.id,這個(gè)id就是指zookeeper服務(wù)器在集群中的編號(hào),我們需要把這個(gè)id寫(xiě)入到myid文件中,這個(gè)myid值在zookeeper集群中的選舉過(guò)程中會(huì)做一個(gè)非常大的作用。比如在192.168.120.78這臺(tái)機(jī)器中首先創(chuàng)建myid文件(dataDir目錄,如上配置是/etc/zookeeper/data),然后執(zhí)行下列操作:
echo "1" > /etc/zookeeper/data/myid
Step3:?jiǎn)?dòng)zookeeper服務(wù),并查看狀態(tài)
首先查看192.168.1.126的zookeeper服務(wù)的狀態(tài):
root@ubuntu:/home/lizhiyong/source/zookeeper-3.4.12/bin# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/lizhiyong/source/zookeeper-3.4.12/bin/../conf/zoo.cfg
Mode: follower
查看192.168.1.128的zookeeper服務(wù)的狀態(tài)
root@ubuntu:/home/lee/source/zookeeper-3.4.12/bin# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/lee/source/zookeeper-3.4.12/bin/../conf/zoo.cfg
Mode: leader
查看192.168.1.130的zookeeper服務(wù)的狀態(tài)
root@ubuntu:/home/lizhiyong/source/zookeeper-3.4.12/bin# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/lizhiyong/source/zookeeper-3.4.12/bin/../conf/zoo.cfg
Mode: follower
Step4: 加入觀察者節(jié)點(diǎn)
具體可以參考:?https://zookeeper.apache.org/doc/r3.4.14/zookeeperObservers.html
觀察者節(jié)點(diǎn)為192.168.1.134,myid為4
第一步,在觀察者節(jié)點(diǎn)的配置文件中增加以下內(nèi)容:
peerType=observer
第二步,在其它的節(jié)點(diǎn)上修改配置添加
server.4=192.168.1.134:2888:3888:observer
查看狀態(tài):
root@ubuntu:/home/lizhiyong/source/zookeeper-3.4.12/bin# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/lizhiyong/source/zookeeper-3.4.12/bin/../conf/zoo.cfg
Mode: observer
視頻教程戳一下~