How to do - ZooKeeper集羣搭建,一篇就夠

ZooKeeper集羣介紹


最典型集羣模式: Master/Slave 模式(主備模式)。在這種模式中,一般 Master服務器做爲主服務器提供寫服務,其餘的 Slave 服務器從服務器經過異步複製的方式獲取 Master 服務器最新的數據提供讀服務。html

可是,在 ZooKeeper 中沒有選擇傳統的 Master/Slave 概念,而是引入了Leader、Follower 和 Observer 三種角色。以下圖所示:apache

 

ZooKeeper 集羣中的全部機器經過一個 Leader 選舉過程來選定一臺稱爲 「Leader」 的機器,Leader 既能夠爲客戶端提供寫服務又能提供讀服務。除了 Leader 外,Follower 和 Observer 都只能提供讀服務。Follower 和 Observer 惟一的區別在於 Observer 機器不參與 Leader 的選舉過程,也不參與寫操做的「過半寫成功」策略,所以 Observer 機器能夠在不影響寫性能的狀況下提高集羣的讀性能。ubuntu

 

ZooKeeper集羣搭建服務器


爲了得到可靠的 ZooKeeper 服務,用戶應該在一個集羣上部署 ZooKeeper 。只要集羣上大多數的ZooKeeper 服務啓動了,那麼總的 ZooKeeper 服務將是可用的。另外,最好使用奇數臺機器, 由於zookeeper集羣是以宕機個數過半纔會讓整個集羣宕機的。 若是 zookeeper擁有 5 臺機器,那麼它就能處理 2 臺機器的故障了。session

【注意】 搭建zookeeper集羣時,必定要先中止已經啓動的zookeeper節點。異步

Step1:配置zookeeperide

# The number of milliseconds of each tick性能

tickTime=2000ui

# The number of ticks that the initialthis

# 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配置

客戶端在與zookeeper交互過程當中會產生很是多的日誌,並且zookeeper也會將內存中的數據做爲snapshot保存下來,這些數據是不會被自動刪除的,這樣磁盤中這樣的數據就會愈來愈多。不過能夠經過這兩個參數來設置,讓zookeeper自動刪除數據。

autopurge.purgeInterval:指定自動清理快照文件和事務日誌文件的時間,單位爲h,默認爲0表示不自動清理,這個時候可使用腳本zkCleanup.sh手動清理。若是不清理則磁盤空間佔用愈來愈大。

autopurge.snapRetainCount:用於指定保留快照文件和事務日誌文件的個數,默認爲3。

不過若是你的集羣是一個很是繁忙的集羣,而後又碰上這個刪除操做,可能會影響zookeeper集羣的性能,因此通常會讓這個過程在訪問低谷的時候進行,可是遺憾的是zookeeper並無設置在哪一個時間點運行的設置,因此有的時候咱們會禁用這個自動刪除的功能,而作一些其餘手段,好比手動或者自動地在凌晨作清理工做。

  • Server.ID配置

server.id=IP/Host : port1 : port2

id:用來配置ZK集羣中的各節點,並建議id的值和myid保持一致,【注意】下文會講myid的配置。

IP/Host: 服務器的 IP 或者是與 IP 地址作了映射的主機名

port1:Leader和Follower或Observer交換數據使用

port2:用於Leader選舉

  • minSessionTimeout, maxSessionTimeout

通常,客戶端鏈接zookeeper的時候,都會設置一個session timeout,若是超過這個時間client沒有與zookeeper server有聯繫,則這個session會被設置爲過時(若是這個session上有臨時節點,則會被所有刪除,這就是實現集羣感知的基礎)。可是這個時間不是客戶端能夠無限制設置的,服務器能夠設置這兩個參數來限制客戶端設置的範圍。

 

 

Step2: 建立myid文件

上文咱們在每一個zookeeper服務器的配置文件裏配置了這樣的信息:

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,這個id就是指zookeeper服務器在集羣中的編號,咱們須要把這個id寫入到myid文件中,這個myid值在zookeeper集羣中的選舉過程當中會作一個很是大的做用。好比在192.168.120.78這臺機器中首先建立myid文件(dataDir目錄,如上配置是/etc/zookeeper/data),而後執行下列操做:

echo "1" > /etc/zookeeper/data/myid

 

Step3:啓動zookeeper服務,並查看狀態

首先查看192.168.1.126的zookeeper服務的狀態:

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服務的狀態

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服務的狀態

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: 加入觀察者節點

具體能夠參考: https://zookeeper.apache.org/doc/r3.4.14/zookeeperObservers.html

觀察者節點爲192.168.1.134,myid爲4

 

第一步,在觀察者節點的配置文件中增長如下內容:

peerType=observer

 

第二步,在其它的節點上修改配置添加

server.4=192.168.1.134:2888:3888:observer

 

查看狀態:

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

點贊收藏轉發,私信一下資料能夠通通帶走~

 

相關文章
相關標籤/搜索