Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Abstracts readiness/ liveness checks and graceful shutdown of Node.js services running in Kubernetes.
Abstracts readiness/ liveness checks and graceful shutdown of Node.js services running in Kubernetes.
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.
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.
/**
* A teardown function called when shutdown is initialized.
*/
type ShutdownHandlerType = () => Promise<void> | void;
/**
* @property port The port on which the Lightship service listens. This port must be different than your main service port, if any. The default port is 9000.
* @property signals An a array of [signal events]{@link https://nodejs.org/api/process.html#process_signal_events}. Default: [SIGTERM].
*/
type LightshipConfigurationType = {|
+port?: number,
+signals?: $ReadOnlyArray<string>
|};
/**
* @property registerShutdownHandler Registers teardown functions that are called when shutdown is initialized. All registered shutdown handlers are executed in the order they have been registered. After all shutdown handlers have been executed, Lightship asks `process.exit()` to terminate the process synchronously.
* @property shutdown Changes server state to SERVER_IS_SHUTTING_DOWN and initialises the shutdown of the application.
* @property signalNotReady Changes server state to SERVER_IS_NOT_READY.
* @property signalReady Changes server state to SERVER_IS_READY.
*/
type LightshipType = {|
+registerShutdownHandler: (shutdownHandler: ShutdownHandlerType) => void,
+shutdown: () => Promise<void>,
+signalNotReady: () => void,
+signalReady: () => void
|};
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
# Allow sufficient amount of time (180 seconds = periodSeconds * failureThreshold)
# for the registered shutdown handlers to run to completion.
periodSeconds: 30
failureThreshold: 3
successThreshold: 1
lightship
is using Roarr to implement logging.
Set ROARR_LOG=true
environment variable to enable logging.
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 default state is "SERVER_IS_NOT_READY". Therefore, you must signal
// that the server is now ready to accept connections.
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 default state is "SERVER_IS_NOT_READY". Therefore, you must signal
// that the server is now ready to accept connections.
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 () => {
// Allow sufficient amount of time to allow all of the existing
// HTTP requests to finish before terminating the service.
await delay(minute);
server.close();
});
// Lightship default state is "SERVER_IS_NOT_READY". Therefore, you must signal
// that the server is now ready to accept connections.
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.
/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
.
FAQs
Abstracts readiness, liveness and startup checks and graceful shutdown of Node.js services running in Kubernetes.
The npm package lightship receives a total of 47,770 weekly downloads. As such, lightship popularity was classified as popular.
We found that lightship demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.