Spring Boot custom Kubernetes readiness probe
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Kubernetes readiness probes determine whether a pod is ready to accept traffic. When a readiness probe fails, the pod is removed from the Service endpoints, so no new requests are routed to it. Spring Boot provides built-in Actuator health infrastructure that integrates directly with Kubernetes probes, and you can extend it with custom health checks to cover your application's specific dependencies.
Default Readiness Support in Spring Boot
Starting with Spring Boot 2.3, the framework natively supports Kubernetes probe endpoints. When you add the Actuator dependency and enable the probe endpoints, Spring Boot exposes /actuator/health/readiness and /actuator/health/liveness automatically.
Add the dependency to your pom.xml:
Then enable the probe endpoints in application.yml:
With this configuration, Spring Boot reports the readiness state based on the application lifecycle. The readiness endpoint returns UP once the application context is fully loaded and all startup tasks are complete.
Creating a Custom HealthIndicator
The default readiness probe only checks the application lifecycle state. In production, you often need to verify that downstream dependencies such as databases, message brokers, or external APIs are reachable. You do this by implementing a custom HealthIndicator.
Spring Boot automatically includes all HealthIndicator beans in the readiness group. When any indicator reports DOWN, the readiness endpoint returns a 503 status, causing Kubernetes to stop routing traffic to that pod.
Grouping Health Indicators
You can control exactly which indicators belong to the readiness probe by defining health groups:
This configuration ensures that the readiness endpoint checks the database, Redis, and a custom external service, while the liveness probe only checks whether the application process is alive. Keeping liveness checks lightweight prevents Kubernetes from killing pods due to slow dependency responses.
Programmatic Readiness State Changes
Spring Boot 2.3+ also lets you change the readiness state programmatically. This is useful when your application needs to temporarily stop accepting traffic, for example during a graceful shutdown or a cache warming phase.
Kubernetes Deployment Configuration
Once your Spring Boot application exposes the readiness endpoint, configure the Kubernetes deployment manifest to use it:
Key parameters:
initialDelaySecondsgives the application time to start before the first probe runs.periodSecondscontrols how often Kubernetes sends the probe request.failureThresholdsets how many consecutive failures trigger the pod to be marked not ready.
Common Pitfalls
- Putting slow checks in the liveness probe: If a database timeout causes the liveness probe to fail, Kubernetes restarts the pod instead of just removing it from traffic. Keep liveness checks fast and dependency-free.
- Missing the Actuator dependency: Without
spring-boot-starter-actuator, the/actuator/health/readinessendpoint does not exist and Kubernetes probes return 404. - Setting initialDelaySeconds too low: If the probe fires before Spring Boot finishes loading, the pod gets marked not ready repeatedly and may enter a restart loop.
- Exposing health details to untrusted networks: Setting
show-details: alwaysreveals internal dependency information. In production, useshow-details: when-authorizedand secure the management endpoints. - Not separating readiness and liveness groups: Using the same health checks for both probes causes Kubernetes to restart pods for transient dependency failures instead of just pausing traffic.
Summary
- Spring Boot 2.3+ natively supports Kubernetes readiness and liveness probe endpoints through Actuator.
- Implement
HealthIndicatorbeans to add custom dependency checks to the readiness probe. - Use health groups to control which indicators belong to readiness versus liveness.
- Configure
initialDelaySeconds,periodSeconds, andfailureThresholdin your Kubernetes manifest to match your application startup time. - Keep liveness probes lightweight and reserve dependency checks for readiness probes to avoid unnecessary pod restarts.
Related reading
- Spring boot on Kubernetes does not get restarted on java.lang.OutOfMemoryError Java heap space
- Spring Cloud Kubernetes - Spring boot fails to start when config reload is enabled
- Spring Cloud Kubernetes Configuration Watcher with Notification Recipient Not Based on Secret Name
- Spring Cloud Kubernetes Spring Cloud Gateway Unable to find instance for k8s service
- Spring Boot in Docker
- Spring Boot integration tests AutoConfigureMockMvc and context caching
- spring boot data cassandra reactive JmxReporter problem
- Spring Boot Data JPA - Modifying update query - Refresh persistence context

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.