标签:表示 image 方法 lsh 步骤 context bad ejs 情况

k8s-graceful-shutdown:该库提供了使用 Kubernetes 实现 Graceful Shutdown(优雅退出) Node.js App 的资源。
在 kubernetes 中运行微服务时。我们需要处理 kubernetes 发出的终止信号。这样做的正确方法是:
SIGINT, SIGTERM/health 路由应返回状态码 4xx,5xx)该库使上述过程变得容易。只需注册您的 graceful shutdown hook(优雅退出的钩子)并添加宽限期即可。
请注意,您的宽限期必须小于 kubernetes 中定义的宽限期!
例如,使用Express框架:
import { Response, Request } from ‘express‘
import express from ‘express‘
import { addGracefulShutdownHook, getHealthHandler, shutdown } from ‘@neurocode.io/k8s-graceful-shutdown‘
const app = express()
app.disable(‘x-powered-by‘)
const port = process.env.PORT || 3000
const server = app.listen(port, () => console.log(`App is running on http://localhost:${port}`))
// 修补 NodeJS 服务器关闭功能,使其具有正确的关闭功能,因为您可能期望为您关闭 keep-alive connections(保持活动的连接)!
// 在这里阅读更多信息 https://github.com/nodejs/node/issues/2642
server.close = shutdown(server)
const healthy = (req: Request, res: Response) => {
res.send(‘everything is great‘)
}
const notHealthy = (req: Request, res: Response) => {
res.status(503).send(‘oh no, something bad happened!‘)
}
const healthTest = async () => {
// 这是可选的
// 你可以用它来进行健康检查
return true
}
const healthCheck = getHealthHandler({ healthy, notHealthy, test: healthTest })
app.get(‘/health‘, healthCheck)
const sleep = (time: number) => new Promise((resolve) => setTimeout(resolve, time))
const asyncOperation = async () => sleep(3000).then(() => console.log(‘Async op done‘))
const closeServers = async () => {
await asyncOperation() // 可以是任何异步操作,例如 mongo db 关闭,或发送 slack 消息;)
server.close()
}
const gracePeriodSec = 5*1000
addGracefulShutdownHook(gracePeriodSec, closeServers)
server.addListener(‘close‘, () => console.log(‘shutdown after graceful period‘))
上面所示的这个简单的应用程序,添加了一个5秒的优雅关闭周期,在此之后,钩子(在关闭功能的帮助下负责关闭服务器)被触发。在发送 SIGINT 或 SIGTERM 信号时,用户可以看到5秒的宽限期,之后发生了3秒的等待异步操作,然后才会显示 “shutdown after graceful period” 的消息,表示关闭服务器。
该应用程序还展示了 “getHealthHandler” 的功能。在请求 localhost:3000/health 时,healthTest 将返回 true,并显示 ‘everything is great‘ 消息,表明 health 检查为正常。用户可以将 healthTest 改为返回 false,然后看到消息变为 ‘oh no, something bad happened!‘ 这表明了一种不健康的状态。
如果您使用 Koa 框架,请查看 demos/ 文件夹。 我们有一个 Koa 示例,其功能与上述应用类似。Koa 应用程序使用具有 health和 notHealthy 处理程序的 fn(ctx) 支持的 getHealthContextHandler,而不是将 health 和 notHealthy 处理程序作为 fn(req, res) 的 getHealthHandler。
正常关闭工作流程的工作方式示例:
Kubernetes 向 Pod 发送 SIGTERM 信号。手动缩小 Pod 或在滚动部署期间自动缩小 Pod 时会发生这种情况SIGTERM 信号并调用您的 notHealthy 处理程序。您的处理程序应返回 400 或 500 的 http 状态代码(抛出错误?),这表明该 pod 不再接收任何流量。注意此步骤是可选的(请检查下一步)5 到 20 秒之间。 kubernetes 端点控制器需要宽限时间才能从有效端点列表中删除 Pod,进而从服务中删除 Pod(从 iptables 所有节点中获取 pod 的 ip 地址)。Kubernetes 从 Service 中删除 PodNodeJS http server, express 和 Koa 不是我的微信:

Node.js & Kubernetes Graceful Shutdown
标签:表示 image 方法 lsh 步骤 context bad ejs 情况
原文地址:https://www.cnblogs.com/hacker-linner/p/14102809.html