-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,79 @@ | ||
# Kubernetes Probe Handlers | ||
# Kubernetes Probe Handlers | ||
|
||
This package has HTTP handlers for kubernetes liveness and readiness HTTP requests that can be easily added to any existing service. It also implements a Probe client if you want to make liveness/readiness checks against a service that implements a handler and finally it adds a server for responding to those checks for containerized applications that don't necessarily respond to HTTP requests. | ||
|
||
## Simple Usage | ||
|
||
If you have an `http.ServeMux` in your application, use that directly: | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
|
||
"go.rtnl.ai/x/probez" | ||
) | ||
|
||
|
||
func main() { | ||
// Create the serve mux and the probe handler | ||
mux := http.NewServeMux() | ||
probe := probez.New() | ||
|
||
// And the probe routes to the muxer | ||
probe.Mux(mux) | ||
|
||
// Add your other routes as necessary | ||
// ... | ||
|
||
// Serve the mux | ||
http.ListenAndServe(":8080", mux) | ||
} | ||
``` | ||
|
||
The probe starts in the healthy/not ready state. To modify the state of the probe, use `probe.Healthy()`, `probe.Unhealthy()`, `probe.Ready()`, `probe.NotReady()`. For example, if you have a server: | ||
|
||
```go | ||
type Server struct { | ||
probe *probez.Handler | ||
} | ||
|
||
func (s *Server) Serve() { | ||
// Set the probe to healthy | ||
s.server.Healthy() | ||
|
||
// Start the server up | ||
// ... | ||
|
||
// Perform database pings and other preparatory code | ||
// ... | ||
|
||
// Mark the server as ready for requests | ||
s.server.Ready() | ||
} | ||
|
||
func (s *Server) Shutdown() { | ||
// Set the server to not ready | ||
s.server.NotReady() | ||
|
||
// Perform server shutdown | ||
// ... | ||
|
||
// Mark the server as not healthy | ||
s.server.NotHealthy() | ||
} | ||
``` | ||
|
||
## Using with Gin | ||
|
||
Use `gin.WrapF` to utilize the probez handler with the gin web framework as follows: | ||
|
||
```go | ||
router := gin.Default() | ||
probe := probez.New() | ||
|
||
router.GET(probez.Livez, gin.Wrapf(probe.Healthz)) | ||
router.GET(probez.Healthz, gin.Wrapf(probe.Healthz)) | ||
router.GET(probez.Readyz, gin.Wrapf(probe.Readyz)) | ||
``` |