All posts

Fix Kubernetes Pod Crash in Scala

Resolve Scala application pod crashes in Kubernetes, including JVM tuning for containers, Akka configuration, and graceful shutdown.

Scala Pods Crashing in Kubernetes

Scala runs on the JVM, so it shares many Kubernetes pitfalls with Java. But Scala frameworks like Akka and Play add their own complications.

JVM Container Awareness

The same JVM flags apply:

ENV JAVA_OPTS="-XX:+UseContainerSupport -XX:MaxRAMPercentage=70.0 -XX:+ExitOnOutOfMemoryError"
CMD java $JAVA_OPTS -jar app.jar

-XX:+ExitOnOutOfMemoryError ensures the pod actually crashes (and gets restarted) instead of limping along in a broken state.

Akka Actor System Shutdown

Akka needs a graceful shutdown hook, or Kubernetes sends SIGKILL after the grace period:

import akka.actor.CoordinatedShutdown

val system = ActorSystem("MySystem")

CoordinatedShutdown(system).addTask(
  CoordinatedShutdown.PhaseBeforeServiceUnbind,
  "cleanup"
) { () =>
  // drain connections, finish processing
  Future.successful(Done)
}

Set a matching termination grace period:

terminationGracePeriodSeconds: 30

Play Framework Health Check

// app/controllers/HealthController.scala
class HealthController extends BaseController {
  def check = Action { Ok("healthy") }
}

// conf/routes
GET /healthz controllers.HealthController.check

Thread Pool Starvation

Scala's ExecutionContext.global is a fork-join pool sized to available processors. In a container, this might be just 1 or 2 threads. Configure explicitly:

akka.actor.default-dispatcher {
  fork-join-executor {
    parallelism-min = 4
    parallelism-max = 16
  }
}

Bugsly captures JVM-level crashes and Akka dead letters, providing the context needed to diagnose pod failures.

Try Bugsly Free

AI-powered error tracking that explains your bugs. Set up in 2 minutes, free forever for small projects.

Get Started Free