Kubernetes probes and shutdown
What you will learn
Kubernetes startup, readiness and liveness probes answer different questions. Startup allows initialization to finish; readiness indicates whether the instance should receive traffic; liveness detects when restarting may help. Graceful shutdown first stops accepting work, then drains bounded in-flight work before process termination.
- Explain distinct probe responses and consequences.
- Model admission and draining as state transitions.
- Budget application cleanup within a termination grace period.
Plain Java examples: save as Main.java in a fresh directory, then run javac --release 21 Main.java and java Main. Standalone Java 21 functional examples. Docker and Kubernetes recipes are source-reviewed, not executed. Companion local HTTP/JDK evidence is separate.
Separate readiness from liveness
public class Main { static class Lifecycle { boolean started, draining; int active; int ready(){return started&&!draining?200:503;} int live(){return 200;} boolean admit(){if(ready()!=200)return false;active++;return true;} void finish(){if(active==0)throw new IllegalStateException();active--;} } public static void main(String[] args) { var s=new Lifecycle(); System.out.println(s.ready()); s.started=true; System.out.println(s.ready()); s.draining=true; System.out.println(s.ready()+" "+s.live()); }}Expected output:
503200503 200Draining makes readiness fail while the process remains live.
Probe contracts
A startup probe suppresses readiness and liveness checks until it succeeds. A failed readiness probe marks the Pod unready rather than restarting it. Repeated startup or liveness failures can trigger restarts. Do not tie liveness to every downstream dependency: restarting all clients during a database outage can amplify failure. Keep endpoints inexpensive and responses small. Probe success is not a complete business health test.
Termination sequence
Kubernetes starts the grace period, runs any configured preStop hook, then asks the runtime to stop the container (normally SIGTERM). Endpoint removal and shutdown can overlap; allow for propagation and in-flight requests. A direct Java process can receive SIGTERM and run registered hooks, but SIGKILL is not catchable. Keep cleanup bounded, coordinate shared state, reject new work when draining, and close executors and resources. The companion local HTTP server demonstrates real response codes and a shutdown marker, not kubelet timing.
Drain existing work
public class Main { static class Lifecycle { boolean started, draining; int active; int ready(){return started&&!draining?200:503;} int live(){return 200;} boolean admit(){if(ready()!=200)return false;active++;return true;} void finish(){if(active==0)throw new IllegalStateException();active--;} } public static void main(String[] args) { var s=new Lifecycle();s.started=true; System.out.println(s.admit());s.draining=true; System.out.println(s.admit());s.finish(); System.out.println(s.active); }}Expected output:
truefalse0A single-threaded state model preserves previously admitted work. Real servers need atomic admission and accounting.
Budget cleanup
public class Main { static int remaining(int grace,int hook,int drain){if(grace<=0||hook<0||drain<0||hook>grace||drain>grace-hook)throw new IllegalArgumentException();return grace-hook-drain;} public static void main(String[] args) { System.out.println(remaining(30,5,10)); }}Expected output:
15Fictional seconds for a 30-second grace period. Remaining time is margin, not a guarantee.
Common mistakes
Do not use the same external dependency check for all probes, assume removal prevents every late request, or block indefinitely inside a shutdown hook. Readiness is not authentication. Expose only approved endpoints.
Practice
Start the lifecycle, admit two requests, start draining, finish both, and print 0. Verify no new work is admitted.
Expected output:
0Compare with the manual review criteria.
Further reading: Probes · Pod lifecycle. Independent editorial review remains pending.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Kubernetes probes and shutdown — knowledge check
Container deployment and operations reasoning.
What does readiness failure normally do?
What is startup probe used for?
What should liveness test?
What signal cannot be caught?
When does the preStop hook consume time?