はじめに
先の記事「minishift V1.28.0でDjango2を動かす①」で観察した結果と
「Win10環境でDjango2のREST APIを試してみる」で作成したプロジェクトを元に
s2iのイメージを作成していきます。
- Win10環境でDjango2のREST APIを試してみる
- minishift V1.28.0でDjango2を動かす①:テンプレート確認
- minishift V1.28.0でDjango2を動かす②:テンプレート&s2iソースの作成
- minishift V1.28.0でDjango2を動かす③:s2iソースの修正&ビルド
- minishift V1.28.0でDjango2を動かす④:残課題
- minishift V1.28.0でnginx+gunicorn+Django2を動かす
- minishift V1.28.0でnginx+gunicorn+Django2を動かす。あっcreatesuperuser忘れてた(笑)
- openshiftに対応するためのDjango2コーディング時に気を付けるべき点
openshift/django-ex.gitのソースを確認
まずはお手本となる
Sample Repository: https://github.com/openshift/django-ex.git
をGit Cloneしてローカルに持ち出します。
一部省略してますが、大体こんなイメージの構成でした。
├─conf
├─openshift
│ ├─scripts
│ └─templates
├─project
│ ├─__init__.py
│ ├─database.py
│ ├─settings.py
│ └─urls.py
├─welcome
│ ├─migrations
│ └─templates
│ └─welcome
├─manage.py
├─requirements.txt
└─wsgi.py
confの中身はcommit時のコメントをみるとGunicorn用設定なのでそのまま
openshift内はopenshiftのテンプレート作成時に使うjsonなのでそのまま
projectディレクトリ内にDjangoのソースとかが入っていて
welcomeとかはstaticなテンプレートが入っている感じでした。
Django2のs2iなgitの準備
よってproject、welcomeはバッサリ削除して、
「Win10環境でDjango2のREST APIを試してみる」で作成したDjango2プロジェクトを持ってきて
微調整でOKそうな感じ
├─conf
├─openshift
│ ├─scripts
│ └─templates
├─restapi
│ ├─migrations
│ ├─__init__.py
│ ├─admin.py
│ ├─apps.py
│ ├─models.py
│ ├─tests.py
│ └─views.py
├─sample
│ ├─__init__.py
│ ├─settings.py
│ ├─urls.py
│ └─wsgi.py
├─manage.py
└─requirements.txt
おっとwsgi.pyがrootにないですね。sampleの下からrootにもCopyしときます。
├─conf
├─openshift
│ ├─scripts
│ └─templates
├─restapi
│ ├─migrations
│ ├─__init__.py
│ ├─admin.py
│ ├─apps.py
│ ├─models.py
│ ├─tests.py
│ └─views.py
├─sample
│ ├─__init__.py
│ ├─settings.py
│ ├─urls.py
│ └─wsgi.py
├─manage.py
├─requirements.txt
└─wsgi.py ※sample配下のをCopy
requirements.txtはシンプルに以下のような構成としました。
django==2.1.4
djangorestframework
django-filter
gunicorn
これをGitLabにPushします。
minishiftでDjango2にチャレンジ
console画面で、django2プロジェクトを作成してImport YAML/JSON
をクリック
表示されたテキストエリアにopenshift\templates\django.json
の内容を貼り付けてCreateボタンをクリック
実行するだけか登録もするか聞かれるので、今回は実行のみとします。
変数 | 初期値 | 設定値 |
---|---|---|
Name | django-example | sample |
Git Repository URL | https://github.com/sclorg/django-ex.git | https://gitlab.com/imp555/django2.git |
template | django-example | django2-example |
app | django-example | django2-example |
でCreate
PodのBuildの様子を探る
Builds > Builds から Last Buildの#1をクリックし、Logsタブをクリックします。
Cloning "https://gitlab.com/imp555/django2.git " ...
Commit: 0cef8fa6301a3308127066fa5b6198781ff0fa2a (update README.md)
Author: imp555 <imp555@nethome.ne.jp>
Date: Sat Dec 29 00:19:30 2018 +0900
Using 172.30.1.1:5000/openshift/python@sha256:091d56e3ab03d52ef0ffac4b88e7e1fa24ea0243bfd05297882c12ff8a0ba1df as the s2i builder image
---> Installing application source ...
---> Installing dependencies ...
Collecting django==2.1.4 (from -r requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/fd/9a/0c028ea0fe4f5803dda1a7afabeed958d0c8b79b0fe762ffbf728db3b90d/Django-2.1.4-py3-none-any.whl (7.3MB)
Collecting djangorestframework (from -r requirements.txt (line 2))
Downloading https://files.pythonhosted.org/packages/99/0b/d37a5a96c5d301e23adcabcc2f3fa659fb34e6308590f95ebb50cdbe98a1/djangorestframework-3.9.0-py2.py3-none-any.whl (924kB)
Collecting django-filter (from -r requirements.txt (line 3))
Downloading https://files.pythonhosted.org/packages/6a/8b/8517167a0adc45ce94d0873efb9487dd4cdeff7e10f96e837ad3d58f5837/django_filter-2.0.0-py3-none-any.whl (69kB)
Collecting gunicorn (from -r requirements.txt (line 4))
Downloading https://files.pythonhosted.org/packages/8c/da/b8dd8deb741bff556db53902d4706774c8e1e67265f69528c14c003644e6/gunicorn-19.9.0-py2.py3-none-any.whl (112kB)
Collecting pytz (from django==2.1.4->-r requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/f8/0e/2365ddc010afb3d79147f1dd544e5ee24bf4ece58ab99b16fbb465ce6dc0/pytz-2018.7-py2.py3-none-any.whl (506kB)
Installing collected packages: pytz, django, djangorestframework, django-filter, gunicorn
Successfully installed django-2.1.4 django-filter-2.0.0 djangorestframework-3.9.0 gunicorn-19.9.0 pytz-2018.7
You are using pip version 9.0.1, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
---> Collecting Django static files ...
WARNING: could not run 'manage.py collectstatic'. To debug, run:
$ python ./manage.py collectstatic --noinput
Ignore this warning if you're not serving static files with Django.
Running post commit hook ...
sh: cannot set terminal process group (-1): Inappropriate ioctl for device
sh: no job control in this shell
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.
----------------------------------------------------------------------
Ran 1 test in 0.188s
OK
Destroying test database for alias 'default'...
Pushing image 172.30.1.1:5000/django2/sample:latest ...
Pushed 0/10 layers, 1% complete
Pushed 1/10 layers, 10% complete
Push successful
Applications > Pods から 稼働中のPodをクリックし、Logsタブをクリックします。
---> Migrating database ...
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying sessions.0001_initial... OK
---> Serving application with gunicorn (wsgi) ...
[2018-12-28 15:27:15 +0000] [1] [INFO] Starting gunicorn 19.9.0
[2018-12-28 15:27:15 +0000] [1] [INFO] Listening at: http://0.0.0.0:8080 (1)
[2018-12-28 15:27:15 +0000] [1] [INFO] Using worker: sync
[2018-12-28 15:27:15 +0000] [35] [INFO] Booting worker with pid: 35
[2018-12-28 15:27:15 +0000] [36] [INFO] Booting worker with pid: 36
[2018-12-28 15:27:15 +0000] [37] [INFO] Booting worker with pid: 37
[2018-12-28 15:27:15 +0000] [38] [INFO] Booting worker with pid: 38
172.17.0.1 - - [29/Dec/2018:00:27:21 +0900] "GET / HTTP/1.1" 404 2048 "-" "kube-probe/1.11+" Not Found: /
172.17.0.1 - - [29/Dec/2018:00:27:31 +0900] "GET / HTTP/1.1" 404 2048 "-" "kube-probe/1.11+"
Not Found: /
Not Found: /
172.17.0.1 - - [29/Dec/2018:00:27:41 +0900] "GET / HTTP/1.1" 404 2048 "-" "kube-probe/1.11+"
Not Found: /
172.17.0.1 - - [29/Dec/2018:00:27:49 +0900] "GET / HTTP/1.1" 404 2048 "-" "kube-probe/1.11+"
Not Found: /
172.17.0.1 - - [29/Dec/2018:00:27:51 +0900] "GET / HTTP/1.1" 404 2048 "-" "kube-probe/1.11+"
172.17.0.1 - - [29/Dec/2018:00:27:59 +0900] "GET / HTTP/1.1" 404 2048 "-" "kube-probe/1.11+"
Not Found: /
172.17.0.1 - - [29/Dec/2018:00:28:01 +0900] "GET / HTTP/1.1" 404 2048 "-" "kube-probe/1.11+"
Not Found: /
172.17.0.1 - - [29/Dec/2018:00:28:09 +0900] "GET / HTTP/1.1" 404 2048 "-" "kube-probe/1.11+"
Not Found: /
[2018-12-28 15:28:09 +0000] [1] [INFO] Handling signal: term
[2018-12-29 00:28:09 +0900] [37] [INFO] Worker exiting (pid: 37)
[2018-12-29 00:28:09 +0900] [35] [INFO] Worker exiting (pid: 35)
[2018-12-29 00:28:09 +0900] [38] [INFO] Worker exiting (pid: 38)
[2018-12-29 00:28:09 +0900] [36] [INFO] Worker exiting (pid: 36)
[2018-12-28 15:28:11 +0000] [1] [INFO] Shutting down: Master
どうやらヘルスチェックとして/
にアクセスしているようですね…。
Crash Loop Back-off(´・ω・`)
Applications > Pods から 稼働中のPodをクリックし、Eventsタブをクリックします。
この/
に対し200 OKを返さないとreadinessProbeかlivenessProbeのチェックにかかって
Unhealthyになっているようですね(´・ω・`)