RStudio Serverを専用AMIから一発で構築する

2016.07.14

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

小ネタです。

以前、Rを使ったデータ分析環境を構築するという下記エントリが投稿されていましたが、よりお手軽な形での導入が実現出来るようになっていましたので手順をご紹介してみたいと思います。

導入手順は至って簡単です。下記サイトにRStudio ServerのAMIが展開されていますので、こちらの情報を使って環境を作ってみたいと思います。赤枠線内のリージョン毎のAMI IDは後ほど使うので対象リージョンの情報を控えておきます。

install-rstudio-server_01

インスタンス構築の前に事前準備。IAM Roleを個別に準備しておき、構築するインスタンスに割り当てておきます。今回は検証用なのでPowerUserAccessで作っちゃいましたが、実環境構築の際は構築するインスタンスがアクセスする状況に応じた権限を割り当てておくようにしてください。

install-rstudio-server_03

セキュリティグループについても個別に用意。Webブラウザからのアクセスでポート:80を、またSSHアクセスでポート:22を利用可能な形にしておきます。(こちらについても、実環境構築の際は適宜必要なアクセス元制限を行うようにして下さい。) RStudioサーバー用にポート:8787等を開けておく必要があるかな、と思いましたが、こちらのAMIでは既にその辺り調整されておりポート:80でアクセス出来るようになっています。素敵。

install-rstudio-server_04

控えておいたAMI IDを使い、AMIを特定。OSはUbuntuになるようです。以降は必要事項を埋め、インスタンス起動まで持って行きます。

install-rstudio-server_02

インスタンス起動後、EIPを割り当てておいてhttp://EIPアドレス/でブラウザアクセス。予め定められているユーザー&パスワード(rstudio/rstudio)でサインインします。

install-rstudio-server_05

以下の様な形でサインイン出来ました。非常にシンプル且つスムーズに環境が整いました。

install-rstudio-server_06

ちなみにサーバにSSHログインする際はログインユーザーがubuntuとなります。

