前言

  • ZooKeeper是一個分散式的,開放原始碼的分散式應用程式協調服務,目前很多架構都基於它來實現配置維護、域名服務、分散式同步、組服務等等。

  • ZooKeeper的基本運轉流程:
    1.選舉Leader
    2.同步資料
    3.選舉Leader過程中演算法有很多,但要達到的選舉標準是一致的
    4.Leader要具有最高的zxid
    5.叢集中大多數的機器得到響應並follow選出的Leader

環境說明

Zookeeper的部署主要是三種方式:單機模式、叢集模式、偽叢集模式,本文主要是偽叢集模式的搭建過程(叢集模式也類似)。

  • 系統環境:Ubuntu 14.04、JDK1.8
  • 所需工具:Zookeeper-3.4.9

下載zookeeper包

$ curl -O https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz

新建5個空目錄

$ mkdir -pv /zookeeper/server001
$ mkdir -pv /zookeeper/server002
$ mkdir -pv /zookeeper/server003
$ mkdir -pv /zookeeper/server004
$ mkdir -pv /zookeeper/server005

在每個目錄中分別建立data和logs資料夾

解壓下載的zookeeper包

$ tar zxvf zookeeper-3.4.9.tar.gz

修改配置檔案

Zookeeper 的配置檔案主要在conf目錄,包括zoo.cfg (zoo_sample.cfg)log4j.properties,修改 zoo_sample.cfg,重新命名為zoo.cfg,開啟zoo.cfg,將內容修改為如下:

tickTime=2000
initLimit=10
syncLimit=5

dataDir=/zookeeper/server001/data
dataLogDir=/zookeeper/server001/logs

clientPort=2181

server.1=127.0.0.1:8881:7771
server.2=127.0.0.1:8882:7772
server.3=127.0.0.1:8883:7773
server.4=127.0.0.1:8884:7774
server.5=127.0.0.1:8885:7775

引數說明:

tickTime:心跳時間,為了確保連線存在的,以毫秒為單位,最小超時時間為兩個心跳時間
initLimit:多少個心跳時間內,允許其他server連線並初始化資料,如果ZooKeeper管理的資料較大,則應相應增大這個值
clientPort:服務的監聽埠
dataDir:用於存放記憶體資料庫快照的資料夾,同時用於叢集的myid檔案也存在這個資料夾裡(注意:一個配置檔案只能包含一個dataDir字樣,即使它被註釋掉了。)
dataLogDir:用於單獨設定transaction log的目錄,transaction log分離可以避免和普通log還有快照的競爭
syncLimit:多少個tickTime內,允許follower同步,如果follower落後太多,則會被丟棄。

server.A=B:C:D
A是一個數字,表示這個是第幾號伺服器
B是這個伺服器的ip地址
C第一個埠用來叢集成員的資訊交換,表示的是這個伺服器與叢集中的Leader伺服器交換資訊的埠
D是在Leader掛掉時專門用來進行選舉Leader所用

將zookeeper資料夾拷貝到/zookeeper/server001/下,並在 /zookeeper/server001/data/下建立一個myid檔案,內容為1

$ echo 1 | sudo dd of=/zookeeper/server001/data/myid

配置偽叢集中的另外幾臺Zookeeper

繼續修改~/zookeeper/目錄中的zookeeper配置檔案檔案中的dataDir、dataLogDir、clientPort配置(server002的配置檔案,注意clientPort=2182,與 server001 中的clientPort=2181不同,後續修改配置均需設定不同的 clientPort),修改後內容如下:

tickTime=2000
initLimit=10
syncLimit=5

dataDir=/zookeeper/server002/data
dataLogDir=/zookeeper/server002/logs

clientPort=2182

server.1=127.0.0.1:8881:7771
server.2=127.0.0.1:8882:7772
server.3=127.0.0.1:8883:7773
server.4=127.0.0.1:8884:7774
server.5=127.0.0.1:8885:7775

然後將此 zookeeper 包拷貝至/tmp/zookeeper/server002/目錄下,並在/tmp/zookeeper/server002/data/下建立一個myid檔案,檔案內容為 2,

$ echo 2 | sudo dd of=/zookeeper/server002/data/myid

依次修改配置檔案,建立server003server004server005節點資料夾,完成上述步驟後/zookeeper目錄結構如下:

root@mesos-2:/zookeeper# tree -d -L 2
.
├── server001
│   ├── data
│   ├── logs
│   └── zookeeper-3.4.9
├── server002
│   ├── data
│   ├── logs
│   └── zookeeper-3.4.9
├── server003
│   ├── data
│   ├── logs
│   └── zookeeper-3.4.9
├── server004
│   ├── data
│   ├── logs
│   └── zookeeper-3.4.9
└── server005
    ├── data
    ├── logs
    └── zookeeper-3.4.9

啟動zookeeper服務

依次進入每個資料夾節點的zookeeper目錄中,執行命令

$ /zookeeper/server001/zookeeper-3.4.9/bin/zkServer.sh start
$ /zookeeper/server002/zookeeper-3.4.9/bin/zkServer.sh start
$ /zookeeper/server003/zookeeper-3.4.9/bin/zkServer.sh start
$ /zookeeper/server004/zookeeper-3.4.9/bin/zkServer.sh start
$ /zookeeper/server005/zookeeper-3.4.9/bin/zkServer.sh start

