Skip to content

Make aspnetcore shutdown work by default on Kubernetes #30387

Open
@jkotalik

Description

@jkotalik

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:

  1. Wait a bit longer before exiting
  2. Continue to process incoming traffic, even though we get SIGTERM
  3. Shutdown all connections (drain), especially long lived ones like DB or WebSockets
  4. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions