How to Set Initial Delay for Probe in Kubernetes
In Kubernetes, you set the initial delay for a probe using the
initialDelaySeconds field inside the probe configuration. This delay tells Kubernetes how many seconds to wait after the container starts before performing the first probe check.Syntax
The initialDelaySeconds field is part of the probe configuration in a Pod spec. It specifies the number of seconds Kubernetes waits after the container starts before running the first probe.
Other related fields include periodSeconds (how often to run the probe), timeoutSeconds (how long to wait for a probe response), and failureThreshold (how many failures before marking the container unhealthy).
yaml
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 10
periodSeconds: 5
timeoutSeconds: 2
failureThreshold: 3Example
This example shows a Pod spec with a liveness probe that waits 15 seconds after the container starts before checking the health endpoint. This helps avoid false failures if the app needs time to initialize.
yaml
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: example-container
image: nginx:1.23
livenessProbe:
httpGet:
path: /healthz
port: 80
initialDelaySeconds: 15
periodSeconds: 10
timeoutSeconds: 3
failureThreshold: 3Output
Pod "example-pod" created. The liveness probe will start checking /healthz on port 80 after 15 seconds of container start.
Common Pitfalls
- Setting
initialDelaySecondstoo low: The probe may run before the app is ready, causing unnecessary restarts. - Not setting
initialDelaySecondsat all: Defaults to 0, which can cause immediate probe failures if the app needs startup time. - Confusing
initialDelaySecondswithperiodSeconds: The initial delay is a one-time wait before the first probe; periodSeconds controls how often probes run after that.
yaml
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 0 # Too low, may cause false failures
# Corrected version:
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 10 # Waits 10 seconds before first checkQuick Reference
| Field | Description | Default Value |
|---|---|---|
| initialDelaySeconds | Seconds to wait before first probe | 0 |
| periodSeconds | Seconds between probe runs | 10 |
| timeoutSeconds | Seconds to wait for probe response | 1 |
| failureThreshold | Failures before marking unhealthy | 3 |
Key Takeaways
Use
initialDelaySeconds to delay the first probe after container start.Set
initialDelaySeconds long enough to let your app initialize.Not setting
initialDelaySeconds can cause premature probe failures.Remember
initialDelaySeconds is a one-time delay, different from periodSeconds.Adjust probe settings based on your app's startup behavior for stability.