zookeeper 動かしてみたメモ


GettingStarted まとめ
http://zookeeper.apache.org/doc/trunk/zookeeperStarted.html

実際に動かしてみよう!
てことで

機能要件

要件を満たせているか?確認する.

centos_63:/home/itomaki 0
% java -version
java version "1.7.0_25"
OpenJDK Runtime Environment (rhel-2.3.10.4.el6_4-x86_64)
OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode)

スタンドアロンモード(Standalone Mode)

インストール

centos_63:/home/itomaki/pkgs 0
% wget http://ftp.meisei-u.ac.jp/mirror/apache/dist/zookeeper/stable/zookeeper-3.4.5.tar.gz

centos_63:/home/itomaki/pkgs 0
% ls
zookeeper-3.4.5.tar.gz

centos_63:/home/itomaki/pkgs/zookeeper-3.4.5 0
% tar zxvf zookeeper-3.4.5.tar.gz

centos_63:/home/itomaki/pkgs 0
% cd zookeeper-3.4.5

centos_63:/home/itomaki/pkgs/zookeeper-3.4.5 0
% ls
CHANGES.txt           bin         docs             src
LICENSE.txt           build.xml   ivy.xml          zookeeper-3.4.5.jar
NOTICE.txt            conf        ivysettings.xml  zookeeper-3.4.5.jar.asc
README.txt            contrib     lib              zookeeper-3.4.5.jar.md5
README_packaging.txt  dist-maven  recipes          zookeeper-3.4.5.jar.sha1

セットアップ

スタンドアロンモードでは以下を設定する.
スタンドアロンモードは実践的ではないけれども, 開発環境, テスト環境で一時的にスタンドアロンモードで zookeeper を起動することはあるだろう.

<zookeeper root dir>/conf/zoo.cfg へ設定を書く.

centos_63:/home/itomaki/pkgs/zookeeper-3.4.5 0
% cp conf/zoo_sample.cfg conf/zoo.cfg

centos_63:/home/itomaki/pkgs/zookeeper-3.4.5 0
% cat conf/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
#
# 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

起動する

centos_63:/home/itomaki/pkgs/zookeeper-3.4.5 0
% bin/zkServer.sh start
JMX enabled by default
Using config: /home/itomaki/pkgs/zookeeper-3.4.5/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

確認する

