A simple module to allow you to easily add health endpoints to your Flask application
Project description
Flask-Healthz
Define endpoints in your Flask application that Kubernetes can use as liveness and readiness probes.
Setting it up
Register the blueprint on your Flask application:
from flask import Flask
from flask_healthz import healthz
app = Flask(__name__)
app.register_blueprint(healthz, url_prefix="/healthz")
Define the functions you want to use to check health. To signal an error, raise flask_healthz.HealthError
.
from flask_healthz import HealthError
def liveness():
pass
def readiness():
try:
connect_database()
except Exception:
raise HealthError("Can't connect to the database")
Now point to those functions in the Flask configuration:
HEALTHZ = {
"live": "yourapp.checks.liveness",
"ready": "yourapp.checks.readiness",
}
It is possible to directly set callables in the configuration, so you could write something like:
HEALTHZ = {
"live": lambda: None,
}
Check that the endpoints actually work:
$ curl http://localhost/yourapp/healthz/live
OK
$ curl http://localhost/yourapp/healthz/ready
OK
Now your can configure Kubernetes or OpenShift to check for those endpoints.
Here's an example of how you could do that in OpenShift's deploymentconfig
:
kind: DeploymentConfig
spec:
[...]
template:
[...]
spec:
containers:
- name: yourapp
[...]
livenessProbe:
httpGet:
path: /healthz/live
port: 8080
initialDelaySeconds: 5
timeoutSeconds: 1
readinessProbe:
httpGet:
path: /healthz/ready
port: 8080
initialDelaySeconds: 5
timeoutSeconds: 1
Examples
Here are some projects that have setup flask-healthz:
- Noggin: https://github.com/fedora-infra/noggin/pull/287
- FASJSON: https://github.com/fedora-infra/fasjson/pull/81
License
Copyright 2020 Red Hat
Flask-Healthz is licensed under the same license as Flask itself: BSD 3-clause.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for flask_healthz-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f24dbf05337beb03fe349636ca8479f6964db6befa4ea16b1aecc8521d06c024 |
|
MD5 | 6c2f0500f0a2231dbd641eca94f78a46 |
|
BLAKE2b-256 | 942346b3742a46f4704923d3b1f8d6ad1b8919132fa884b6e22e0cc2b56c978c |