Page
Add health checks to your application
Kubernetes and a number of other cloud deployment technologies, provide health checking as a system that allows the cloud deployment technology to monitor the deployed application and to take action should the application fail or report itself as "unhealthy."
The simplest form of health check is process-level health checking, where Kubernetes checks to see if the application process still exists and restarts the container (and therefore the application process) if it is not. This provides a basic restart capability, but does not handle scenarios where the application exists but is unresponsive, or where it would be desirable to restart the application for other reasons.
The next level of health check is HTTP-based, where the application exposes a "livenessProbe" URL endpoint that Kubernetes can make requests to determine whether the application is running and responsive. Additionally, the request can be used to drive self-checking capabilities in the application.
In order to get full benefit from taking this lesson, you need:
- The application that you created in the previous lesson.
- An environment where you can install and run a Node.js application.
In this lesson, you will:
- Add health checks to your Express.js application.
Add health checks to your application
Using our previously created server.js, we are going to add a liveness health check endpoint called /live.
app.get('/live', (req, res) => res.status(200).json({ status: 'ok' }));
Add this line after the app.use(helmet()); line. This adds a /live endpoint to your application. As no liveness checks are registered, it will return a status code of 200 OK and a JSON payload of {"status":"ok"}
.
Now, restart your application.
npm start
Check that your livenessProbe health check endpoint is running by visiting the live endpoint http://localhost:8080/live.
You can stop your server by entering Ctrl + C in your terminal window.
And you’re done! You have successfully added a health check to your application. For more information on health/liveness checks, refer to the following articles:
- NodeShift Reference Architecture for Node.js Applications - Health Checks
- You (probably) need liveness and readiness probes
In the next lesson, you will set up metrics-based monitoring for your application.