2017年12月21日木曜日

VagrantでZooKeeperがインストールされた仮想マシン(Ubuntu16.04)の3ノードクラスタを構築する

以下のVagrantfileを使用して、ZooKeeperがインストールされた3ノードクラスタを構築する事ができます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.define "zknode1" do |server|
    server.vm.box = "bento/ubuntu-16.04"
    server.vm.hostname = "zknode1"
    server.vm.network :public_network, ip:"192.168.1.91"
    server.vm.network "private_network", ip: "192.168.55.91", :netmask => "255.255.255.0"
    server.vm.provider :virtualbox do |vbox|
      vbox.name = "zknode1"
      vbox.cpus = 2
      vbox.memory = 2048
      vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
    end
    server.vm.provision "shell", inline: <<-SHELL
# add host entries...
sed -i -e 's/^.*zknode1$//' /etc/hosts
cat << EOF >> /etc/hosts
192.168.55.91    zknode1
192.168.55.92    zknode2
192.168.55.93    zknode3
EOF

# 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
initLimit=5
syncLimit=2
server.1=zknode1:2888:3888
server.2=zknode2:2888:3888
server.3=zknode3:2888:3888
EOF

mkdir -p /var/zookeeper
echo '1' >> /var/zookeeper/myid

# 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 mydata1


SHELL
  end
  #----------------------------------------------------------------------------------------
  config.vm.define "zknode2" do |server|
    server.vm.box = "bento/ubuntu-16.04"
    server.vm.hostname = "zknode2"
    server.vm.network :public_network, ip:"192.168.1.92"
    server.vm.network "private_network", ip: "192.168.55.92", :netmask => "255.255.255.0"
    server.vm.provider :virtualbox do |vbox|
      vbox.name = "zknode2"
      vbox.cpus = 2
      vbox.memory = 2048
      vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
    end
    server.vm.provision "shell", inline: <<-SHELL
# add host entries...
sed -i -e 's/^.*zknode2$//' /etc/hosts
cat << EOF >> /etc/hosts
192.168.55.91    zknode1
192.168.55.92    zknode2
192.168.55.93    zknode3
EOF

# 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
initLimit=5
syncLimit=2
server.1=zknode1:2888:3888
server.2=zknode2:2888:3888
server.3=zknode3:2888:3888
EOF

mkdir -p /var/zookeeper
echo '2' >> /var/zookeeper/myid

# 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 mydata2


SHELL
  end
  #----------------------------------------------------------------------------------------
  config.vm.define "zknode3" do |server|
    server.vm.box = "bento/ubuntu-16.04"
    server.vm.hostname = "zknode3"
    server.vm.network :public_network, ip:"192.168.1.93"
    server.vm.network "private_network", ip: "192.168.55.93", :netmask => "255.255.255.0"
    server.vm.provider :virtualbox do |vbox|
      vbox.name = "zknode3"
      vbox.cpus = 2
      vbox.memory = 2048
      vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
    end
    server.vm.provision "shell", inline: <<-SHELL
# add host entries...
sed -i -e 's/^.*zknode3$//' /etc/hosts
cat << EOF >> /etc/hosts
192.168.55.91    zknode1
192.168.55.92    zknode2
192.168.55.93    zknode3
EOF


# 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
initLimit=5
syncLimit=2
server.1=zknode1:2888:3888
server.2=zknode2:2888:3888
server.3=zknode3:2888:3888
EOF

mkdir -p /var/zookeeper
echo '3' >> /var/zookeeper/myid

# 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
  #----------------------------------------------------------------------------------------

end

0 件のコメント:

コメントを投稿