¥Health Checks and Graceful Shutdown
¥Graceful shutdown
当你部署新版本的应用时,你必须替换以前的版本。你使用的进程管理器将首先向应用发送 SIGTERM 信号,通知它将被终止。一旦应用收到此信号,它应该停止接受新请求,完成所有正在进行的请求,清理它使用的资源,包括数据库连接和文件锁,然后退出。
¥When you deploy a new version of your application, you must replace the previous version. The process manager you’re using will first send a SIGTERM signal to the application to notify it that it will be killed. Once the application gets this signal, it should stop accepting new requests, finish all the ongoing requests, clean up the resources it used, including database connections and file locks then exit.
¥Example
const server = app.listen(port)
process.on('SIGTERM', () => {
debug('SIGTERM signal received: closing HTTP server')
server.close(() => {
debug('HTTP server closed')
})
})
¥Health checks
负载均衡器使用健康检查来确定应用实例是否健康并且可以接受请求。例如,Kubernetes 有两个健康检查:
¥A load balancer uses health checks to determine if an application instance is healthy and can accept requests. For example, Kubernetes has two health checks:
liveness
,确定何时重新启动容器。
¥liveness
, that determines when to restart a container.
readiness
,确定容器何时准备好开始接受流量。当 pod 未就绪时,它会从服务负载均衡器中删除。
¥readiness
, that determines when a container is ready to start accepting traffic. When a pod is not ready, it is removed from the service load balancers.