centos_63:/home/itomaki/pkgs/zookeeper-3.4.5 0
% ps xua | grep zookeeper
itomaki  23456  0.4  6.5 1157892 32172 pts/7   Sl   15:30   0:00 java -Dzookeeper.log.dir=. -Dzookeeper.root.logger=INFO,CONSOLE -cp /home/itomaki/pkgs/zookeeper-3.4.5/bin/../build/classes:/home/itomaki/pkgs/zookeeper-3.4.5/bin/../build/lib/*.jar:/home/itomaki/pkgs/zookeeper-3.4.5/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/itomaki/pkgs/zookeeper-3.4.5/bin/../lib/slf4j-api-1.6.1.jar:/home/itomaki/pkgs/zookeeper-3.4.5/bin/../lib/netty-3.2.2.Final.jar:/home/itomaki/pkgs/zookeeper-3.4.5/bin/../lib/log4j-1.2.15.jar:/home/itomaki/pkgs/zookeeper-3.4.5/bin/../lib/jline-0.9.94.jar:/home/itomaki/pkgs/zookeeper-3.4.5/bin/../zookeeper-3.4.5.jar:/home/itomaki/pkgs/zookeeper-3.4.5/bin/../src/java/lib/*.jar:/home/itomaki/pkgs/zookeeper-3.4.5/bin/../conf: -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=false org.apache.zookeeper.server.quorum.QuorumPeerMain /home/itomaki/pkgs/zookeeper-3.4.5/bin/../conf/zoo.cfg
itomaki  23493  0.0  0.1 107448   940 pts/7    S+   15:33   0:00 grep --color zookeeper

レプリケートモード(Replicated Mode)

実践的に zookeeper を利用するならば, やはりレプリケートモードで起動しなければ意味がない. レプリケートモードでは, 複数台のサーバを zookeeper として起動しておき, そのうちの 1 台が死んでも大丈夫なように, 任意の 1 台への更新作業を他の全台へ同期sてくれるんですな.

zookeeper ではレプリケート用サーバをグループとして扱い, これらのサーバをquorumと呼ぶ. 1つの quorum に含まれるサーバ上の zookeeper の設定は同じものにする!

準備

3 台のホストを用意

  • 192.168.11.16 zoo1.itomaki
  • 192.168.11.17 zoo2.itomaki
  • 192.168.11.18 zoo3.itomaki

すべてのホストへ zookeeper をインストールする.

centos_63:/home/itomaki/pkgs 0
% /usr/bin/scp  zookeeper-3.4.5.tar.gz zoo1.itomaki:/home/itomaki
centos_63:/home/itomaki/pkgs 0
% /usr/bin/scp  zookeeper-3.4.5.tar.gz zoo2.itomaki:/home/itomaki
centos_63:/home/itomaki/pkgs 0
% /usr/bin/scp  zookeeper-3.4.5.tar.gz zoo3.itomaki:/home/itomaki

centos_63:/home/itomaki/pkgs 0
% ssh zoo1.itomaki "tar zxvf zookeeper-3.4.5.tar.gz"
centos_63:/home/itomaki/pkgs 0
% ssh zoo2.itomaki "tar zxvf zookeeper-3.4.5.tar.gz"
centos_63:/home/itomaki/pkgs 0
% ssh zoo3.itomaki "tar zxvf zookeeper-3.4.5.tar.gz"

セットアップ

各ホストのセットアップをする. と言っても, 今回の例では, すべてのホストへ同じ設定ファイルを配布するだけだが...

各ホストに配布する設定ファイルは以下である.

# 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
#
# 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
server.1=zoo1.itomaki:2888:3888
server.2=zoo2.itomaki:2888:3888
server.3=zoo3.itomaki:2888:3888

設定ファイルの最後の 3 行で, レプリケートモードで利用するすべてのサーバを列挙している.
書式は以下

server.<id>=<hostname>:<port1>:<port2>

id は, 各サーバに割り振られる識別子である. 各サーバは, dataDir に設定されたパス上に myid ファイルを保持しており, このファイルに id が書かれている. 各サーバは myid ファイルに書かれた id を見ることで, 全体から見て自分がどれに相当するか?を認識している.

port1 は, ピアや follower から leader への通信に用いられるポートらしい(このあたりについては理解が浅い).
port2 は, リーダー選挙のためのポートらしい.

とにかく, このファイルを各ホストへ配布する.

centos_63:/home/itomaki/pkgs 0
% scp ~/zoo_sample.cfg zoo1.itomaki:/home/itomaki/zookeeper-3.4.5/conf/zoo.cfg
centos_63:/home/itomaki/pkgs 0
% scp ~/zoo_sample.cfg zoo2.itomaki:/home/itomaki/zookeeper-3.4.5/conf/zoo.cfg
centos_63:/home/itomaki/pkgs 0
% scp ~/zoo_sample.cfg zoo3.itomaki:/home/itomaki/zookeeper-3.4.5/conf/zoo.cfg

また, myid ファイルも作成する.

centos_63:/home/itomaki 0
% ssh zoo1.itomaki "echo 1 > /tmp/zookeeper/myid"
centos_63:/home/itomaki 0
% ssh zoo2.itomaki "echo 2 > /tmp/zookeeper/myid"
centos_63:/home/itomaki 0
% ssh zoo3.itomaki "echo 3 > /tmp/zookeeper/myid"

これで起動の準備は整った.
実際に起動してみよう.

centos_63:/home/itomaki 0
% ssh zoo1.itomaki "zookeeper-3.4.5/bin/zkServer.sh start"
JMX enabled by default
Using config: /home/itomaki/zookeeper-3.4.5/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
centos_63:/home/itomaki 0
% ssh zoo2.itomaki "zookeeper-3.4.5/bin/zkServer.sh start"
JMX enabled by default
Using config: /home/itomaki/zookeeper-3.4.5/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
centos_63:/home/itomaki 0
% ssh zoo3.itomaki "zookeeper-3.4.5/bin/zkServer.sh start"
JMX enabled by default
Using config: /home/itomaki/zookeeper-3.4.5/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

ちゃんと起動していることを確認する.

centos_63:/home/itomaki 0
% ssh zoo1.itomaki "ps aux | grep zookeeper"
itomaki   3035  0.9  6.6 1176296 33608 ?       Sl   16:39   0:02 java -Dzookeeper.log.dir=. -Dzookeeper.root.logger=INFO,CONSOLE -cp /home/itomaki/zookeeper-3.4.5/bin/../build/classes:/home/itomaki/zookeeper-3.4.5/bin/../build/lib/*.jar:/home/itomaki/zookeeper-3.4.5/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/itomaki/zookeeper-3.4.5/bin/../lib/slf4j-api-1.6.1.jar:/home/itomaki/zookeeper-3.4.5/bin/../lib/netty-3.2.2.Final.jar:/home/itomaki/zookeeper-3.4.5/bin/../lib/log4j-1.2.15.jar:/home/itomaki/zookeeper-3.4.5/bin/../lib/jline-0.9.94.jar:/home/itomaki/zookeeper-3.4.5/bin/../zookeeper-3.4.5.jar:/home/itomaki/zookeeper-3.4.5/bin/../src/java/lib/*.jar:/home/itomaki/zookeeper-3.4.5/bin/../conf: -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=false org.apache.zookeeper.server.quorum.QuorumPeerMain /home/itomaki/zookeeper-3.4.5/bin/../conf/zoo.cfg
itomaki   3064  0.0  0.2 106060  1488 ?        Ss   16:43   0:00 bash -c ps aux | grep zookeeper
itomaki   3070  0.0  0.1 107460   936 ?        S    16:43   0:00 grep zookeeper
centos_63:/home/itomaki 0
% ssh zoo2.itomaki "ps aux | grep zookeeper"
itomaki   2757  1.1  6.9 1182596 35100 ?       Sl   16:40   0:02 java -Dzookeeper.log.dir=. -Dzookeeper.root.logger=INFO,CONSOLE -cp /home/itomaki/zookeeper-3.4.5/bin/../build/classes:/home/itomaki/zookeeper-3.4.5/bin/../build/lib/*.jar:/home/itomaki/zookeeper-3.4.5/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/itomaki/zookeeper-3.4.5/bin/../lib/slf4j-api-1.6.1.jar:/home/itomaki/zookeeper-3.4.5/bin/../lib/netty-3.2.2.Final.jar:/home/itomaki/zookeeper-3.4.5/bin/../lib/log4j-1.2.15.jar:/home/itomaki/zookeeper-3.4.5/bin/../lib/jline-0.9.94.jar:/home/itomaki/zookeeper-3.4.5/bin/../zookeeper-3.4.5.jar:/home/itomaki/zookeeper-3.4.5/bin/../src/java/lib/*.jar:/home/itomaki/zookeeper-3.4.5/bin/../conf: -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=false org.apache.zookeeper.server.quorum.QuorumPeerMain /home/itomaki/zookeeper-3.4.5/bin/../conf/zoo.cfg
itomaki   2792  2.0  0.2 106060  1484 ?        Ss   16:43   0:00 bash -c ps aux | grep zookeeper
itomaki   2798  0.0  0.1 107460   936 ?        S    16:43   0:00 grep zookeeper
centos_63:/home/itomaki 0
% ssh zoo3.itomaki "ps aux | grep zookeeper"
itomaki   2073  1.2  6.6 1171944 33568 ?       Sl   16:41   0:01 java -Dzookeeper.log.dir=. -Dzookeeper.root.logger=INFO,CONSOLE -cp /home/itomaki/zookeeper-3.4.5/bin/../build/classes:/home/itomaki/zookeeper-3.4.5/bin/../build/lib/*.jar:/home/itomaki/zookeeper-3.4.5/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/itomaki/zookeeper-3.4.5/bin/../lib/slf4j-api-1.6.1.jar:/home/itomaki/zookeeper-3.4.5/bin/../lib/netty-3.2.2.Final.jar:/home/itomaki/zookeeper-3.4.5/bin/../lib/log4j-1.2.15.jar:/home/itomaki/zookeeper-3.4.5/bin/../lib/jline-0.9.94.jar:/home/itomaki/zookeeper-3.4.5/bin/../zookeeper-3.4.5.jar:/home/itomaki/zookeeper-3.4.5/bin/../src/java/lib/*.jar:/home/itomaki/zookeeper-3.4.5/bin/../conf: -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=false org.apache.zookeeper.server.quorum.QuorumPeerMain /home/itomaki/zookeeper-3.4.5/bin/../conf/zoo.cfg
itomaki   2105  0.0  0.2 106060  1488 ?        Ss   16:43   0:00 bash -c ps aux | grep zookeeper
itomaki   2111  0.0  0.1 107460   936 ?        S    16:43   0:00 grep zookeeper

起動できた!ちゃんとレプリケートされるかどうか?検証しておきたいところである.

検証

軽く検証してみよう. なんとなく, 以下の項目を見てみたい.

  • 任意の 1 台でデータを更新し, 他の 2 台から読み取る. このとき, 1 台への変更が反映されている.
  • どのサーバがフォロワーで, どのサーバがリーダか?を確認し, 以下を行なう.
  • フォロワー 1 台をシャットダウンする. シャットダウンしたサーバ以外が正常に動作する(データの更新, 読み取りが可能である)か?
  • リーダー 1 台をシャットダウンする. シャットダウンしたサーバ以外が正常に動作する(データの更新, 読み取りが可能である)か?
  • 任意の 1 台でデータを更新し, 他の 2 台から読み取る. このとき, 1 台への変更が何秒で反映されるか?

長くなりそうなんで、次