zookeeper學習與實戰(二)集羣部署

上一篇介紹了單機版zookeeper安裝,這種狀況通常用於開發測試。若是是生產環境建議用分佈式集羣部署,防止單點故障,增長zookeeper服務的高可用。html

【環境介紹】java

      三臺機器:192.168.126.160,192.168.126.161,192.168.126.162mysql

      操做系統:centos linuxlinux

以上就是本次集羣的機器狀況。下面進行安裝和配置:web

1、安裝和配置sql

1)解壓文件(先在一臺機器上操做數據庫

tar -zxvf zookeeper-3.4.6.tar.gz

2)重命名配置文件zoo_sample.cfg並修改其內容以下:apache

mv zoo_sample.cfg zoo.cfg
vim zoo.cn

內容爲:vim

tickTime=2000  
dataDir=/home/mysql/zookeeper-3.4.6/data
clientPort=2181  
initLimit=5  
syncLimit=2  
server.1=192.168.126.160:2888:3888  
server.2=192.168.126.161:2888:3888  
server.3=192.168.126.162:2888:3888  

tickTime:該參數用來定義心跳的間隔時間,zookeeper的客戶端和服務端之間也有和web開發裏相似的session的概念,而zookeeper裏最小的session過時時間就是tickTime的兩倍。單位是毫秒centos

dataDir:  存放內存數據庫的快照,用戶恢復數據,若是不指定logdatadir,那麼默認log也會存在這裏。

clientPort:供客戶端程序鏈接的端口,若是在單機部署多臺server,那麼端口須要不一樣。

server.x:用於集羣中發現彼此,這裏的1,2,3須要跟data/myid裏數字對應,後面會有說明。2888表示集羣中互聯的端口,3888用於選主的端口。

initLimit:參數設定了容許全部跟隨者與領導者進行鏈接並同步的時間,若是在設定的時間段內,半數以上的跟隨者未能完成同步,領導者便會宣佈放棄領導地位,進行另外一次的領導選舉。若是zk集羣環境數量確實很大,同步數據的時間會變長,所以這種狀況下能夠適當調大該參數。默認爲10
syncLimit:參數設定了容許一個跟隨者與一個領導者進行同步的時間,若是在設定的時間段內,跟隨者未完成同步,它將會被集羣丟棄。全部關聯到這個跟隨者的客戶端將鏈接到另一個跟隨着。

而後,把修改好的zookeeper安裝文件傳輸到其餘兩臺機器上:

 scp -r zookeeper-3.4.6 192.168.126.161:/home/

3)設置myid

在咱們配置的dataDir指定的目錄下面,建立一個myid文件,裏面內容爲一個數字,用來標識當前主機,conf/zoo.cfg文件中配置的server.X中X爲何數字,則myid文件中就輸入這個數字,例如:

192.168.126.160> cd /zookeeper-3.4.6/data && touch myid && echo "1" >myid
192.168.126.161> cd /zookeeper-3.4.6/data && touch myid && echo "2" >myid
192.168.126.162> cd /zookeeper-3.4.6/data && touch myid && echo "3" >myid

到這裏,整個集羣就配置好了。

 

2、啓動

192.168.126.160> cd /zookeeper-3.4.6/bin
192.168.126.160 bin> ./zkServer.sh start
JMX enabled by default
Using config: /home/mysql/zookeeper-3.4.6/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

192.168.126.161> cd /zookeeper-3.4.6/bin
192.168.126.161 bin> ./zkServer.sh start
JMX enabled by default
Using config: /home/mysql/zookeeper-3.4.6/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

192.168.126.162> cd /zookeeper-3.4.6/bin
192.168.126.162 bin> ./zkServer.sh start
JMX enabled by default
Using config: /home/mysql/zookeeper-3.4.6/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

安裝正確,以上三臺都啓動了,組成了一個zookeeper集羣,即便其中一臺down機了,其餘兩臺還能夠繼續提供服務。

3、命令

一、查看集羣機器狀態:

192.168.126.160>  ./zkServer.sh status
JMX enabled by default
Using config: /home/mysql/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: follower

192.168.126.161> ./zkServer.sh status
JMX enabled by default
Using config: /home/mysql/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: leader

192.168.126.162>  ./zkServer.sh status
JMX enabled by default
Using config: /home/mysql/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: follower

