应用故障时如何自动重启Heroku Dynos?Heroku如何对标K8s探针机制?
Great question! Let's break down how Heroku approaches app fault recovery compared to Kubernetes's liveness/readiness probes, and how you can set up automatic restarts for your Dynos.
Default Crash Recovery Behavior
First off, Heroku has a built-in safety net for crashed Dynos. If your app's main process exits with a non-zero status code (like when a web app crashes due to an unhandled exception, or runs out of memory), Heroku's dyno manager will automatically attempt to restart the Dyno immediately.
This is a passive check—Heroku doesn't actively ping your app like Kubernetes does with probes; it just monitors whether your process is running. By default, it will keep retrying restarts (with a small backoff interval for repeated failures) to get your app back up.
Implementing Probe-Like Health Checks (For Active Monitoring)
If you want something closer to Kubernetes's active liveness probes—where the platform checks your app's internal health (not just if the process is running)—Heroku offers a Health Checks feature that you can configure manually. Here's how to set it up:
- First, add a health check endpoint to your app (e.g.,
/health). This endpoint should return a200 OKstatus code when your app is fully functional, and a5xxstatus code if it's in an unhealthy state (like unable to connect to a database, or stuck in a deadlock). - Next, configure the health check via Heroku CLI:
This tells Heroku to ping yourheroku health:add http://<your-app-domain>/health --interval=30 --timeout=5 --failure-threshold=3/healthendpoint every 30 seconds, wait 5 seconds for a response, and restart the Dyno if it gets 3 consecutive failures. - You can also adjust these settings or manage health checks directly from the Heroku Dashboard under your app's "Settings" tab.
Other Ways to Trigger Automatic Restarts
If you need more flexibility, here are a couple of additional methods:
- Process Exit Codes: Ensure your app exits with a non-zero code whenever it encounters an unrecoverable error. Heroku will pick up on this and restart the Dyno automatically—no extra configuration needed.
- Scheduler Add-on: Use the Heroku Scheduler to run a custom script at regular intervals. The script can check your app's health (e.g., by hitting your
/healthendpoint) and runheroku restartif it detects a failure. Note that this is a periodic check, not real-time like Health Checks. - Third-Party Monitoring Tools: Services like New Relic or Datadog can monitor your app's performance and health. You can set up alerts that trigger a Dyno restart via the Heroku API when specific failure conditions are met.
Key Difference from Kubernetes
The biggest contrast is that Kubernetes relies on active, configurable probes to proactively assess app health and take action (restart, mark as unready). Heroku's default behavior is passive (only reacts when the process dies), but you can replicate the proactive probe behavior using Heroku's Health Checks feature.
内容的提问来源于stack exchange,提问作者Andy Verbunt

