Kubernetes for Java Services: Getting Resources, Probes, and Shutdown Right
On this page

Kubernetes for Java Services: Getting Resources, Probes, and Shutdown Right
Why this post exists
Most teams copy the deployment YAML from another service, tweak the memory number, and move on. Then months later they hit mysterious latency spikes or pods getting killed for no clear reason — and the cause is almost always sitting in that YAML. Here are the decisions that actually matter, explained simply.
- Memory: requests = limits
resources:
requests:
memory: "1024Mi"
limits:
memory: "1024Mi"
Memory limits are a hard wall — if your process goes over, the kernel kills the container instantly (OOMKilled), no warning. Because JVM memory usage is fairly predictable once sized correctly, setting requests = limits (called Guaranteed QoS) is the right default — it tells Kubernetes exactly what this pod needs, with no surprises.
The part people miss: don’t set -Xmx equal to the container limit. The JVM needs room inside that limit for things outside the heap — thread stacks, metaspace, JIT code cache, native buffers. Leave 25–35% headroom. Easiest way: use -XX:MaxRAMPercentage=70.0 instead of a fixed -Xmx, so the JVM sizes the heap as a percentage of what it detects, with headroom built in automatically.
2. CPU: decide Guaranteed or Burstable — deliberately
resources:
requests:
cpu: "500m"
limits:
cpu: "700m"
Memory works like a wall — cross it and you’re killed. CPU works differently — it’s more like a speed bump. If your app tries to use more CPU than its limit, Kubernetes doesn’t kill it. It just slows it down by pausing your threads for a moment, even if the rest of the server is sitting idle. This “slowing down” is called throttling, and it’s a common hidden cause of slow responses and GC pauses under load.
So you have two options:
- Requests = limits: Your app always gets exactly what it asked for, no surprises. Costs a bit more, but performance stays steady. Good for services where speed and consistency really matter.
- Requests < limits: Cheaper day-to-day, and your app can borrow extra CPU when it needs a quick burst. The tradeoff: when the server is busy, you may get throttled.
If you choose the second option, keep an eye on a specific metric called container_cpu_cfs_throttled_periods_total. This tells you how often your app got throttled — plain "CPU usage %" won't show you this, since a pod can look only half-used while still being throttled behind the scenes.
One more tip: don’t pick your CPU numbers just by looking at how big the server is. Run a real load test, watch what your app actually uses, and size it from that.
3. Probes: three different questions
startupProbe:
httpGet: { path: /health/live, port: 8081 }
failureThreshold: 30
periodSeconds: 10
readinessProbe:
httpGet: { path: /health/ready, port: 8081 }
failureThreshold: 3
periodSeconds: 10
livenessProbe:
httpGet: { path: /health/live, port: 8081 }
failureThreshold: 3
periodSeconds: 10
**startupProbe**— "has the app finished booting?" While it's failing, Kubernetes won't run liveness/readiness checks or restart anything — it just waits. JVM cold start (class loading, JIT warmup, connection pool init) can take a couple of minutes, so give this a generous threshold (here, up to 300 seconds). Without it, your tighter livenessProbe can kill the pod mid-startup, causing a crash loop that isn't actually a bug.**readinessProbe**— "should traffic go to this pod right now?" Failing this just pulls the pod out of load-balancing rotation — nothing gets killed. Good for checking real dependencies (database, downstream services).**livenessProbe**— "is this process broken and does it need a hard restart?" This is the only one that kills the container, so keep it narrow — check basic process health only. Never point it at something that checks a database or another service; if that dependency has a blip, Kubernetes will restart every pod at once, turning a small outage into a full one.
4. Graceful shutdown
lifecycle:
preStop:
exec:
command: ["sh", "-c", "sleep 5"]
terminationGracePeriodSeconds: 45
When Kubernetes stops a pod, it sends SIGTERM, waits for terminationGracePeriodSeconds, then force-kills it. The catch: the pod can still receive requests for a moment after it's marked for removal, because that removal takes a few seconds to propagate across the cluster.
The preStop sleep buys time for that propagation to finish before your app starts actually shutting down. The longer grace period then gives your app time to stop accepting new connections and finish in-flight requests cleanly instead of being killed mid-request. Confirm your app actually handles SIGTERM this way — it's a code-level behavior, not just a YAML setting.
5. Logs and volumes: usually simpler than people think
The default, correct pattern: log to stdout/stderr, no volume at all. Kubernetes and your log aggregator (Fluentd, Fluent Bit, Loki, etc.) capture stdout natively — zero extra config, and it’s what kubectl logs reads too.
If your app currently writes logs to a file by default (OpenLiberty does this out of the box), redirect that to stdout rather than adding a volume to persist the file.
Don’t reach for a **PersistentVolume** for logs — it ties log retention to a specific pod, doesn't scale with replicas or rolling deploys, and isn't what PVs are for. Long-term retention belongs in your log aggregation backend, not on the pod.
If you truly need a local file (some legacy log appender, or a compliance requirement), use emptyDir — never a PersistentVolume — paired with a sidecar container (Fluent Bit is standard) that tails the file and ships it out. And set explicit log rotation (max size, max file count) at the appender level, or an unrotated file will grow until it fills the volume and starts evicting your pod — a self-inflicted outage that looks mysterious until you check disk usage.
6. A few JVM args worth setting explicitly
**-XX:MaxRAMPercentage=70.0**— sizes the heap as a percentage of the container's memory limit, with headroom, instead of a fixed number that can break if the limit ever changes.**-XX:ActiveProcessorCount=N**— the JVM can round a CPU limit like700mup to1full core when deciding how many GC threads or thread-pool threads to create. Setting this explicitly to match your real CPU limit avoids that mismatch.**-XX:MaxGCPauseMillis=200**— gives G1GC an explicit pause target instead of relying on the default, which matters directly for your probe timeouts.**-XX:+ExitOnOutOfMemoryError**— makes the JVM exit immediately on OOM instead of limping along broken. Combined with yourlivenessProbe, this turns a silent zombie pod into a fast, clean restart.
The honest takeaway
None of these six things are exotic on their own, but they interact — a tight CPU limit changes GC pause behavior, which changes probe timing, which changes how fast Kubernetes reacts to real problems. Getting these right once is usually the highest-leverage half-day of work you can spend on a Java service running in Kubernetes.