Motivation
The examples in this post are for a TCP health check see Health Checks for detailed motivation and alternatives like HTTP.
“Kubernetes relies on probes in your application to assess whether your application is healthy.” - dzone.com
If the worker instance is un-healthy then K8s will tear it down and spin up a new instance.
Code Example
Install the following package :
Microsoft.Extensions.Diagnostics.HealthChecks
Create a simple health check that implements IHealthCheck and always returns healthy.
1 | using Microsoft.Extensions.Diagnostics.HealthChecks; |
- Create a new background service to accept the TCP socket liveness probe, this code was adapted from dzone.com and is intentionally simple without logging and error handling. So turn piss off Karen 😛
1 | public class TcpLivenessProbeService : BackgroundService |
- Now resolve the dependencies in Program.cs
1 | // Liveness check |
- You will then need to configure the liveness probe in K8s to query port 5000
1 | spec: |