Skip to content

Instantly share code, notes, and snippets.

@eon01
Created March 26, 2020 23:38
Show Gist options
  • Save eon01/34e4ac7fb4c2751e35a0300435a10165 to your computer and use it in GitHub Desktop.
Save eon01/34e4ac7fb4c2751e35a0300435a10165 to your computer and use it in GitHub Desktop.
A simple view to create a /readiness for Kubernetes probes. Example based on connectivity with DB. It can be customized.
# views.py
# Example inspired by: https://www.ianlewis.org/en/kubernetes-health-checks-django
def readiness(request):
try:
from django.db import connections
for name in connections:
cursor = connections[name].cursor()
cursor.execute("SELECT 1;")
row = cursor.fetchone()
if row is None:
return HttpResponseServerError("db: invalid response")
except Exception as e:
logger.exception(e)
return HttpResponseServerError("db: cannot connect to database.")
response = HttpResponse()
response.status_code = 200
response.write("OK")
return response
# urls.py
from app.views import readiness
urlpatterns += [
url(r'^readiness/$', readiness, name = 'readiness'),
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment