Blog Logo
TAGS

Lightship - Abstracts readiness, liveness, and startup checks and graceful shutdown of Node.js services in Kubernetes

# Lightship 🚢 [![Coveralls](https://img.shields.io/coveralls/gajus/lightship.svg?style=flat-square)](https://coveralls.io/github/gajus/lightship) [![NPM version](http://img.shields.io/npm/v/lightship.svg?style=flat-square)](https://www.npmjs.org/package/lightship) [![Canonical Code Style](https://img.shields.io/badge/code%20style-canonical-blue.svg?style=flat-square)](https://github.com/gajus/canonical) [![Twitter Follow](https://img.shields.io/twitter/follow/kuizinas.svg?style=social&label=Follow)](https://twitter.com/kuizinas) (Please read [Best practices](#best-practices) section.) Abstracts readiness, liveness and startup checks and graceful shutdown of Node.js services running in Kubernetes. {gitdown: contents} ## Behaviour Creates a HTTP service used to check [container probes](https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#container-probes). Refer to the following Kubernetes documentation for information about the readiness and liveness checks: [Pod Lifecycle](https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/) [Configure Liveness and Readiness Probes](https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/) ### Local-mode If Lightship detects that it is running in a non-Kubernetes environment (e.g. your local machine) then...* It starts the HTTP service on any available HTTP port. This is done to avoid port collision when multiple services using Lightship are being developed on the same machine. * `shutdownDelay` defaults to `0`, i.e. immediately proceed to execute the shutdown routine. Detection of the local-mode can be overridden by setting `{detectKubernetes: false}`. ### `/health` `/health` endpoint describes the current state of a Node.js service. The endpoint responds: * `200` status code, message SERVER_IS_READY when server is accepting new connections.* `500` status code, message SERVER_IS_NOT_READY when server is initialising.* `500` status code, message SERVER_IS_SHUTTING_DOWN when server is shutting down. Used for human inspection. ### `/live` The endpoint responds: * `200` status code, message SERVER_IS_NOT_SHUTTING_DOWN.* `500` status code, message SERVER_IS_SHUTTING_DOWN. Used to configure liveness probe. ### `/ready` The endpoint responds: * `200` status code, message SERVER_IS_READY.* `500` status code, message SERVER_IS_NOT_READY. Used to configure readiness probe. ### Timeouts Lightship has two timeout configurations: `gracefulShutdownTimeout` and `shutdownHandlerTimeout`. `gracefulShutdownTimeout` (default: 30 seconds) is a number of milliseconds Lightship waits for Node.js process to exit gracefully after it receives a shutdown signal (either via `process` or by calling `lightship.shutdown()`) before killing the process using `process.exit(1)`. This timeout should be sufficiently big to allow Node.js process to complete tasks (if any) that are active at the time that the shutdown signal is received (e.g. complete serving responses to all HTTP requests) (Note: You must explicitly inform Lightship about active tasks using [beacons](#beacons)). `shutdownHandlerTimeout` (default: 5 seconds) is a number of milliseconds Lightship waits for shutdown handlers (see `registerShutdownHandler`) to complete before killing the process using `process.exit(1)`. If after all beacons are dead and all shutdown handlers are resolved Node.js process does not exit gracefully, then Lightship will force terminate the process with an e