Implement liveness and readiness probes by exposing two HTTP endpoints that return 200 OK when the application is healthy or ready to serve traffic. Add handlers for /healthz (liveness) and /readyz (readiness) in your main package, then configure your container orchestrator to call them.
package main
import (
"log"
"net/http"
)
func main() {
// Liveness probe: returns 200 if the app is running
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
// Readiness probe: returns 200 if the app is ready to accept traffic
http.HandleFunc("/readyz", func(w http.ResponseWriter, r *http.Request) {
// Add logic here to check dependencies (DB, cache, etc.)
w.WriteHeader(http.StatusOK)
})
log.Println("Server starting on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
In your Kubernetes deployment YAML, configure the probes:
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
readinessProbe:
httpGet:
path: /readyz
port: 8080
initialDelaySeconds: 5
periodSeconds: 5