Skip to content
This repository was archived by the owner on Mar 27, 2023. It is now read-only.

Commit 0b133fc

Browse files
committed
added healthcheck for redis
1 parent 78ddaa7 commit 0b133fc

File tree

10 files changed

+85
-23
lines changed

10 files changed

+85
-23
lines changed

backend/backend/settings/base.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@
9999
INSTALLED_APPS = DJANGO_APPS + PROJECT_APPS + THIRD_PARTY_APPS
100100

101101
MIDDLEWARE = [
102-
'corsheaders.middleware.CorsMiddleware',
103102
'django.middleware.security.SecurityMiddleware',
104103
'django.contrib.sessions.middleware.SessionMiddleware',
105104
'social_django.middleware.SocialAuthExceptionMiddleware',
@@ -108,7 +107,6 @@
108107
'django.contrib.auth.middleware.AuthenticationMiddleware',
109108
'django.contrib.messages.middleware.MessageMiddleware',
110109
'django.middleware.clickjacking.XFrameOptionsMiddleware',
111-
'core.middleware.SubdomainMiddleware',
112110
]
113111

114112
ROOT_URLCONF = 'backend.urls'

backend/backend/settings/development.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
INSTALLED_APPS += DEBUG_APPS # noqa
2222

2323
MIDDLEWARE = [
24+
'core.middleware.healthchecks.HealthCheckMiddleware',
2425
'debug_toolbar.middleware.DebugToolbarMiddleware',
2526
] + MIDDLEWARE # noqa
2627

backend/core/middleware.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

backend/core/middleware/__init__.py

Whitespace-only changes.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
https://www.ianlewis.org/en/kubernetes-health-checks-django
3+
"""
4+
import logging
5+
import os
6+
7+
import redis
8+
from django.http import HttpResponse, HttpResponseServerError
9+
10+
logger = logging.getLogger("django")
11+
12+
r = redis.Redis(
13+
host=os.environ.get('REDIS_SERVICE_HOST')
14+
)
15+
16+
17+
class HealthCheckMiddleware(object):
18+
def __init__(self, get_response):
19+
self.get_response = get_response
20+
# One-time configuration and initialization.
21+
22+
def __call__(self, request):
23+
if request.method == "GET":
24+
if request.path == "/readiness":
25+
return self.readiness(request)
26+
elif request.path == "/healthz":
27+
return self.healthz(request)
28+
return self.get_response(request)
29+
30+
def healthz(self, request):
31+
"""
32+
Returns that the server is alive.
33+
"""
34+
return HttpResponse("OK")
35+
36+
def readiness(self, request):
37+
# Connect to each database and do a generic standard SQL query
38+
# that doesn't write any data and doesn't depend on any tables
39+
# being present.
40+
try:
41+
from django.db import connections
42+
for name in connections:
43+
cursor = connections[name].cursor()
44+
cursor.execute("SELECT 1;")
45+
row = cursor.fetchone()
46+
if row is None:
47+
return HttpResponseServerError(
48+
"Postgres: invalid response")
49+
except Exception as e:
50+
logger.exception(e)
51+
return HttpResponseServerError(
52+
"Postgres: cannot connect to database.")
53+
54+
# Call get_stats() to connect to each memcached
55+
# instance and get it's stats.
56+
# This can effectively check if each is online.
57+
try:
58+
import redis
59+
r = redis.Redis(host=os.environ.get('REDIS_SERVICE_HOST', 'redis'))
60+
r.ping()
61+
except Exception as e:
62+
logger.exception(e)
63+
return HttpResponseServerError(
64+
"Redis: cannot connect to redis.")
65+
return HttpResponse("OK")

compose/minikube.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ services:
1515
- HTTP_PROTOCOL=http
1616

1717
backend:
18-
image: backend:2
18+
image: backend:8
1919
build:
2020
context: ../backend/
2121
dockerfile: scripts/dev/Dockerfile

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ services:
106106
- GITHUB_SECRET=${GITHUB_SECRET}
107107
- GOOGLE_OAUTH2_KEY=${GOOGLE_OAUTH2_KEY}
108108
- GOOGLE_OAUTH2_SECRET=${GOOGLE_OAUTH2_SECRET}
109+
- REDIS_SERVICE_HOST=redis
109110
depends_on:
110111
- postgres
111112

documentation/docs/topics/minikube/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,7 @@ https://stackoverflow.com/questions/55573426/virtualbox-is-configured-with-multi
310310
```
311311
minikube addons enable ingress
312312
```
313+
314+
315+
## Healthchecks
316+

kubernetes/django/deployment.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,19 @@ spec:
1515
containers:
1616
- name: backend
1717
imagePullPolicy: IfNotPresent
18-
image: backend:2
18+
image: backend:8
1919
command: ["./manage.py", "runserver", "0.0.0.0:8000"]
20+
livenessProbe:
21+
httpGet:
22+
path: /healthz
23+
port: 8000
24+
readinessProbe:
25+
# an http probe
26+
httpGet:
27+
path: /readiness
28+
port: 8000
29+
initialDelaySeconds: 20
30+
timeoutSeconds: 5
2031
ports:
2132
- containerPort: 8000
2233
env:

kubernetes/redis/service.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apiVersion: v1
22
kind: Service
33
metadata:
4-
name: redis-service
4+
name: redis
55
spec:
66
selector:
77
pod: redis

0 commit comments

Comments
 (0)