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:
/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;
type LightshipConfigurationType = {|
+port?: number,
+signals?: $ReadOnlyArray<string>
|};
type LightshipType = {|
+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
initialDelaySeconds: 5
periodSeconds: 5
failureThreshold: 1
successThreshold: 1
livenessProbe:
httpGet:
path: /live
port: 9000
initialDelaySeconds: 10
periodSeconds: 30
failureThreshold: 3
successThreshold: 1
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 checks, simply create an instance of Lightship and use onShutdown
hook to shutdown your server, 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({
onShutdown: () => {
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({
onShutdown: () => {
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 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
.