#健康检查 #fast-cgi #http #fcgi

app fastcgi-healthcheck

Fastcgi健康检查,由kvarn-fastcgi-client、axum和tokio提供支持

1个不稳定版本

0.2.0 2024年4月9日

#229 in HTTP服务器

GPL-3.0许可证

26KB
102

简介

由于在容器世界中健康检查非常关键,而且我没有找到任何简单的fastcgi (fcgi)客户端来完成此类检查,所以我决定用Rust创建一个。

目前,HTTP协议广泛支持健康检查,但fastcgi协议不支持。

fastcgi-healthcheck工具仅存在一个目的,它使用特定的URL调用fcgi服务器,并期望得到特定的答案。根据该答案,该工具将向请求者返回200或404 HTTP代码。

环境变量

变量 描述 默认值
SERVER 工具应监听的服务器和端口组合 127.0.0.1:8080
DESTINATION FCGI目标地址,可以是名称或IP地址 127.0.0.1
DESTINATION_PORT FCGI目标端口 9000
PING_PATH FCGI服务器上的Ping URL /fpm-ping
PING_RESPONSE FCGI服务器返回的Ping响应 pong

我使用以下库进行FCGI调用: https://github.com/Icelk/fastcgi-client-rs

自有的健康端点

此工具有自己的健康端点 /health/healthz

免责声明

这是我第一个Rust程序,我非常肯定这个工具可以简化 😃
我欢迎建议。

示例调试运行

场景:PHP-FPM未运行

健康检查工具运行

# shell 1
RUST_LOG=debug PING_PATH=/lala SERVER=127.0.0.1:8080 DESTINATION=127.0.0.1 DESTINATION_PORT=9000 cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.02s
Running `target/debug/fastcgi-healhcheck`
INFO 2024-04-09 11:03:53 UTC: Listening on 127.0.0.1:8080
DEBUG 2024-04-09 11:03:56 UTC: Request Headers {"host": "127.0.0.1:8080", "user-agent": "curl/7.81.0", "accept": "*/*"}
DEBUG 2024-04-09 11:03:56 UTC: Request Request { method: GET, uri: /fcgi-ping, version: HTTP/1.1, headers: {"host": "127.0.0.1:8080", "user-agent": "curl/7.81.0", "accept": "*/*"}, body: Body(UnsyncBoxBody) }
DEBUG 2024-04-09 11:03:56 UTC: Request URI :/fcgi-ping:
DEBUG 2024-04-09 11:03:56 UTC: destination_addr :127.0.0.1:
DEBUG 2024-04-09 11:03:56 UTC: destination_port :9000:
DEBUG 2024-04-09 11:03:56 UTC: ping_path :/lala:
DEBUG 2024-04-09 11:03:56 UTC: ping_response :pong:
ERROR 2024-04-09 11:03:56 UTC: Stream error :Connection refused (os error 111):
thread 'tokio-runtime-worker' panicked at src/main.rs:63:13:
Connection refused (os error 111)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

curl调用

