vdnih’s diary

技術的な記事を書きます。毎週日曜更新予定。

AWS無料枠でデータ分析勉強用環境を作成したい(part2)


作成者:仮想デニッシュ(@vdnih)

作成日:2020年1月26日

今回の趣旨

AWSやデータ分析の勉強のために、AWS無料枠を利用してデータ分析環境を作成します。
今回はpart2、EC2上にJupyterhub環境を構築します。
※EC2 Instance Connectの設定を行うつもりでしたが、次回にしたいと思います。

Jupyterhubとは

What is JupyterHub?

JupyterHub brings the power of notebooks to groups of users. It gives users access to computational environments and resources without burdening the users with installation and maintenance tasks. Users - including students, researchers, and data scientists - can get their work done in their own workspaces on shared resources which can be managed efficiently by system administrators.

公式ドキュメント

自分は英語はあまり得意ではないので自信がないですが…笑
簡単に言えば複数人で使えるノートブックです。
今回自分は複数人で作業可能な分析環境を作りたいと考えていたのでピッタリですね。

Anacondaのインストール

まずはAnacondaをインストールします。
この時、Anacondaを /opt/ 等、一般ユーザでもアクセス可能なディレクトリに
インストールする必要があるとのことです。

まずは前提パッケージをインストールします。

[root@ip-10-0-0-137 ~]# yum install libXcomposite libXcursor libXi libXtst libXrandr alsa-lib mesa-libEGL libXdamage mesa-libGL libXScrnSaver

次に、Anacondaをwgetで取得します。

[root@ip-10-0-0-137 ~]# wget https://repo.anaconda.com/archive/Anaconda3-2019.10-Linux-x86_64.sh

インストールします

[root@ip-10-0-0-137 ~]# bash Anaconda3-2019.10-Linux-x86_64.sh

~~~省略~~~

Anaconda3 will now be installed into this location:
/root/anaconda3

  - Press ENTER to confirm the location
  - Press CTRL-C to abort the installation
  - Or specify a different location below

[/root/anaconda3] >>> /opt/anaconda3
PREFIX=/opt/anaconda3

~~~省略~~~

確認します。

[root@ip-10-0-0-137 ~]# conda
-bash: conda: command not found

パスが通っていないですね。
bashrcを再読み込みすればロードが可能です。

[root@ip-10-0-0-137 ~]# source .bashrc
(base) [root@ip-10-0-0-137 ~]# conda -V
conda 4.7.12

Jupyterhubのインストール

公式のクイックスタート通りコマンドを打ち込んでいきます。

(base) [root@ip-10-0-0-137 ~]# conda install -c conda-forge jupyterhub  # installs jupyterhub and proxy
(base) [root@ip-10-0-0-137 ~]# conda install notebook  # needed if running the notebook servers locally

確認します。

(base) [root@ip-10-0-0-137 ~]# jupyterhub -V
1.1.0
(base) [root@ip-10-0-0-137 ~]# configurable-http-proxy -V
4.0.1

ではJupyterhubを起動します。

(base) [root@ip-10-0-0-137 ~]# jupyterhub
[I 2020-01-26 13:57:57.995 JupyterHub app:2240] Running JupyterHub version 1.1.0
[I 2020-01-26 13:57:57.996 JupyterHub app:2271] Using Authenticator: jupyterhub.auth.PAMAuthenticator-1.1.0
[I 2020-01-26 13:57:57.996 JupyterHub app:2271] Using Spawner: jupyterhub.spawner.LocalProcessSpawner-1.1.0
[I 2020-01-26 13:57:57.996 JupyterHub app:2271] Using Proxy: jupyterhub.proxy.ConfigurableHTTPProxy-1.1.0
[I 2020-01-26 13:57:58.009 JupyterHub app:1394] Writing cookie_secret to /root/jupyterhub_cookie_secret
[I 2020-01-26 13:57:58.039 alembic.runtime.migration migration:154] Context impl SQLiteImpl.
[I 2020-01-26 13:57:58.039 alembic.runtime.migration migration:161] Will assume non-transactional DDL.
[I 2020-01-26 13:57:58.046 alembic.runtime.migration migration:513] Running stamp_revision  -> 4dc2d5a8c53c
[I 2020-01-26 13:57:58.094 JupyterHub proxy:461] Generating new CONFIGPROXY_AUTH_TOKEN
[W 2020-01-26 13:57:58.095 JupyterHub app:1624] No admin users, admin interface will be unavailable.
[W 2020-01-26 13:57:58.095 JupyterHub app:1626] Add any administrative users to `c.Authenticator.admin_users` in config.
[I 2020-01-26 13:57:58.095 JupyterHub app:1655] Not using whitelist. Any authenticated user will be allowed.
10
[I 2020-01-26 13:57:58.135 JupyterHub app:2311] Initialized 0 spawners in 0.001 seconds
[W 2020-01-26 13:57:58.138 JupyterHub proxy:643] Running JupyterHub without SSL.  I hope there is SSL termination happening somewhere else...
[I 2020-01-26 13:57:58.138 JupyterHub proxy:646] Starting proxy @ http://:8000
13:57:58.414 [ConfigProxy] info: Proxying http://*:8000 to (no default)
13:57:58.419 [ConfigProxy] info: Proxy API at http://127.0.0.1:8001/api/routes
[I 2020-01-26 13:57:58.839 JupyterHub app:2556] Hub API listening on http://127.0.0.1:8081/hub/
13:57:58.841 [ConfigProxy] info: 200 GET /api/routes
13:57:58.842 [ConfigProxy] info: 200 GET /api/routes
[I 2020-01-26 13:57:58.884 JupyterHub proxy:320] Checking routes
[I 2020-01-26 13:57:58.884 JupyterHub proxy:400] Adding default route for Hub: / => http://127.0.0.1:8081
13:57:58.887 [ConfigProxy] info: Adding route / -> http://127.0.0.1:8081
[I 2020-01-26 13:57:58.888 JupyterHub app:2631] JupyterHub is now running at http://:8000
13:57:58.888 [ConfigProxy] info: 201 POST /api/routes/

いろいろメッセージ出てますが修正はそのうち行うことにして、ひとまず接続確認を行います。

Jupyterhubへのアクセス

セキュリティグループでアクセスを制限しているため、このままではアクセスできません。
セキュリティグループの8000ポートを解放します。

f:id:vdnih:20200126233122p:plain
securitygroup

あとは、EC2のアドレスの8000ポートへブラウザでログインします。
以下の画面が出てくれば成功です。

f:id:vdnih:20200126233034p:plain
jupyterhub-login
ひとまず今回はここまでとします。
次回は、実際に複数アカウントによるログインやpythonの実行を行いたいと思います。

参考

おわり

英語のドキュメントが多くて、英語が苦手な自分は大変な思いをしています。
英語も勉強しなくては…