如何为 dockerized vue3 应用程序创建活动探测状态的端点?

How to create for dockerized vue3 app an endpoint for liveness probe status?

我有一个 vue3 typescript 应用程序,它在 k8s 上的 docker 中运行。

Vue 应用有不同的路由 / /关于 /登录

所有这些路由将从 vue 组件发回 html 响应。

对于 docker 活性探测,我需要一个简单的 json 响应 /status -> HTTP 200 & res=> JSON {status:ok}

Vue3 如何只响应特定路由上的 JSON 对象?

我的docker文件

FROM node:16.13.0 as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY ./ .
RUN npm run build

FROM nginx:1.20.1 as production-stage
RUN mkdir /app
COPY --from=build-stage /app/dist /app
COPY nginx.conf /etc/nginx/nginx.conf
COPY /entrypoint.sh /entrypoint.sh
EXPOSE 80

ENTRYPOINT ["/entrypoint.sh"]

我通过添加 nginx.conf 附加位置来解决它

  location /health/liveness {
    #access_log off;
    error_log   off;
    add_header 'Content-Type' 'application/json';
    return 200 '{"status":"ok"}';
  } 

nginx.conf


user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
  worker_connections  1024;
}
http {
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;
  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
  access_log  /var/log/nginx/access.log  main;
  sendfile        on;
  keepalive_timeout  65;
  server {
    listen       80;
    server_name  example.com;
    location / {
      root   /app;
      index  index.html;
      try_files $uri $uri/ /index.html;
    }
    location /health/liveness {
    #access_log off;
    error_log   off;
    add_header 'Content-Type' 'application/json';
    return 200 '{"status":"ok"}';
  }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
      root   /usr/share/nginx/html;
    }
  }
}