ZooKeeperを複数台の冗長(Replicate)モードでインストール＆起動する設定メモ＠AWS

参考サイト
http://qiita.com/soushiy/items/f4d2d91d7ac79669e0dd
http://jubat.us/ja/tutorial_distributed.html
http://www.atmarkit.co.jp/ait/articles/1206/22/news142_3.html
https://zookeeper.apache.org/

## zookeeperセットアップ（冗長化モード）
#事前準備
AWSでCentOS7のAMIでクラスタを３台起動。
※CentOS7はAmazonMarketplaceから

```
sudo su
yum -y update
yum -y install java-1.8.0-openjdk-devel
yum -y install wget
mkdir /usr/local/downloadcd /usr/local/download
```

#zookeeperダウンロード＆インストール
```
wget http://ftp.kddilabs.jp/infosystems/apache/zookeeper/zookeeper-3.4.8/zookeeper-3.4.8.tar.gz
tar xvf  zookeeper-3.4.8.tar.gz 
mv -f /usr/local/download/zookeeper-3.4.8 /usr/local/download/zookeeper;
cp -r -f /usr/local/download/zookeeper /usr/local/;
rm -r -f /usr/local/download/zookeeper;
```

#パスの設定
```
echo "export PATH=\$PATH:/usr/local/zookeeper/bin" >> /etc/profile;
export PATH=$PATH:/usr/local/zookeeper/bin
```

#zookeeperのconfigファイル設定
```
cd /usr/local/zookeeper/conf/
cp -p zoo_sample.cfg zoo.cfg
sed -i -e 's/^dataDir/#dataDir/g' /usr/local/zookeeper/conf/zoo.cfg;
sed -i -e '/^#dataDir/a dataDir=\/usr\/local\/zookeeper\/data' /usr/local/zookeeper/conf/zoo.cfg;
sed -i -e 's/^clientPort/#clientPort/g'
sed -i -e 's/^clientPort/#clientPort/g' /usr/local/zookeeper/conf/zoo.cfg;
sed -i -e '/^#clientPort/a clientPort=2181' /usr/local/zookeeper/conf/zoo.cfg;
```

```
vi /usr/local/zookeeper/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
dataDir=/usr/local/zookeeper/data
# the port at which the clients will connect
#clientPort=2181
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
server.1=192.168.0.10:2888:3888
server.2=192.168.0.11:2888:3888
server.3=192.168.0.12:2888:3888
==============
```

```
vi /usr/local/zookeeper/data/myid 
==============
1
==============
```
↑zoo.cfgのserver.XのXをクラスタ毎に記載

#ログ出力ファイル修正
```
cd /usr/local/zookeeper/
cp log4j.properties log4j.properties.bak# zookeeper.root.logger=INFO, CONSOLE

sed -i -e 's/^zookeeper\.root\.logger/#zookeeper\.root\.logger/g' /usr/local/zookeeper/conf/log4j.properties;
sed -i -e '/^#zookeeper\.root\.logger/a zookeeper\.root\.logger=INFO, CONSOLE' /usr/local/zookeeper/conf/log4j.properties;
# zookeeper.console.threshold=INFO
sed -i -e 's/^zookeeper\.console\.threshold/#zookeeper\.console\.threshold/g' /usr/local/zookeeper/conf/log4j.properties;
sed -i -e '/^#zookeeper\.console\.threshold/a zookeeper\.console\.threshold=INFO' /usr/local/zookeeper/conf/log4j.properties;
# zookeeper.log.dir=/usr/local/zookeeper/log/
sed -i -e 's/^zookeeper\.log\.dir/#zookeeper\.log\.dir/g' /usr/local/zookeeper/conf/log4j.properties;
sed -i -e '/^#zookeeper\.log\.dir/a zookeeper\.log\.dir=\/usr\/local\/zookeeper\/log\/' /usr/local/zookeeper/conf/log4j.properties;
# zookeeper.log.file=zookeeper.log
sed -i -e 's/^zookeeper\.log\.file/#zookeeper\.log\.file/g' /usr/local/zookeeper/conf/log4j.properties;
sed -i -e '/^#zookeeper\.log\.file/a zookeeper\.log\.file=zookeeper.log' /usr/local/zookeeper/conf/log4j.properties;
# zookeeper.log.threshold=DEBUG
sed -i -e 's/^zookeeper\.log\.threshold/#zookeeper\.log\.threshold/g' /usr/local/zookeeper/conf/log4j.properties;
sed -i -e '/^#zookeeper\.log\.threshold/a zookeeper\.log\.threshold=DEBUG' /usr/local/zookeeper/conf/log4j.properties;
# zookeeper.tracelog.dir=/usr/local/zookeeper/log/
sed -i -e 's/^zookeeper\.tracelog\.dir/#zookeeper\.tracelog\.dir/g' /usr/local/zookeeper/conf/log4j.properties;
sed -i -e '/^#zookeeper\.tracelog\.dir/a zookeeper\.tracelog\.dir=\/usr\/local\/zookeeper\/log\/' /usr/local/zookeeper/conf/log4j.properties;
# zookeeper.tracelog.file=zookeeper_trace.log
sed -i -e 's/^zookeeper\.tracelog\.file/#zookeeper\.tracelog\.file/g' /usr/local/zookeeper/conf/log4j.properties;
sed -i -e '/^#zookeeper\.tracelog\.file/a zookeeper\.tracelog\.file=zookeeper_trace.log' /usr/local/zookeeper/conf/log4j.properties;
# DEFAULT: console appender only#log4j.rootLogger=${zookeeper.root.logger}
sed -i -e 's/^log4j\.rootLogger/#log4j\.rootLogger/g' /usr/local/zookeeper/conf/log4j.properties;
# Rolling log file
sed -i -e 's/^#log4j\.rootLogger=DEBUG, CONSOLE, ROLLINGFILE/log4j\.rootLogger=DEBUG, CONSOLE, ROLLINGFILE/' /usr/local/zookeeper/conf/log4j.properties;
```

#データ＆ログディレクトリ作成
```
mkdir /usr/local/zookeeper/data/
mkdir /usr/local/zookeeper/log/
```

#zookeeper起動
```
bin/zkServer.sh start
```

#動作確認
```
yum -y install nc
echo ruok | nc localhost 2181
```
※imokという応答があれば正常起動

#クラスタ間の同期の確認
```
bin/zkCli.sh -server localhost:2181
create /test dummy
ls /
```
※他のクラスタでtestが生成されていれば同期されている