Add health check for Nginx in docker file

2.9k views Asked by At

I have a WebAssmebly Blazor App that runs locally, I deploy it to Azure K8s cluster using Helm,

The app pod keeps restarting when I checked the logs its complaining about health check that are missing

[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"

Since WebAssmebly Blazor runs on the client side, a health isnt needed.

So, Im trying to write some static health check at Nginx level.

This is the docker file:

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

and Nginx config file:

server {
    location / {
        root /usr/share/nginx/html;
    }

    location //health/liveness {
        return 200 'alive';
        add_header Content-Type text/plain;
    }
}

I keep getting this error

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
1

There are 1 answers

0
ikenahim On

First WebAssmebly Blazor runs at the client side therefore to hackaround K8s health check I created static reply on specific routes in Nginx by modifying Nginx config file. For a valid health check I recomamand to use Server Blazor type since its run on the server side, there is possibility to add an actual healthy check in startup class.

Regarding the issuer with Nginx config file the template I was was wrong, updated to this

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';
  }
}

Also an update is needed in 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

K8s health check logs after updating Nginx:

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" "-"