LoginSignup
0
0

More than 5 years have passed since last update.

minishift V1.28.0でDjango2を動かす③:s2iソースの修正&ビルド

Last updated at Posted at 2018-12-29

はじめに

前回まででほぼminishiftでDjango2を動作させるところまで来ましたが
oenshiftのreadinessProbeおよびlivenessProbeに対応できておらず
Crash Loop Back-offになっていました。

そこでDjango2 Projectにhealthcheckを追加してPodを生成します。

openshiftのreadinessProbe、livenessProbeに対応させる

podのLogsを確認すると
readinessProbeおよびlivenessProbeはすべてGET / HTTP/1.1となっていました。
つまりなんでも良いので/でアクセスしたときに200 OKを返せればOKとなります。

そこで上で作ったProjectに/でのアクセスしたときにコンテンツを返すよう修正します。
本当はコンテンツ空っぽでstatus.HTTP_200_OKでもOKなんですが、一応REST APIらしくそれっぽいコンテンツを返すようにしました(笑)

sample/urls.py
"""sample URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from restapi import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', views.current_time, name='current_time'),
    path('', views.health, name='health'),         # ここを追加
]
restapi/views.py
from datetime import datetime
from django.shortcuts import render
from django.http import JsonResponse
from rest_framework import status
from rest_framework.decorators import api_view

# Create your views here.

@api_view(['GET'])
def current_time(request):
    ret = {'now':str(datetime.now())}
    return JsonResponse(ret, status=status.HTTP_200_OK)

def health(request):
    return JsonResponse({'health':'ok'}, status=status.HTTP_200_OK)
restapi/tests.py
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase

# Create your tests here.
class restapiTest(APITestCase):
    def test_current_time(self):
        url = reverse('current_time')
        response = self.client.get(url, '')

        self.assertEqual(response.status_code, status.HTTP_200_OK)


    def test_health(self):
        url = reverse('health')
        response = self.client.get(url, '')

        self.assertEqual(response.status_code, status.HTTP_200_OK)

Let's Build

それでは実際にBuildしてみます。
40.png
無事Buildに成功しPodが作成されました♪

Podのログを確認すると...いい感じ♪

---> 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 23:47:05 +0000] [1] [INFO] Starting gunicorn 19.9.0
[2018-12-28 23:47:05 +0000] [1] [INFO] Listening at: http://0.0.0.0:8080  (1)
[2018-12-28 23:47:05 +0000] [1] [INFO] Using worker: sync
[2018-12-28 23:47:05 +0000] [35] [INFO] Booting worker with pid: 35
[2018-12-28 23:47:05 +0000] [36] [INFO] Booting worker with pid: 36
[2018-12-28 23:47:05 +0000] [37] [INFO] Booting worker with pid: 37
[2018-12-28 23:47:05 +0000] [38] [INFO] Booting worker with pid: 38
172.17.0.1 - - [29/Dec/2018:08:47:12 +0900] "GET / HTTP/1.1" 200 16 "-" "kube-probe/1.11+"
172.17.0.1 - - [29/Dec/2018:08:47:22 +0900] "GET / HTTP/1.1" 200 16 "-" "kube-probe/1.11+"
172.17.0.1 - - [29/Dec/2018:08:47:30 +0900] "GET / HTTP/1.1" 200 16 "-" "kube-probe/1.11+"
172.17.0.1 - - [29/Dec/2018:08:47:32 +0900] "GET / HTTP/1.1" 200 16 "-" "kube-probe/1.11+"
172.17.0.1 - - [29/Dec/2018:08:47:40 +0900] "GET / HTTP/1.1" 200 16 "-" "kube-probe/1.11+"
172.17.0.1 - - [29/Dec/2018:08:47:42 +0900] "GET / HTTP/1.1" 200 16 "-" "kube-probe/1.11+" 

実際にルートhttp://django2-example-django2.192.168.42.246.nip.io/にアクセスしてみると

{"health": "ok"}

REST-APIのパスhttp://django2-example-django2.192.168.42.246.nip.io/api/にアクセスしてみると

{"now": "2018-12-29 09:12:58.410133"}

うまく行きました(^^♪

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0