Description
Right now, as soon as an aspnetcore app sees CTRL + C, it immediately starts shutting down the server, rejecting and draining connections. This works well when running on command line, but when you move into k8s, requests may be dropped.
This article gives a general overview of the problem. Kubernetes, the ingress controller, CoreDNS need time to remove the IP address from their internal state.
There are some solutions where you could have a dotnet app listen for endpoint changes within the k8s cluster. The recommended approach is to actually just wait after receiving a SIGTERM signal, rather than immediately shutting down.
The modification would be to enable shutdown on k8s to:
- Wait a bit longer before exiting
- Continue to process incoming traffic, even though we get SIGTERM
- Shutdown all connections (drain), especially long lived ones like DB or WebSockets
- Close the process
A recommend time to wait is 15 seconds.
I think the most difficult part of this will be detecting if you are running inside of k8s. Besides that, a Task.Delay seems like an appropriate solution.