Middle+
What factors contribute to the completion speed of pods according to set time management?
sobes.tech AI
Answer from AI
The termination of pods in Kubernetes is regulated by the following factors:
- terminationGracePeriodSeconds: Defines the time given to applications in a pod for proper termination (handling current requests, closing connections, etc.) after receiving the
SIGTERMsignal. If the application does not terminate within this time, aSIGKILLis sent.
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: app
image: my-image
terminationGracePeriodSeconds: 30 # Default value - 30 seconds
- ** preStop Hook:** Allows executing specific actions before sending
SIGTERM. For example, stopping acceptance of new connections.
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: app
image: my-image
lifecycle:
preStop:
exec:
command: ["/bin/sh", "-c", "nginx -s quit"] # Example: proper shutdown of Nginx
- ** readiness and liveness probes:** Determine the readiness and "alive" state of the pod. If pods become "not alive" (livenessProbe fails) or "not ready" (readinessProbe fails, and the controller sees that the pod is no longer needed), they can be terminated.
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: app
image: my-image
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 3
livenessProbe:
httpGet:
path: /live
port: 8080
initialDelaySeconds: 15
periodSeconds: 5
- ** Desired State:** Kubernetes controllers (e.g., Deployment, ReplicaSet) aim to maintain the specified number of replicas. When the Desired State changes (decreasing the number of replicas) or during updates, pods that are no longer needed start to terminate. The speed of this process depends on the update strategy (e.g.,
RollingUpdate) and parameters likemaxUnavailable.
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1 # Defines how many pods can be unavailable during update
maxSurge: 1
-
** Node Drain:** When a node is put into maintenance mode (
kubectl drain), Kubernetes terminates the pods on that node. The speed also depends onterminationGracePeriodSecondsand the—ignore-daemonsetssetting. -
** Application responding to SIGTERM:** The application inside the pod should handle the
SIGTERMsignal properly and be able to finish its work within theterminationGracePeriodSeconds.
| Factor | Impact on termination |
|---|---|
terminationGracePeriodSeconds |
Time for proper termination after SIGTERM |
preStop Hook |
Actions before SIGTERM |
| Readiness/Liveness Probes | Triggers for initiating termination process |
| Desired State / Strategy | Number and speed of termination during replica change/update |
| Node Drain | Termination of all pods on a node for maintenance |
Handling SIGTERM in application |
Application's ability to terminate itself properly |