2017年12月14日木曜日

VagrantでZooKeeperをインストールした仮想マシン(Ubuntu16.04)を構築する

以下のVagrantfileを使用して、ZooKeeperをインストールした仮想マシンを構築する事ができます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.hostname = "ub1604zookeeper"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1604zookeeper"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
  config.vm.network "private_network", ip: "192.168.55.105", :netmask => "255.255.255.0"
  config.vm.network "public_network", ip:"192.168.1.105", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
# update packages
apt-get update
#DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
locale-gen ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8

# install java
apt-get -y install openjdk-8-jdk

# download and install zookeeper
wget http://ftp.riken.jp/net/apache/zookeeper/zookeeper-3.4.11/zookeeper-3.4.11.tar.gz
tar xvfz zookeeper-3.4.11.tar.gz
mv zookeeper-3.4.11 /opt/zookeeper

# create configuration file.
cat << EOF > /opt/zookeeper/conf/zoo.cfg
tickTime=2000
dataDir=/var/zookeeper
clientPort=2181
EOF

# register as a service.
cat << EOF > /etc/systemd/system/zookeeper.service
[Unit]
Description=Apache Zookeeper
Requires=network.target
After=network.target

[Service]
Type=forking
ExecStart=/opt/zookeeper/bin/zkServer.sh start /opt/zookeeper/conf/zoo.cfg
ExecStop=/opt/zookeeper/bin/zkServer.sh stop
WorkingDirectory=/opt/zookeeper

[Install]
WantedBy=multi-user.target
EOF
systemctl enable zookeeper.service
systemctl start zookeeper.service

# 2181ポートでlistenするまで待つ.
while netstat -lnt | awk '$4 ~ /:2181$/ {exit 1}'; do sleep 10; done
sleep 10

# サンプル実行
/opt/zookeeper/bin/zkCli.sh -server localhost:2181 create /test mydata
/opt/zookeeper/bin/zkCli.sh -server localhost:2181 ls /
/opt/zookeeper/bin/zkCli.sh -server localhost:2181 get /test

SHELL
end

0 件のコメント:

コメントを投稿