2018年6月18日月曜日

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

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

〇Jupyter Labの画面

仮想マシン構築後、ブラウザからhttp://192.168.55.104:8888/?token=jupyterにアクセスします。

仮想マシンの構築

以下のVagrantfileで、Jupyter LabとCockroachDBをインストールした仮想マシン(Ubuntu18.04)を構築する事ができます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-18.04"
  config.vm.hostname = "ub1804jupyterlabcockroachdb"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1804jupyterlabcockroachdb"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.104", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.104", :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 cockroachdb
wget https://binaries.cockroachdb.com/cockroach-v2.0.2.linux-amd64.tgz
tar xvfz cockroach-v2.0.2.linux-amd64.tgz
mv cockroach-v2.0.2.linux-amd64 /opt/cockroach

cat << EOF > /etc/systemd/system/cockroachdb.service
[Unit]
Description=cockroachdb
Requires=network.target
After=zookeepepr.service

[Service]
Type=forking
WorkingDirectory=/opt/cockroach
ExecStart=/opt/cockroach/cockroach start --insecure --host=192.168.55.104 --background
ExecStop=/opt/cockroach/cockroach quit

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

# execute sample sql statements
/opt/cockroach/cockroach user set maxroach --insecure --host=192.168.55.104
/opt/cockroach/cockroach sql --insecure --host=192.168.55.104 << EOF
CREATE DATABASE test;
CREATE TABLE test.messages (message_id INT PRIMARY KEY, message varchar(100));
INSERT INTO test.messages VALUES (1, 'hello world.');
select * from test.messages;
GRANT ALL ON DATABASE test TO maxroach;
GRANT ALL ON TABLE test.messages TO maxroach;
EOF

# 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 psycopg2
apt-get -y install libpq-dev python-dev
pip install psycopg2-binary

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 = 8888
conf.NotebookApp.token = 'jupyter'
EOF"

cat << EOF > /etc/systemd/system/jupyter.service
[Unit]
Description=Jupyter notebook
[Service]
Type=simple
EnvironmentFile=/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

echo 'cockroachdb -> http://192.168.55.104:8080'
echo 'jupyter -> http://192.168.55.104:8888/?token=jupyter'
SHELL
end

〇動作検証用コード
import psycopg2

con = psycopg2.connect("dbname=test host=192.168.55.104 user=maxroach port=26257")

try:
  cur= con.cursor()
  sql = "select message_id, message from messages"
  cur.execute(sql)

  for row in cur.fetchall():
    print(row)

except psycopg2.Error as er:
    print('psycopg2.Error:', er)
cur.close
con.close


関連情報

・CockroachDBに関する他の記事はこちらを参照してください。

・psycopg2に関する他の記事はこちらを参照してください。

0 件のコメント:

コメントを投稿