I can't find a way to provide a "graceful shutdown" in Nest microservices, in particular using NATS

2/15/2022

Hello everyone I can't find a way to provide a "graceful shutdown" in Nest microservices, in particular using NATS.

Expected behavior:

  1. The application in kubernetes received a 'SIGTERM' signal.
  2. stops listening for new incoming requests.
  3. service of accepted requests is completed and a response is given.
  4. the application closes all connections and shuts down.
-- Арсений Воробьёв
graceful-shutdown
kubernetes
nestjs

1 Answer

2/15/2022

You can use the Kubernetes spec config to set the graceful shutdown for POD: terminationGracePeriodSeconds

Default value of terminationGracePeriodSeconds is 30 seconds

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
    name: test
spec:
    replicas: 1
    template:
        spec:
            containers:
              - name: test
                image: ...
            terminationGracePeriodSeconds: 60

Read best practise : https://cloud.google.com/blog/products/containers-kubernetes/kubernetes-best-practices-terminating-with-grace

-- Harsh Manvar
Source: StackOverflow