2018年4月26日木曜日

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

Jupyter Labでインタラクティブなコンピューティング環境を提供する事ができます。

〇Jupyter Labの画面


〇仮想マシンの構築
以下のVagrantfileで、Jupyter LabとApache ZooKeeperをインストールした仮想マシンを構築する事ができます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.hostname = "ub1604jupyterlabzk"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1604jupyterlabzk"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.101", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.101", :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


# install anaconda & jupyterlab
wget https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh
chmod +x Anaconda3-5.1.0-Linux-x86_64.sh
./Anaconda3-5.1.0-Linux-x86_64.sh -b -p /opt/anaconda
source /opt/anaconda/bin/activate
pip install --upgrade pip
pip install jupyterlab


# install kazoo
pip install kazoo

useradd py
mkdir -p /home/py
chown -R py:py /home/py
sudo -u py bash -c "mkdir /home/py/.jupyter"
sudo -u py bash -c "cat << EOF > /home/py/.jupyter/jupyter_notebook_config.py
conf = get_config()
conf.NotebookApp.ip = '*'
conf.NotebookApp.open_browser = False
conf.NotebookApp.port = 8080
conf.NotebookApp.token = 'jupyter'
EOF"

cat << EOF > /etc/systemd/system/jupyter.service
[Unit]
Description=Jupyter notebook
[Service]
Type=simple
ExecStartPre=source /opt/anaconda/bin/activate
ExecStart=/opt/anaconda/bin/jupyter lab
User=py
Group=py
WorkingDirectory=/home/py
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl enable jupyter
sudo systemctl start jupyter

# 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

echo 'access -> http://192.168.55.101:8080/?token=jupyter'

SHELL
end

〇動作確認用コード
from kazoo.client import KazooClient

zk = KazooClient(hosts='127.0.0.1:2181', read_only=True)
zk.start()
data, stat = zk.get("/test")
print("Version: " + str(stat.version))
print("Data: " + data.decode("utf-8"))

0 件のコメント:

コメントを投稿