Lightship 🚢
Abstracts readiness/ liveness checks and graceful shutdown of Node.js services running in Kubernetes.
Behaviour
Creates a HTTP service used to check container probes.
Refer to the following Kubernetes documentation for information about the readiness and liveness checks:
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. This behaviour can be changed using detectKubernetes
and port
configuration.
/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.
Usage
Use createLightship
to create an instance of Lightship.
import {
createLightship
} from 'lightship';
const configuration: LightshipConfigurationType = {};
const lightship: LightshipType = createLightship(configuration);
The following types describe the configuration shape and the resulting Lightship instance interface.
type ShutdownHandlerType = () => Promise<void> | void;
export type LightshipConfigurationType = {|
+detectKubernetes?: boolean,
+port?: number,
+signals?: $ReadOnlyArray<string>,
+timeout?: number
|};
type LightshipType = {|
+server: http$Server,
+isServerReady: () => boolean,
+isServerShuttingDown: () => boolean,
+registerShutdownHandler: (shutdownHandler: ShutdownHandlerType) => void,
+shutdown: () => Promise<void>,
+signalNotReady: () => void,
+signalReady: () => void
|};
Kubernetes container probe configuration
This is an example of a reasonable container probe configuration to use with Lightship.
readinessProbe:
httpGet:
path: /ready
port: 9000
failureThreshold: 1
initialDelaySeconds: 5
periodSeconds: 5
successThreshold: 1
timeoutSeconds: 5
livenessProbe:
httpGet:
path: /live
port: 9000
failureThreshold: 3
initialDelaySeconds: 10
periodSeconds: 30
successThreshold: 1
timeoutSeconds: 5
Logging
lightship
is using Roarr to implement logging.
Set ROARR_LOG=true
environment variable to enable logging.
Usage examples
Using with Express.js
Suppose that you have Express.js application that simply respond "Hello, World!".
import express from 'express';
const app = express();
app.get('/', (req, res) => {
res.send('Hello, World!');
});
app.listen(8080);
To create a liveness and readiness check, simply create an instance of Lightship and use registerShutdownHandler
to register a server shutdown handler, e.g.
import express from 'express';
import {
createLightship
} from 'lightship';
const app = express();
app.get('/', (req, res) => {
res.send('Hello, World!');
});
const server = app.listen(8080);
const lightship = createLightship();
lightship.registerShutdownHandler(() => {
server.close();
});
lightship.signalReady();
Suppose that a requirement has been added that you need to ensure that you do not say "Hello, World!" more often than 100 times per minute.
Use signalNotReady
method to change server state to "SERVER_IS_NOT_READY" and use signalReady
to revert the server state to "SERVER_IS_READY".
import express from 'express';
import {
createLightship
} from 'lightship';
const app = express();
const minute = 60 * 1000;
let runningTotal = 0;
app.get('/', (req, res) => {
runningTotal++;
setTimeout(() => {
runningTotal--;
if (runningTotal < 100) {
lightship.signalReady();
} else {
lightship.signalNotReady();
}
}, minute);
res.send('Hello, World!');
});
const server = app.listen(8080);
const lightship = createLightship();
lightship.registerShutdownHandler(() => {
server.close();
});
lightship.signalReady();
How quick Kubernetes observes that the server state has changed depends on the probe configuration, specifically periodSeconds
, successThreshold
and failureThreshold
, i.e. expect requests to continue coming through for a while after the server state has changed.
Suppose that a requirement has been added that the server must shutdown after saying "Hello, World!" 1000 times.
Use shutdown
method to change server state to "SERVER_IS_SHUTTING_DOWN", e.g.
import express from 'express';
import delay from 'delay';
import {
createLightship
} from 'lightship';
const app = express();
const minute = 60 * 1000;
let total = 0;
let runningTotal = 0;
app.get('/', (req, res) => {
total++;
runningTotal++;
if (total === 1000) {
lightship.shutdown();
}
setTimeout(() => {
runningTotal--;
if (runningTotal < 100) {
lightship.signalReady();
} else {
lightship.signalNotReady();
}
}, minute);
res.send('Hello, World!');
});
const server = app.listen(8080);
const lightship = createLightship();
lightship.registerShutdownHandler(async () => {
await delay(minute);
server.close();
});
lightship.signalReady();
Do not call process.exit()
in a shutdown handler – Lighthouse calls process.exit()
after all registered shutdown handlers have run to completion.
If for whatever reason a registered shutdown handler hangs, then (subject to the Pod's restart policy) Kubernetes will forcefully restart the Container after the livenessProbe
deems the service to be failed.
FAQ
What is the reason for having separate /live
and /ready
endpoints?
Distinct endpoints are needed if you want your Container to be able to take itself down for maintenance (as done in the Using with Express.js usage example). Otherwise, you can use /health
.
Related projects
- Iapetus – Prometheus metrics server.
- Preoom – Retrieves & observes Kubernetes Pod resource (CPU, memory) utilisation.