How to setup health check page in django

空扰寡人 提交于 2019-12-05 17:18:02

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.

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/

@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.

http://supervisord.org/

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!