$ ssh -i rstudio-server-key.pem ubuntu@xx.xxx.xx.xxx
Welcome to Ubuntu 16.04 LTS (GNU/Linux 4.4.0-22-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.


Last login: Xxx Xxx XX xx:xx:xx 2016 from xx.xxx.xxx.xxx
$

上記手順は必要最低限の環境導入手順となりますが、RStudioでパッケージが必要となる場合は以下の情報等を参考にして状況を整える事が可能です。

また、RStudioサーバの起動停止等制御については以下に記載がありますのでご参考に。

$ sudo rstudio-server stop
$ sudo rstudio-server start
$ sudo rstudio-server restart
$ sudo rstudio-server status

 

追記:当AMIにはJupyterHubも導入済みだった

下記エントリでは当AMIを使った後の環境であったり、また個別にRStudioを導入した環境に対してJupyterHubを導入する試みをまとめていましたが、内容を確認するとRStudio Server起動の時点で既にJupyterHubが導入されていたようです。

導入直後のバージョン確認。確かに入っています。

$ jupyterhub --version
0.6.1

また、jupyterhubはserviceコマンドとしても組み込まれている様です。以下の形でステータスを確認する事が出来ました。

$ sudo service jupyterhub status
● jupyterhub.service - LSB: Start jupyterhub
   Loaded: loaded (/etc/init.d/jupyterhub; bad; vendor preset: enabled)
   Active: active (running) since Tue 2016-07-19 22:51:14 UTC; 7min ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1220 ExecStart=/etc/init.d/jupyterhub start (code=exited, status=0/SUCCESS)
    Tasks: 6 (limit: 512)
   Memory: 54.5M
      CPU: 573ms
   CGroup: /system.slice/jupyterhub.service
           ├─1254 /usr/bin/python3 /usr/local/bin/jupyterhub --config=/etc/jupyterhub/jupyterhub_config.py
           └─1334 node /usr/local/bin/configurable-http-proxy --ip 127.0.0.1 --port 8000 --api-ip 127.0.0.1 --api-port 8001 --default-ta

Jul 19 22:51:14 ip-10-0-0-43 systemd[1]: Starting LSB: Start jupyterhub...
Jul 19 22:51:14 ip-10-0-0-43 systemd[1]: Started LSB: Start jupyterhub.

OSユーザーにパスワードを設定してログインを試みてみますがアクセス出来ず。とその前に、この環境ではログファイルが出力されていない(出力箇所が不明)状況でしたので、下記情報を参考にログファイル出力設定を行いたいと思います。

設定ファイルは下記パスに配備されているようです。

$ ll /etc/jupyterhub/jupyterhub_config.py 
-rw-r--r-- 1 root root 98 May  8 22:35 /etc/jupyterhub/jupyterhub_config.py

設定ファイルに以下の内容を追記。併せてIP指定箇所(3行目)の部分もコメントアウトしておきます。

c.JupyterHub.confirm_no_ssl = True
c.JupyterHub.base_url = '/julia'
#c.JupyterHub.ip = '127.0.0.1' ## (コメントアウト)

## (以下内容を追記)
# Configuration file for jupyterhub.
# Tell Jupyterhub where to put the cookie secret and database file
c.JupyterHub.cookie_secret_file = '/srv/jupyterhub/cookie_secret'
c.JupyterHub.db_url = '/srv/jupyterhub/jupyterhub.sqlite'

# Set the Jupyterhub log file location
c.JupyterHub.extra_log_file = '/var/log/jupyterhub.log'

# Set the log level by value or name.
c.JupyterHub.log_level = 'DEBUG'

# Enable debug-logging of the single-user server
c.Spawner.debug = True

# Enable debug-logging of the single-user server
c.LocalProcessSpawner.debug = True

上記追記設定に含まれるフォルダを予め作成しておきます。

$ sudo mkdir /srv/jupyterhub/

また、以下の箇所も事前に所有者変更を行っておきます。

$ sudo chown -R ubuntu.ubuntu /home/ubuntu/.local/

JupyterHub再起動。ログファイルも出力されている事を確認出来ています。

$ sudo service jupyterhub restart
$ tail -f /var/log/jupyterhub.log 
[W 2016-07-19 23:09:25.593 JupyterHub app:757] No admin users, admin interface will be unavailable.
[W 2016-07-19 23:09:25.593 JupyterHub app:758] Add any administrative users to `c.Authenticator.admin_users` in config.
[I 2016-07-19 23:09:25.594 JupyterHub app:785] Not using whitelist. Any authenticated user will be allowed.
[D 2016-07-19 23:09:25.599 JupyterHub app:888] Loaded users: 
[I 2016-07-19 23:09:25.604 JupyterHub app:1231] Hub API listening on http://127.0.0.1:8081/julia/hub/
[W 2016-07-19 23:09:25.606 JupyterHub app:959] Running JupyterHub without SSL. There better be SSL termination happening somewhere else...
[I 2016-07-19 23:09:25.606 JupyterHub app:968] Starting proxy @ http://*:8000/
[D 2016-07-19 23:09:25.606 JupyterHub app:969] Proxy cmd: ['configurable-http-proxy', '--ip', '', '--port', '8000', '--api-ip', '127.0.0.1', '--api-port', '8001', '--default-target', 'http://127.0.0.1:8081', '--error-target', 'http://127.0.0.1:8081/julia/hub/error']
[D 2016-07-19 23:09:25.810 JupyterHub app:997] Proxy started and appears to be up
[I 2016-07-19 23:09:25.811 JupyterHub app:1254] JupyterHub is now running at http://127.0.0.1:8000/
[I 2016-07-19 23:10:05.489 JupyterHub log:100] 302 GET / (@::ffff:36.12.63.84) 1.14ms
[I 2016-07-19 23:10:05.581 JupyterHub log:100] 302 GET /julia/hub (@::ffff:36.12.63.84) 0.44ms
[I 2016-07-19 23:10:05.642 JupyterHub log:100] 302 GET /julia/hub/ (@::ffff:36.12.63.84) 0.75ms
[I 2016-07-19 23:10:05.946 JupyterHub log:100] 200 GET /julia/hub/login (@::ffff:36.12.63.84) 245.36ms
[D 2016-07-19 23:10:06.179 JupyterHub log:100] 200 GET /julia/hub/static/css/style.min.css?v=91c753d3c28f12b7480e5d0d9e7c55b2 (@::ffff:36.12.63.84) 5.00ms
[D 2016-07-19 23:10:06.220 JupyterHub log:100] 200 GET /julia/hub/static/components/requirejs/require.js?v=6da8be361b9ee26c5e721e76c6d4afce (@::ffff:36.12.63.84) 0.79ms
[D 2016-07-19 23:10:06.223 JupyterHub log:100] 200 GET /julia/hub/logo (@::ffff:36.12.63.84) 1.87ms
[D 2016-07-19 23:10:06.995 JupyterHub log:100] 200 GET /favicon.ico (@::ffff:36.12.63.84) 1.13ms

上記設定でJupyterHubの起動及びログインまでは確認出来ました。併せて以下の手順を踏まえる事で同等の環境を用意する事が可能となった事を書き記しておきます。

Python3系の導入

$ python --version
Python 2.7.11+
$ git --version
$ git clone https://github.com/yyuu/pyenv.git ~/.pyenv
$ vi ~/.bashrc
----
# 2016/07/xx Added
export PYENV_ROOT="${HOME}/.pyenv"
if [ -d "${PYENV_ROOT}" ]; then
export PATH=${PYENV_ROOT}/bin:$PATH
eval "$(pyenv init -)"
fi
----
$ source ~/.bashrc
$ pyenv install --list
$ pyenv install 3.5.2
$ pyenv global 3.5.2
$ pyenv rehash
$ python --version
Python 3.5.2
$ pyenv versions
  system
* 3.5.2 (set by /home/ubuntu/.pyenv/version)

npm最新パッケージの導入

$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.2/install.sh | bash
$ nvm --version
$ nvm install v6.2.2
$ node -v
v6.2.2
$ npm -v
3.9.5

R実行環境のJupyterHub連携

$ sudo apt-get -y install libzmq3 
$ sudo apt-get -y install libzmq3-dev
$ R
> install.packages(c('rzmq','repr','IRkernel','IRdisplay'), repos = c('http://irkernel.github.io/', getOption('repos')))
The downloaded source packages are in
	‘/tmp/Rtmpbo6Rh0/downloaded_packages’
> IRkernel::installspec()
[InstallKernelSpec] Installed kernelspec ir in /home/ubuntu/.local/share/jupyter/kernels/ir
> q()
Save workspace image? [y/n/c]: y

RStudioの起動ポート変更

上記手順ではポート:80で起動。必要に応じて変更可能。

$ sudo vi /etc/rstudio/rserver.conf
----
##www-address=127.0.0.1
www-port=8787
----
$ sudo service rstudio-server restart

まとめ

以上、RStudio Serverのカンタン環境構築方法のご紹介でした。このような形で直ぐに利用出来る環境が整えられているのは利用者にとっても嬉しいところですね。こちらからは以上です。

参考情報