Adding a Marathon health check to your Python 3 Mesos application

Post to Twitter

My last article showed you how to deploy a Python 3 Application to an Apache Mesos Cluster using the Marathon API. I just wanted to do a quick follow-up on how you can easily add a Marathon HTTP health check for your application.

There are several ways you can add a health check to an application deployed with the Marathon API. I’m going to focus on just a simple HTTP health check. See the Marathon docs for more details and other options.

If you followed my previous article then the changes to the Python 3 code are trivial:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello World'

@app.route('/health')
def health():
    return '', 200

if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True)

I simply added a /health endpoint that returns an HTTP Status code of 200. This is most minimal thing you can do to support health checks in Marathon.

Rebuild the docker image:

$ docker build -t mypythonapp .

Deployment is very easily taking most of the defaults for the health check it looks like this:

curl -X POST -H "Accept: application/json" -H "Content-Type: application/json" localdocker:8080/v2/apps -d '{"id": "mypythonapp", "container": {"docker": {"image": "mypythonapp"}, "type": "DOCKER", "volumes": []}, "cpus": 0.2, "mem": 32.0, "instances": 1, "healthChecks": [{"protocol": "HTTP", "path": "/health", "port": 5000}]}'

Note: Make sure you are running Docker as well as this.

That cURL call above will deploy the Python 3 web app along with the health check which will kick in by default at 60 seconds.

Marathon UI - Running app with health status

Now you can now play with the code and health checks more and make them as sophisticated as needed.

Post to Twitter

This entry was posted in Apache Mesos, Open Source, Python. Bookmark the permalink.