-
Install with go get
or your favorite Go dependency manager: go get -u github.com/heptiolabs/healthcheck
-
Import the package: import "github.com/heptiolabs/healthcheck"
-
Create a healthcheck.Handler
:
health := healthcheck.NewHandler()
-
Configure some application-specific liveness checks (whether the app itself is unhealthy):
health.AddLivenessCheck("goroutine-threshold", healthcheck.GoroutineCountCheck(100))
-
Configure some application-specific readiness checks (whether the app is ready to serve requests):
health.AddReadinessCheck(
"upstream-dep-dns",
healthcheck.DNSResolveCheck("upstream.example.com", 50*time.Millisecond))
health.AddReadinessCheck("database", healthcheck.DatabasePingCheck(db, 1*time.Second))
-
Expose the /live
and /ready
endpoints over HTTP (on port 8086):
go http.ListenAndServe("0.0.0.0:8086", health)
-
Configure your Kubernetes container with HTTP liveness and readiness probes see the (Kubernetes documentation) for more detail:
apiVersion: v1
kind: Pod
metadata:
name: heptio-healthcheck-example
spec:
containers:
- name: liveness
image: your-registry/your-container
livenessProbe:
httpGet:
path: /live
port: 8086
initialDelaySeconds: 5
periodSeconds: 5
readinessProbe:
httpGet:
path: /ready
port: 8086
periodSeconds: 5
-
If one of your readiness checks fails, Kubernetes will stop routing traffic to that pod within a few seconds (depending on periodSeconds
and other factors).
-
If one of your liveness checks fails or your app becomes totally unresponsive, Kubernetes will restart your container.