Kubernetes - Health Checks
Kubernetes - Health Checks
ianlewis.org
Liveness Probes
http.HandleFunc("/healthz", func(w
http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
1 of 5 11/28/18, 10:32 AM
Using Kubernetes Health Checks - Ian Lewis about:reader?url=https://www.ianlewis.org/en/u...
}
http.ListenAndServe(":8080", nil)
livenessProbe:
# an http probe
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 15
timeoutSeconds: 1
Readiness Probes
2 of 5 11/28/18, 10:32 AM
Using Kubernetes Health Checks - Ian Lewis about:reader?url=https://www.ianlewis.org/en/u...
If the readiness probe for your app fails, then that pod is
removed from the endpoints that make up a service. This
makes it so that pods that are not ready will not have traffic
sent to them by Kubernetes' service discovery mechanism.
This is really helpful for situations where a new pod for a
service gets started; scale up events, rolling updates, etc.
Readiness probes make sure that pods are not sent traffic
in the time between when they start up, and and when they
are ready to serve traffic.
readinessProbe:
# an http probe
httpGet:
path: /readiness
port: 8080
initialDelaySeconds: 20
timeoutSeconds: 5
You will want to check that you can connect to all of your
application's dependencies in your readiness probe. To use
the example where we depend on a database and
3 of 5 11/28/18, 10:32 AM
Using Kubernetes Health Checks - Ian Lewis about:reader?url=https://www.ianlewis.org/en/u...
http.HandleFunc("/readiness", func(w
http.ResponseWriter, r *http.Request) {
ok := true
errMsg = ""
// Check memcache
if mc != nil {
err := mc.Set(&memcache.Item{Key:
"healthz", Value: []byte("test")})
}
if mc == nil || err != nil {
ok = false
errMsg += "Memcached not ok.¥n"
}
// Check database
if db != nil {
_, err := db.Query("SELECT 1;")
}
if db == nil || err != nil {
ok = false
errMsg += "Database not ok.¥n"
}
4 of 5 11/28/18, 10:32 AM
Using Kubernetes Health Checks - Ian Lewis about:reader?url=https://www.ianlewis.org/en/u...
if ok {
w.Write([]byte("OK"))
} else {
// Send 503
http.Error(w, errMsg,
http.StatusServiceUnavailable)
}
})
http.ListenAndServe(":8080", nil)
5 of 5 11/28/18, 10:32 AM