# shell 2
curl -v http://127.0.0.1:8080/fcgi-ping*   Trying 127.0.0.1:8080...
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> GET /fcgi-ping HTTP/1.1
> Host: 127.0.0.1:8080
> User-Agent: curl/7.81.0
> Accept: */*
>
* Empty reply from server
* Closing connection 0
curl: (52) Empty reply from server

场景:PHP-FPM正在运行

健康检查工具运行

# shell 1
RUST_LOG=debug SERVER=127.0.0.1:8080 DESTINATION=127.0.0.1 DESTINATION_PORT=9000 cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.02s
Running `target/debug/fastcgi-healhcheck`
INFO 2024-04-09 11:07:44 UTC: Listening on 127.0.0.1:8080
DEBUG 2024-04-09 11:07:45 UTC: Request Headers {"host": "127.0.0.1:8080", "user-agent": "curl/7.81.0", "accept": "*/*"}
DEBUG 2024-04-09 11:07:45 UTC: Request Request { method: GET, uri: /fcgi-ping, version: HTTP/1.1, headers: {"host": "127.0.0.1:8080", "user-agent": "curl/7.81.0", "accept": "*/*"}, body: Body(UnsyncBoxBody) }
DEBUG 2024-04-09 11:07:45 UTC: Request URI :/fcgi-ping:
DEBUG 2024-04-09 11:07:45 UTC: destination_addr :127.0.0.1:
DEBUG 2024-04-09 11:07:45 UTC: destination_port :9000:
DEBUG 2024-04-09 11:07:45 UTC: ping_path :/fpm-ping:
DEBUG 2024-04-09 11:07:45 UTC: ping_response :pong:
DEBUG 2024-04-09 11:07:45 UTC: dest_connection :PollEvented { io: Some(TcpStream { addr: 127.0.0.1:60274, peer: 127.0.0.1:9000, fd: 8 }) }:
DEBUG 2024-04-09 11:07:45 UTC: Start handle request id=1
DEBUG 2024-04-09 11:07:45 UTC: Send to stream. id=1 begin_request_rec="BeginRequestRec {header: Header { version: 1, type: BeginRequest, request_id: 1, content_length: 8, padding_length: 0, reserved: 0 }, begin_request: BeginRequest { role: Responder, flags: 0, reserved: [0, 0, 0, 0, 0] }}"
DEBUG 2024-04-09 11:07:45 UTC: Params will be sent. id=1 param_pairs=ParamPairs([ParamPair { name_length: Short(15), value_length: Short(17), name_data: "SERVER_SOFTWARE", value_data: "fastcgi-client-rs" }, ParamPair { name_length: Short(14), value_length: Short(3), name_data: "REQUEST_METHOD", value_data: "GET" },ParamPair { name_length: Short(11), value_length: Short(9), name_data: "SCRIPT_NAME", value_data: "/fpm-ping" }, ParamPair { name_length: Short(11), value_length: Short(9), name_data: "REQUEST_URI", value_data: "/fpm-ping" }, ParamPair { name_length: Short(12), value_length: Short(9), name_data: "DOCUMENT_URI", value_data: "/fpm-ping" }, ParamPair { name_length: Short(11), value_length: Short(4), name_data: "SERVER_PORT", value_data: "9000" }, ParamPair { name_length: Short(15), value_length: Short(8), name_data: "SERVER_PROTOCOL", value_data: "HTTP/1.1" }, ParamPair { name_length: Short(17), value_length: Short(11), name_data: "GATEWAY_INTERFACE", value_data: "FastCGI/1.0" }, ParamPair { name_length: Short(15), value_length: Short(9), name_data: "SCRIPT_FILENAME", value_data: "/fpm-ping" }, ParamPair { name_length: Short(11), value_length: Short(9), name_data: "SERVER_ADDR", value_data: "127.0.0.1" }])
DEBUG 2024-04-09 11:07:45 UTC: Send to stream for Params. id=1 header=Header { version: 1, type: Params, request_id: 1, content_length: 240, padding_length: 0, reserved: 0 }
DEBUG 2024-04-09 11:07:45 UTC: Send to stream for Params. id=1 header=Header { version: 1, type: Params, request_id: 1, content_length: 0, padding_length: 0, reserved: 0 }
DEBUG 2024-04-09 11:07:45 UTC: Send to stream for Stdin. id=1 header=Header { version: 1, type: Stdin, request_id: 1, content_length: 0, padding_length: 0, reserved: 0 }
DEBUG 2024-04-09 11:07:45 UTC: Send to stream for Stdin. id=1 header=Header { version: 1, type: Stdin, request_id: 1, content_length: 0, padding_length: 0, reserved: 0 }
DEBUG 2024-04-09 11:07:45 UTC: Receive from stream. id=1 header=Header { version: 1, type: Stdout, request_id: 1, content_length: 186, padding_length: 6, reserved: 0 }
DEBUG 2024-04-09 11:07:45 UTC: Receive from stream. id=1 header=Header { version: 1, type: EndRequest, request_id: 1, content_length: 8, padding_length: 0, reserved: 0 }
DEBUG 2024-04-09 11:07:45 UTC: Receive from stream. id=1 end_request_rec=EndRequestRec { header: Header { version: 1, type: EndRequest, request_id: 1, content_length: 8, padding_length: 0, reserved: 0 }, end_request: EndRequest { app_status: 0, protocol_status: RequestComplete, reserved: [0, 0, 0] } }

curl调用

# shell 2
curl -v http://127.0.0.1:8080/fcgi-ping
*   Trying 127.0.0.1:8080...
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> GET /fcgi-ping HTTP/1.1
> Host: 127.0.0.1:8080
> User-Agent: curl/7.81.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< content-length: 0
< date: Tue, 09 Apr 2024 12:14:38 GMT
<
* Connection #0 to host 127.0.0.1 left intact

依赖项

~8–18MB
~228K SLoC