nginx - 在 docker 文件中添加 Nginx 的健康检查
问题描述
我有一个WebAssmebly Blazor
在本地运行的应用程序,我使用 Helm 将它部署到 Azure K8s 集群,
当我检查它抱怨健康检查丢失的日志时,应用程序 pod 不断重启
[21/Apr/2021:11:23:55 +0000] "GET /health/liveness HTTP/1.1" 404 153 "-" "kube-probe/1.17" "-"
2021/04/21 11:23:55 [error] 31#31: *3 open() "/usr/share/nginx/html/health/liveness" failed (2: No such file or directory), client: 10.244.0.1, server: localhost, request: "GET /health/liveness HTTP/1.1", host: "10.244.0.230:80"
由于WebAssmebly Blazor
在客户端运行,因此不需要运行状况。
所以,我试图在 Nginx 级别编写一些静态健康检查。
这是泊坞窗文件:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /source
FROM build AS publish
WORKDIR /source/app.CustomerApplication/
RUN dotnet publish -c release
FROM nginx AS runtime
COPY --from=publish /source/app.CustomerApplication/bin/release/netstandard2.1/publish/wwwroot/. /usr/share/nginx/html/.
COPY ./app.CustomerApplication/nginx.conf /etc/nginx/nginx.conf
RUN rm /etc/nginx/conf.d/default.conf
和 Nginx 配置文件:
server {
location / {
root /usr/share/nginx/html;
}
location //health/liveness {
return 200 'alive';
add_header Content-Type text/plain;
}
}
我不断收到此错误
cust-app /docker-entrypoint.sh: Configuration complete; ready for start up
cust-app | 2021/04/20 23:56:16 [emerg] 1#1: unknown directive "server" in /etc/nginx/nginx.conf:1
cust-app | nginx: [emerg] unknown directive "server" in /etc/nginx/nginx.conf:1
解决方案
首先WebAssmebly Blazor
在客户端运行,因此为了破解 K8s 健康检查,我通过修改 Nginx 配置文件在 Nginx 中的特定路由上创建了静态回复。对于有效的健康检查,我建议使用Server Blazor
类型,因为它在服务器端运行,因此可以在类中添加实际的健康检查startup
。
关于带有Nginx config
文件的发行者,我错了,更新到这个
server {
#listen 80;
#root /usr/share/nginx;
location / {
root /usr/share/nginx/html; #sepecify Blazor app route
}
location /health/liveness {
#access_log off;
error_log off;
return 200 'ok';
}
location /health/readiness {
#access_log off;
error_log off;
return 200 'ok';
}
}
还需要更新Dockerfile
:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /source
FROM build AS publish
WORKDIR /source/app.CustomerApplication/
RUN dotnet publish -c release
FROM nginx AS runtime
COPY --from=publish /source/app.CustomerApplication/bin/release/netstandard2.1/publish/wwwroot/. /usr/share/nginx/html/.
ADD ./app.CustomerApplication/default.conf /etc/nginx/conf.d/default.conf
更新 Nginx 后的 K8s 健康检查日志:
10.244.0.1 - - [22/Apr/2021:07:15:43 +0000] "GET /health/liveness HTTP/1.1" 200 2 "-" "kube-probe/1.17" "-"
10.244.0.1 - - [22/Apr/2021:07:15:58 +0000] "GET /health/liveness HTTP/1.1" 200 2 "-" "kube-probe/1.17" "-"
推荐阅读
- django-rest-framework - Django REST 嵌套序列化器
- jenkins - 詹金斯:“无法初始化类 org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist”
- python - Python 3 如何打印 lambda 表达式?
- node.js - 如何使用nodejs替换csv文件
- powershell - 从 SharePoint Online 列表中删除所有项目
- google-drive-api - 是否可以使用从服务器端 Oauth2 获取的访问令牌打开谷歌选择器?
- linux - 可以使用 WSL2 构建 LFS
- keycloak - 令牌中的 KeyCloak 组 ID 丢失
- javascript - tf.compat.v1.image.resize 的等效 tensorflow.js 函数?
- r - 如何用spss文件中的data.frames中的标签替换值?