如果一切順利,Zookeeper 偽分散式模式安裝成功,下面驗證 Zookeeper 安裝的正確性。
進入任意一個資料夾節點的zookeeper包所在的目錄,執行一下命令:

$ bin/zkCli.sh  -server 127.0.0.1:2181

執行成功後:

Connecting to 127.0.0.1:2181
2016-12-28 10:51:43,473 [myid:] - INFO  [main:[email protected]100] - Client environment:zookeeper.version=3.4.9-1757313, built on 08/23/2016 06:50 GMT
2016-12-28 10:51:43,476 [myid:] - INFO  [main:[email protected]100] - Client environment:host.name=mesos-2
2016-12-28 10:51:43,476 [myid:] - INFO  [main:[email protected]100] - Client environment:java.version=1.7.0_121
2016-12-28 10:51:43,479 [myid:] - INFO  [main:[email protected]100] - Client environment:java.vendor=Oracle Corporation
2016-12-28 10:51:43,479 [myid:] - INFO  [main:[email protected]100] - Client environment:java.home=/usr/lib/jvm/java-7-openjdk-amd64/jre
2016-12-28 10:51:43,479 [myid:] - INFO  [main:[email protected]100] - Client environment:java.class.path=/zookeeper/server002/zookeeper-3.4.9/bin/../build/classes:/zookeeper/server002/zookeeper-3.4.9/bin/../build/lib/*.jar:/zookeeper/server002/zookeeper-3.4.9/bin/../lib/slf4j-log4j12-1.6.1.jar:/zookeeper/server002/zookeeper-3.4.9/bin/../lib/slf4j-api-1.6.1.jar:/zookeeper/server002/zookeeper-3.4.9/bin/../lib/netty-3.10.5.Final.jar:/zookeeper/server002/zookeeper-3.4.9/bin/../lib/log4j-1.2.16.jar:/zookeeper/server002/zookeeper-3.4.9/bin/../lib/jline-0.9.94.jar:/zookeeper/server002/zookeeper-3.4.9/bin/../zookeeper-3.4.9.jar:/zookeeper/server002/zookeeper-3.4.9/bin/../src/java/lib/*.jar:/zookeeper/server002/zookeeper-3.4.9/bin/../conf:
2016-12-28 10:51:43,480 [myid:] - INFO  [main:[email protected]100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib
2016-12-28 10:51:43,480 [myid:] - INFO  [main:[email protected]100] - Client environment:java.io.tmpdir=/tmp
2016-12-28 10:51:43,480 [myid:] - INFO  [main:[email protected]100] - Client environment:java.compiler=<NA>
2016-12-28 10:51:43,480 [myid:] - INFO  [main:[email protected]100] - Client environment:os.name=Linux
2016-12-28 10:51:43,480 [myid:] - INFO  [main:[email protected]100] - Client environment:os.arch=amd64
2016-12-28 10:51:43,480 [myid:] - INFO  [main:[email protected]100] - Client environment:os.version=4.4.0-31-generic
2016-12-28 10:51:43,480 [myid:] - INFO  [main:[email protected]100] - Client environment:user.name=root
2016-12-28 10:51:43,481 [myid:] - INFO  [main:[email protected]100] - Client environment:user.home=/root
2016-12-28 10:51:43,481 [myid:] - INFO  [main:[email protected]100] - Client environment:user.dir=/zookeeper/server002/zookeeper-3.4.9
2016-12-28 10:51:43,482 [myid:] - INFO  [main:[email protected]438] - Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=30000 [email protected]3eda834c
2016-12-28 10:51:43,513 [myid:] - INFO  [main-SendThread(127.0.0.1:2181):[email protected]1032] - Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
Welcome to ZooKeeper!
JLine support is enabled
2016-12-28 10:51:43,541 [myid:] - INFO  [main-SendThread(127.0.0.1:2181):[email protected]876] - Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
[zk: 127.0.0.1:2181(CONNECTING) 0] 2016-12-28 10:51:43,702 [myid:] - INFO  [main-SendThread(127.0.0.1:2181):[email protected]1299] - Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x159435690070000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null

help 幫助:

[zk: 127.0.0.1:2181(CONNECTED) 0] help
ZooKeeper -server host:port cmd args
    connect host:port
    get path [watch]
    ls path [watch]
    set path data [version]
    rmr path
    delquota [-n|-b] path
    quit
    printwatches on|off
    create [-s] [-e] path data acl
    stat path [watch]
    close
    ls2 path [watch]
    history
    listquota path
    setAcl path acl
    getAcl path
    sync path
    redo cmdno
    addauth scheme auth
    delete path [version]
    setquota -n|-b val path

也可在任意一個zookeeper目錄中執行如下命令檢視節點狀態:

$ bin/zkServer.sh status

輸出如下:

ZooKeeper JMX enabled by default
Using config: /zookeeper/server003/zookeeper-3.4.9/bin/../conf/zoo.cfg
Mode: leader

掃一掃,長按識別二維碼關注我
不定期分享技術架構、運維、技術管理等內容文章
這裡寫圖片描述

.