{"id":20419458,"url":"https://github.com/risingstack/kubernetes-graceful-shutdown-example","last_synced_at":"2025-04-12T17:41:12.413Z","repository":{"id":80615615,"uuid":"71053748","full_name":"RisingStack/kubernetes-graceful-shutdown-example","owner":"RisingStack","description":"Example app for graceful start and stop with Kubernetes and Node.js","archived":false,"fork":false,"pushed_at":"2017-12-05T13:03:40.000Z","size":12,"stargazers_count":169,"open_issues_count":2,"forks_count":29,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-26T12:02:31.768Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RisingStack.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2016-10-16T13:58:47.000Z","updated_at":"2025-03-17T04:39:01.000Z","dependencies_parsed_at":null,"dependency_job_id":"56afdb41-35ff-407f-9455-a5541bf42024","html_url":"https://github.com/RisingStack/kubernetes-graceful-shutdown-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RisingStack%2Fkubernetes-graceful-shutdown-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RisingStack%2Fkubernetes-graceful-shutdown-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RisingStack%2Fkubernetes-graceful-shutdown-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RisingStack%2Fkubernetes-graceful-shutdown-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RisingStack","download_url":"https://codeload.github.com/RisingStack/kubernetes-graceful-shutdown-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248607791,"owners_count":21132609,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-15T06:37:13.457Z","updated_at":"2025-04-12T17:41:12.389Z","avatar_url":"https://github.com/RisingStack.png","language":"JavaScript","readme":"# kubernetes-graceful-shutdown-example\n\nExample repository to give a help with Kubernetes graceful start and shutdown in Node.js  \nRelated article: https://blog.risingstack.com/graceful-shutdown-node-js-kubernetes/\n\n## What it does?\n\n![Kubernetes graceful shutdown](https://cloud.githubusercontent.com/assets/1764512/19427956/7593df80-9447-11e6-9658-e593843a0f76.png)\n\n1. pod receives *SIGTERM* signal because Kubernetes wants to stop the it because of deploy, scale etc.\n2. App *(pod)* starts to return `500` for `GET /health` to let `readinessProbe` *(Service)* know that it's not ready to receive more requests.\n3. Kubernetes `readinessProbe` checks `GET /health` and after *(failureThreshold * periodSecond)* it stops redirecting traffic after to the app *(because it continuously returns 500)*\n4. App waits *(failureThreshold * periodSecond)* before starts shutdown, to being sure that Service is get notified via `readinessProbe` fail\n5. App starts graceful shutdown\n6. App first close server with live working DB connections\n7. App closes databases after the server is closed\n8. App exists process\n9. Kubernetes force kill application after 30s *(SIGKILL)* if it's still running *(in an optimal case it doesn't happen)*\n\nIn our case Kubernetes `livenessProbe` won't kill the app before graceful shutdown because needs to wait *(failureThreshold * periodSecond)* to do it, so `livenessProve` threshold should be larger than `readinessProbe` threshold *(graceful stop happens around 4s, force kill would happen 30s after SIGTERM)*\n\n## Benchmark\n\n### Test case\n\n```\n$ ab -n 100000 -c 20\n```\n\nPlus changing an environment variable in the `Deployment` to re-deploy all pods during the `ab` benchmarking.\n\n### AB output\n\n```\nDocument Path:          /\nDocument Length:        3 bytes\n\nConcurrency Level:      20\nTime taken for tests:   172.476 seconds\nComplete requests:      100000\nFailed requests:        0\nTotal transferred:      7800000 bytes\nHTML transferred:       300000 bytes\nRequests per second:    579.79 [#/sec] (mean)\nTime per request:       34.495 [ms] (mean)\nTime per request:       1.725 [ms] (mean, across all concurrent requests)\nTransfer rate:          44.16 [Kbytes/sec] received\n```\n\n### Application log output\n\n```\nGot SIGTERM. Graceful shutdown start 2016-10-16T18:54:59.208Z\nRequest after sigterm: / 2016-10-16T18:54:59.217Z\nRequest after sigterm: / 2016-10-16T18:54:59.261Z\n...\nRequest after sigterm: / 2016-10-16T18:55:00.064Z\nRequest after sigterm: /health?type=readiness 2016-10-16T18:55:00.820Z\nHEALTH: NOT OK\nRequest after sigterm: /health?type=readiness 2016-10-16T18:55:02.784Z\nHEALTH: NOT OK\nRequest after sigterm: /health?type=liveness 2016-10-16T18:55:04.781Z\nHEALTH: NOT OK\nRequest after sigterm: /health?type=readiness 2016-10-16T18:55:04.800Z\nHEALTH: NOT OK\nServer is shutting down... 2016-10-16T18:55:05.210Z\nSuccessful graceful shutdown 2016-10-16T18:55:05.212Z\n```\n\n### Benchmark result\n\n**Success!**\n\nZero failed requests: you can see in the app log that Service stopped sending traffic to the pod before we disconnected from db and killed the app.\n\n## Known issues\n\n### keep-alive\n\nKubernetes doesn't handover keep-alive connections.\nRequest from agents with keep-alive header will be still routed to the pod.\nIt's tricked me first when I benchmarked with [autocannon](https://github.com/mcollina/autocannon) or `Google Chrome`.\n\n### Docker signaling\n\n`CMD [\"node\", \"src\"]` works, `CMD [\"npm\", \"start\"]` not.\nIt doesn't pass the `SIGTERM` to the node process.\n\nAn alternative can be:\nhttps://github.com/Yelp/dumb-init\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frisingstack%2Fkubernetes-graceful-shutdown-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frisingstack%2Fkubernetes-graceful-shutdown-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frisingstack%2Fkubernetes-graceful-shutdown-example/lists"}