蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

通过真实本机请求区分健康与就绪

PYTHONPython 3.12.14 与 kubectl 客户端,本地回环测试。probe_checks.py
probe_checks.py45 行
"""Use real local HTTP requests to verify the probe response contract."""
from http.client import HTTPConnection
import json
from threading import Thread

from lab_tools import run_exercise
from probe_app import ProbeServer


def request(port, path):
    client = HTTPConnection("127.0.0.1", port, timeout=3)
    try:
        client.request("GET", path)
        response = client.getresponse()
        return [response.status, json.loads(response.read())["status"]]
    finally:
        client.close()


def exercise(checks, _):
    paths = ["/livez", "/startupz", "/readyz", "/missing"]
    with ProbeServer(("127.0.0.1", 0)) as server:
        thread = Thread(target=server.serve_forever, daemon=True)
        thread.start()
        try:
            port = server.server_port
            first = [request(port, path) for path in paths]
            server.ready.set()
            second = [request(port, path) for path in paths]
            server.ready.clear()
            third = [request(port, path) for path in ("/livez", "/readyz")]
        finally:
            server.shutdown()
            thread.join(timeout=3)
    checks.expect("deploy_probe_warmup", paths, "GET actual loopback endpoints before readiness",
                  [[200,"live"],[503,"warming"],[503,"warming"],[404,"not_found"]], first)
    checks.expect("deploy_probe_ready", paths, "GET actual loopback endpoints after setting readiness",
                  [[200,"live"],[200,"ready"],[200,"ready"],[404,"not_found"]], second)
    checks.expect("deploy_probe_degraded", ["/livez","/readyz"], "Clear readiness without stopping the process",
                  [[200,"live"],[503,"warming"]], third)


if __name__ == "__main__":
    run_exercise(exercise)
本例需在 Python 3.12.14 与 kubectl 客户端,本地回环测试。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Python 3.12.14 与 kubectl 客户端,本地回环测试。

入口文件:projects/ai-agent-production/probe_checks.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 probe_checks.py,或下载右侧课程工程包。
  2. 确认本地已准备 Python 3.12.14 与 kubectl 客户端,本地回环测试。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:AI Agent(智能体)

课程关联工程包(登记关联48章) · 含当前章节或实例 · mbskycom202612a3073.zip

下载课程关联工程包:AI Agent(智能体)

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3098.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习