0π
NOT RECOMMENDED β this was a quick and dirty way to get Django up and running with ELB, but in most cases a health check response should be returned directly from your application code. See the rest of the answers for a better solution.
This is something better handled by nginx, so the fact that itβs serving a django app should make no difference.
You will first need to configure ELBβs health check to ping a specific URL of your instance, say /elb-status
. You can follow the instructions here: http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/elb-healthchecks.html#update-health-check-config
After that, all you need to do is set up nginx to always send back an HTTP 200 status code. You can add something like this to your server
block in nginx.conf
:
location /elb-status {
access_log off;
return 200;
}
See this answer for more details.
18π
Here is another solution using Django Middleware.
from django.http import HttpResponse
class HealthCheckMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
if request.path == '/health':
return HttpResponse('ok')
return self.get_response(request)
And put your HealthCheckMiddleware
in front of other middlewares in settings.py
MIDDLEWARE = [
'yourdjangoapp.middleware.HealthCheckMiddleware',
......
]
And your application will always respond to path /health
with ok
as long as your app is running regardless of any configurations and authentication.
- How to make Django Password Reset Email Beautiful HTML?
- Django 'function' object has no attribute 'objects'
10π
You can use django-health-check
3rd party app.
Install it using pip β
pip install django-health-check
Configue URL as β
urlpatterns = [
# ...
url(r'^health_check/', include('health_check.urls')),
]
and add the health_check applications to your INSTALLED_APPS:
INSTALLED_APPS = [
# ...
'health_check', # required
]
More details : https://github.com/KristianOellegaard/django-health-check
Now you can confiure ELB health check url as β /health_check/
1π
Had the same issue at one point and we ended up creating django-heartbeat:
https://github.com/pbs/django-heartbeat
It provides an endpoint for making sure your app is up and running.
Plus some other useful "checkers" that you can enable, out of the box, to surface information about like:
- build version
- python, django or other dependencies
- database(s), cache(s) information, etc
https://github.com/pbs/django-heartbeat#available-checkers
If you find it useful, feel free to β it.
If you need any additional functionalities or want to contribute β let us know or just go ahead and open a PR.
Thanks!
- Django: correctly retrieve data where date and time are greater than now
- How to find out whether a model's column is a foreign key?
- Why does Django South 1.0 use iteritems()?
- How can I send signals from within Django migrations?
0π
@app.route('/elb-health-check')
def check_httpd_service():
server = xmlrpclib.Server('http://'+supervisor_host+':'+supervisor_rpc_port+'/RPC2')
state = server.supervisor.getProcessInfo(process)
print state['statename']
if state['statename'] == 'RUNNING':
return state['statename'], 200
else:
return state['statename'], 500
Define the variables accordingly.
The above code is an example to create a custom api without authentication. It can be invoked by HTTP GET request method and It can be used to perform health check from ELB.
It is just an example, you can use your own idea with flask/django to create a custom health check service for ELB.
Here I am control my application with supervisor, a process control system. From supervisor I can track the status of the application. Supervisor reloads the application, if it is exited.
If the service is running, the server will return 200 response code. If the service is not running on the host, it will return 500 response code. Based on this response code, we can route the traffic in the load balancer.
- Following users like twitter in Django, how would you do it?
- Is it correct to modify old migration files in Django?
- How to create or register User using django-tastypie API programmatically?
- Improving Performance of Django ForeignKey Fields in Admin