为python3实现flask-healthz

数据管道工

我正在尝试为我的 python 应用程序实现 flask-healthz ( https://pypi.org/project/flask-healthz/ ) 以获得活跃度和红色度探测的回报。但不知何故它对我不起作用。下面是我的代码片段:

from flask import Flask
from flask_healthz import healthz
from flask_healthz import HealthError

def printok():
    print("Everything is fine")

app = Flask(__name__)
app.register_blueprint(healthz, url_prefix="/healthz")

def liveness():
    try:
        printok()
    except Exception:
        raise HealthError("Can't connect to the file")

def readiness():
    try:
        printok()
    except Exception:
        raise HealthError("Can't connect to the file")

HEALTHZ = {
    "live": "yourapp.checks.liveness",
    "ready": "yourapp.checks.readiness",
}

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

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000)

代码的输出如下所示:

(app-root) curl http://localhost:5000/ 
127.0.0.1 - - [02/Jun/2021 01:02:56] "GET / HTTP/1.1" 200 -
Hello World! 



(app-root) curl http://localhost/yourapp/healthz/live
curl: (7) Failed to connect to localhost port 80: Connection refused


(app-root) curl http://localhost:5000/yourapp/healthz/live
127.0.0.1 - - [02/Jun/2021 01:03:23] "GET /yourapp/healthz/live HTTP/1.1" 404 -
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>


(app-root) curl http://localhost:5000/yourapp/healthz/ready
127.0.0.1 - - [02/Jun/2021 01:03:37] "GET /yourapp/healthz/ready HTTP/1.1" 404 -
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>


(app-root) curl http://localhost:5000/healthz/readiness
127.0.0.1 - - [02/Jun/2021 01:04:02] "GET /healthz/readiness HTTP/1.1" 404 -
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The readiness check endpoint is not setup</p>


(app-root) curl http://localhost:5000/healthz/liveness
127.0.0.1 - - [02/Jun/2021 01:04:10] "GET /healthz/liveness HTTP/1.1" 404 -
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The liveness check endpoint is not setup</p>
(app-root) 

是不是我做错了什么。我试图在 Openshift 中很好地运行它,但没有运气。

如果有一些工作示例,我会很高兴。

贾布森

假设这是文档中的复制粘贴,这里是您可以更改以使其工作的内容。

app.py

from flask import Flask
from flask_healthz import healthz
from flask_healthz import HealthError

app = Flask(__name__)
app.register_blueprint(healthz, url_prefix="/healthz")

def printok():
    print("Everything is fine")

def liveness():
    try:
        printok()
    except Exception:
        raise HealthError("Can't connect to the file")

def readiness():
    try:
        printok()
    except Exception:
        raise HealthError("Can't connect to the file")

app.config.update(
    HEALTHZ = {
        "live": "app.liveness",
        "ready": "app.readiness",
    }
)

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

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000)

然后

curl localhost:5000/healthz/live
OK

curl localhost:5000/healthz/ready
OK

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

向NGINX Docker映像添加/ healthz

无法使用kubeadm设置kubelet healthz绑定地址

kubeadm连接失败,http:// localhost:10248 / healthz连接被拒绝

Python3 Flask bool查询参数始终为true

使用/ healthz进行应用程序运行状况检查的惯例从何而来?

Istio 排除匹配不适用于没有 jwt 主体的 healthz api

在Google App Engine上部署的应用程序的/ healthz路由返回404

k8s:Get "http://localhost:10248/healthz": dial tcp [::1]:10248: connect: 连接被拒绝

kubespray 中途停止,https://127.0.0.1:6443/healthz, Request failed: <urlopen error Tunnel connection failed: 403 Forbidden>"

在python3中为Enum类型实现比较运算符

Python3 Flask路由变量显示为变量名,而不是邮递员传递的值

在 Python Flask App 中为用户实现版本控制

使用python3而不是python运行Flask

Python3。Octree的实现占用大量内存。如何优化?

Flask 是为 Python 2.7 而不是为 Python 3 安装的

路由挂起中的Python3 Flask asyncio子进程

让Flask使用Python3(Apache / mod_wsgi)

Flask python3 中的 SOAP 服务器

无法为 python3 正确安装 pip3

使用brew为Python3安装Boost-python

如何使用 pip 为 python3 安装 python 包?

关于在flask(flask,Python3)中保存表单数据的问题

如何在树莓派上为python3安装pocketsphinx

python3:json可以解码为元组吗?

在Mac OS上为python3安装mariadb的mysqlclient

在Ubuntu上为Python3安装mod_wsgi

为 python3 安装 simplekml 时出错

使用Python3将Meld设置为git mergetool

在Linux Mint 16上为python3构建protobuf