Giter Site home page Giter Site logo

flask-healthz's Introduction

Flask-Healthz

Define endpoints in your Flask application that Kubernetes can use as liveness and readiness probes.

Setting it up

Blueprint

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
{"status": 200, "title": "OK"}
$ curl http://localhost/yourapp/healthz/ready
{"status": 200, "title": "OK"}

Now your can configure Kubernetes or OpenShift to check for those endpoints.

Extension

You can also use the provided Flask extension to register the healthz blueprint:

from flask import Flask
from flask_healthz import Healthz

app = Flask(__name__)
Healthz(app)

The rest of the configuration is identical.

The extension has an additional option, no_log, that can disable logging of the HTTP requests handled by your healthz endpoints, to avoid cluttering your web log files with automated requests. At the moment, only the gunicorn web server is supported.

Healthz(app, no_log=True)

Examples

Here's an example of how you could use flask-healthz 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

Some projects that have setup flask-healthz:

License

Copyright 2020-2021 Red Hat

Flask-Healthz is licensed under the same license as Flask itself: BSD 3-clause.

codecov

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.