What is terminationGracePeriodSeconds in Kubernetes?
In Kubernetes, terminationGracePeriodSeconds defines how much time (in seconds) a Pod gets to shut down gracefully before it is forcefully killed.
When Kubernetes decides to stop a Pod (for example during scaling down, deployment update, or manual deletion), it does not immediately kill the container. Instead, it gives the application some time to finish its work properly.
By default, this time is 30 seconds, but you can increase or decrease it depending on your application needs.
This is important because your application may need time to:
- Finish active requests
- Close DB connections
- Save data
- Release resources
Without this, your application may lose data or cause errors.
How it works internally (step-by-step flow)
When a Pod is terminated, Kubernetes follows a graceful shutdown sequence:
- Pod is marked as Terminating
- If defined โ
preStophook runs - Kubernetes sends SIGTERM signal to the container
- It waits for
terminationGracePeriodSeconds - If still running โ Kubernetes sends SIGKILL (force kill)
๐ Important understanding:
SIGTERM= polite request to stopSIGKILL= force kill (no cleanup)
Simple real-world example
Imagine your app is a web server (like NGINX or API server):
- Users are connected
- Requests are processing
Now Kubernetes wants to stop the pod.
If terminationGracePeriodSeconds = 30, then:
- Kubernetes tells your app:
๐ โFinish your work and shut down in 30 secondsโ - During this time:
- No new requests should be accepted
- Existing requests complete
- If still running after 30 seconds โ Kubernetes kills it forcefully
YAML Example: Pod will get 60 seconds before force termination.
apiVersion: v1kind: Podmetadata: name: my-appspec: terminationGracePeriodSeconds: 60 # Give 60 seconds to shutdown containers: - name: app-container image: nginx
YAML Example (Deployment)
apiVersion: apps/v1kind: Deploymentmetadata: name: web-appspec: replicas: 2 selector: matchLabels: app: web template: metadata: labels: app: web spec: terminationGracePeriodSeconds: 45 containers: - name: web image: nginx
Commands to use and test
โ 1. Apply YAML
kubectl apply -f pod.yaml
2. Check pod
kubectl get pods
3. Delete pod (trigger termination)
kubectl delete pod my-app
4. Watch termination behavior
kubectl describe pod my-app-- logskubectl logs -f my-app
5. Override grace period during delete
kubectl delete pod my-app --grace-period=10
When should you increase this value?
You should increase terminationGracePeriodSeconds when your application:
- Handles long-running requests
- Writes data to DB/files
- Needs cleanup logic
- Uses message queues (Kafka, RabbitMQ)
๐ Example:
- Banking app โ 60โ120 seconds
- API server โ 30โ60 seconds
- Batch job โ even higher
Final Summary
terminationGracePeriodSeconds= graceful shutdown time- Default = 30 seconds
- Uses:
- Prevent data loss
- Complete running requests
- Flow:
preStop โ SIGTERM โ wait โ SIGKILL