I'm trying to verify that shutdown is completing cleanly on Kubernetes, with a .NET Core 2.0 app.
I have an app which can run in two "modes" - one using ASP.NET Core and one as a kind of worker process. Both use Console and JSON-which-ends-up-in-Elasticsearch-via-Filebeat-sidecar-container logger output which indicate startup and shutdown progress.
Additionally, I have console output which writes directly to stdout when a SIGTERM or Ctrl-C is received and shutdown begins.
Locally, the app works flawlessly - I get the direct console output, then the logger output flowing to stdout on Ctrl+C (on Windows).
My experiment scenario:
helm
, though I imagine that doesn't make a difference)kubectl logs -f
to stream logs from the specific containerhelm delete
FROM microsoft/dotnet:2.1-aspnetcore-runtime
and has ENTRYPOINT ["dotnet", "MyAppHere.dll"]
, so not wrapped in a bash
process or anythingterminationGracePeriodSeconds
so guess it defaults to 30 secResults:
My conclusions:
I would like to know:
Try add STOPSIGNAL SIGINT
to your Dockerfile
I've started to look into this for my own purposes and have come across your question over a year after it was posted... This is a bit late, but have you tried GraceTerm? There is an associated NuGET package for this.
From the description...
Graceterm middleware provides implementation to ensure graceful shutdown of AspNet Core applications. The basic concept is: After application received a SIGTERM (a signal asking it to terminate), Graceterm will hold it alive till all pending requests are completed or a timeout occur.
I haven't personally tried this yet, but it does look promising.
SIGTERM
does indeed signal termination. The less obvious part is that when the SIGTERM
handler returns, everything is considered finished.
The fix is to not return from the SIGTERM
handler until the app has finished shutting down. For example, using a ManualResetEvent
and Wait()
ing it in the handler.