126.161機器是leader,其餘都是小弟角色。

二、經過客戶端腳本鏈接到集羣:

如今整個集羣對外是一個總體,鏈接到任意一臺機器上看到的視圖都是同樣的。

192.168.126.162> ./zkCli.sh -server 192.168.126.160:2181
Connecting to 192.168.126.160:2181
2016-11-25 16:40:47,036 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
2016-11-25 16:40:47,038 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=darren.org
2016-11-25 16:40:47,038 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.7.0_67
2016-11-25 16:40:47,039 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2016-11-25 16:40:47,040 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/usr/java/jdk1.7.0_67-cloudera/jre
2016-11-25 16:40:47,040 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/home/mysql/zookeeper-3.4.6/bin/../build/classes:/home/mysql/zookeeper-3.4.6/bin/../build/lib/*.jar:/home/mysql/zookeeper-3.4.6/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/mysql/zookeeper-3.4.6/bin/../lib/slf4j-api-1.6.1.jar:/home/mysql/zookeeper-3.4.6/bin/../lib/netty-3.7.0.Final.jar:/home/mysql/zookeeper-3.4.6/bin/../lib/log4j-1.2.16.jar:/home/mysql/zookeeper-3.4.6/bin/../lib/jline-0.9.94.jar:/home/mysql/zookeeper-3.4.6/bin/../zookeeper-3.4.6.jar:/home/mysql/zookeeper-3.4.6/bin/../src/java/lib/*.jar:/home/mysql/zookeeper-3.4.6/bin/../conf:.:/usr/java/jdk1.7.0_67-cloudera/lib/dt.jar:/usr/java/jdk1.7.0_67-cloudera/lib/tools.jar:/usr/java/jdk1.7.0_67-cloudera/jre/lib/rt.jar
2016-11-25 16:40:47,040 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2016-11-25 16:40:47,040 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2016-11-25 16:40:47,040 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>
2016-11-25 16:40:47,040 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Linux
2016-11-25 16:40:47,040 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=amd64
2016-11-25 16:40:47,040 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=2.6.32-504.23.4.el6.x86_64
2016-11-25 16:40:47,040 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=root
2016-11-25 16:40:47,040 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/root
2016-11-25 16:40:47,040 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/home/mysql/zookeeper-3.4.6/bin
2016-11-25 16:40:47,041 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=192.168.126.160:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@71fdf17c
Welcome to ZooKeeper!
2016-11-25 16:40:47,073 [myid:] - INFO  [main-SendThread(192.168.126.160:2181):ClientCnxn$SendThread@975] - Opening socket connection to server 192.168.126.160/192.168.126.160:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2016-11-25 16:40:47,076 [myid:] - INFO  [main-SendThread(192.168.126.160:2181):ClientCnxn$SendThread@852] - Socket connection established to 192.168.126.160/192.168
.126.160:2181, initiating session [zk: 192.168.126.160:2181(CONNECTING) 0] 2016-11-25 16:40:47,100 [myid:] - INFO [main-SendThread(192.168.126.160:2181):ClientCnxn$SendThread@1235] - Session establishment complete on server 192.168.126.160/192.168.126.160:2181, sessionid = 0x1589a9a25170000, negotiated timeout = 30000 WATCHER:: WatchedEvent state:SyncConnected type:None path:null [zk: 192.168.126.160:2181(CONNECTED) 0] 

查看數據:

[zk: 192.168.126.160:2181(CONNECTED) 0] ls /
[zookeeper]

用ls /命令查看目錄狀況,發現只有一個zookeeper目錄。

咱們到安裝目錄的/data文件夾下看看都有什麼文件?

-rw-r--r-- 1 root root        1 Nov 25 16:29 acceptedEpoch
-rw-r--r-- 1 root root        1 Nov 25 16:29 currentEpoch
-rw-r--r-- 1 root root 67108880 Nov 25 16:40 log.100000001
-rw-r--r-- 1 root root      296 Nov 25 16:29 snapshot.0

【解釋】:

snapshot.0:就是咱們存放數據在內存中的快照,zookeeper服務剛啓動時須要用它來恢復數據
log.100000001:日誌文件,默認存放在數據目錄中。

 

 

到此爲止,整個zookeeper集羣環境的搭建配置就完成了,挺簡單的吧。

 

 

 

【參考文獻】:

相關文章
相關標籤/搜索