From de91b99639a59d2150095231e6ba6ad3bb107ed8 Mon Sep 17 00:00:00 2001 From: Timo Reimann Date: Tue, 14 Mar 2017 01:22:08 +0100 Subject: [PATCH] Rename health check URL parameter to path. Also improve documentation. --- docs/basics.md | 14 ++++++++++---- healthcheck/healthcheck.go | 10 +++++----- server.go | 4 ++-- types/types.go | 2 +- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/docs/basics.md b/docs/basics.md index 58a74ee51..25dd5d6f5 100644 --- a/docs/basics.md +++ b/docs/basics.md @@ -236,16 +236,22 @@ For example: sticky = true ``` -Healthcheck URL can be configured with a relative URL for `healthcheck.URL`. -Interval between healthcheck can be configured by using `healthcheck.interval` -(default: 30s) +A health check can be configured in order to remove a backend from LB rotation +as long as it keeps returning HTTP status codes other than 200 OK to HTTP GET +requests periodically carried out by Traefik. The check is defined by a path +appended to the backend URL and an interval (given in a format understood by [time.ParseDuration](https://golang.org/pkg/time/#ParseDuration)) specifying how +often the health check should be executed (the default being 30 seconds). Each +backend must respond to the health check within 5 seconds. + +A recovering backend returning 200 OK responses again is being returned to the +LB rotation pool. For example: ```toml [backends] [backends.backend1] [backends.backend1.healthcheck] - URL = "/health" + path = "/health" interval = "10s" ``` diff --git a/healthcheck/healthcheck.go b/healthcheck/healthcheck.go index 30b4af495..4a178ac7d 100644 --- a/healthcheck/healthcheck.go +++ b/healthcheck/healthcheck.go @@ -25,7 +25,7 @@ func GetHealthCheck() *HealthCheck { // BackendHealthCheck HealthCheck configuration for a backend type BackendHealthCheck struct { - URL string + Path string Interval time.Duration DisabledURLs []*url.URL lb loadBalancer @@ -81,7 +81,7 @@ func (hc *HealthCheck) execute(ctx context.Context) { enabledURLs := currentBackend.lb.Servers() var newDisabledURLs []*url.URL for _, url := range currentBackend.DisabledURLs { - if checkHealth(url, currentBackend.URL) { + if checkHealth(url, currentBackend.Path) { log.Debugf("HealthCheck is up [%s]: Upsert in server list", url.String()) currentBackend.lb.UpsertServer(url, roundrobin.Weight(1)) } else { @@ -91,7 +91,7 @@ func (hc *HealthCheck) execute(ctx context.Context) { currentBackend.DisabledURLs = newDisabledURLs for _, url := range enabledURLs { - if !checkHealth(url, currentBackend.URL) { + if !checkHealth(url, currentBackend.Path) { log.Debugf("HealthCheck has failed [%s]: Remove from server list", url.String()) currentBackend.lb.RemoveServer(url) currentBackend.DisabledURLs = append(currentBackend.DisabledURLs, url) @@ -104,12 +104,12 @@ func (hc *HealthCheck) execute(ctx context.Context) { } } -func checkHealth(serverURL *url.URL, checkURL string) bool { +func checkHealth(serverURL *url.URL, path string) bool { timeout := time.Duration(5 * time.Second) client := http.Client{ Timeout: timeout, } - resp, err := client.Get(serverURL.String() + checkURL) + resp, err := client.Get(serverURL.String() + path) if err != nil || resp.StatusCode != 200 { return false } diff --git a/server.go b/server.go index 457093eb2..2ee11581c 100644 --- a/server.go +++ b/server.go @@ -666,7 +666,7 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo interval = time.Second * 30 } } - backendsHealthcheck[frontend.Backend] = healthcheck.NewBackendHealthCheck(configuration.Backends[frontend.Backend].HealthCheck.URL, interval, rebalancer) + backendsHealthcheck[frontend.Backend] = healthcheck.NewBackendHealthCheck(configuration.Backends[frontend.Backend].HealthCheck.Path, interval, rebalancer) } } case types.Wrr: @@ -700,7 +700,7 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo interval = time.Second * 30 } } - backendsHealthcheck[frontend.Backend] = healthcheck.NewBackendHealthCheck(configuration.Backends[frontend.Backend].HealthCheck.URL, interval, rr) + backendsHealthcheck[frontend.Backend] = healthcheck.NewBackendHealthCheck(configuration.Backends[frontend.Backend].HealthCheck.Path, interval, rr) } } maxConns := configuration.Backends[frontend.Backend].MaxConn diff --git a/types/types.go b/types/types.go index 308a71e25..2fa59f2e1 100644 --- a/types/types.go +++ b/types/types.go @@ -39,7 +39,7 @@ type CircuitBreaker struct { // HealthCheck holds HealthCheck configuration type HealthCheck struct { - URL string `json:"url,omitempty"` + Path string `json:"path,omitempty"` Interval string `json:"interval,omitempty"` }