【发布时间】:2021-06-03 03:03:28
【问题描述】:
我正在尝试为我的 python 应用程序实现 flask-healthz (https://pypi.org/project/flask-healthz/) 以获得活性和红色探测的回报。但不知何故,它对我不起作用。下面是我的代码 sn-p :
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 中很好地运行相同的程序,但没有成功。
如果有一些可行的例子,我会很高兴。
【问题讨论】:
-
你有你的 'yourapp.checks.liveness' 和 'yourapp.checks.readiness' 或者这只是示例的复制粘贴?
-
对于 openshift 部署,在我的 deployment.yaml 中,我将容器的名称设置为 yourapp。为了在本地 python 终端中运行这个脚本,这只是一个复制粘贴,因为本地没有 yourapp
标签: python-3.x docker kubernetes openshift