1
0
Fork 0

Rename health check URL parameter to path.

Also improve documentation.
This commit is contained in:
Timo Reimann 2017-03-14 01:22:08 +01:00 committed by Emile Vauge
parent f324983946
commit a507cb4835
No known key found for this signature in database
GPG key ID: D808B4C167352E59
4 changed files with 39 additions and 33 deletions

View file

@ -240,16 +240,22 @@ For example:
sticky = true sticky = true
``` ```
Healthcheck URL can be configured with a relative URL for `healthcheck.URL`. A health check can be configured in order to remove a backend from LB rotation
Interval between healthcheck can be configured by using `healthcheck.interval` as long as it keeps returning HTTP status codes other than 200 OK to HTTP GET
(default: 30s) 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: For example:
```toml ```toml
[backends] [backends]
[backends.backend1] [backends.backend1]
[backends.backend1.healthcheck] [backends.backend1.healthcheck]
URL = "/health" path = "/health"
interval = "10s" interval = "10s"
``` ```

View file

@ -25,11 +25,11 @@ func GetHealthCheck() *HealthCheck {
// BackendHealthCheck HealthCheck configuration for a backend // BackendHealthCheck HealthCheck configuration for a backend
type BackendHealthCheck struct { type BackendHealthCheck struct {
URL string Path string
Interval time.Duration Interval time.Duration
DisabledURLs []*url.URL DisabledURLs []*url.URL
requestTimeout time.Duration requestTimeout time.Duration
lb loadBalancer lb loadBalancer
} }
var launch = false var launch = false
@ -85,12 +85,12 @@ func (hc *HealthCheck) execute(ctx context.Context, backendID string, backend *B
checkBackend(backend) checkBackend(backend)
ticker := time.NewTicker(backend.Interval) ticker := time.NewTicker(backend.Interval)
defer ticker.Stop() defer ticker.Stop()
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
log.Debugf("Stopping all current Healthcheck goroutines") log.Debugf("Stopping all current Healthcheck goroutines")
return return
case <-ticker.C: case <-ticker.C:
log.Debugf("Refreshing healthcheck for currentBackend %s ", backendID) log.Debugf("Refreshing healthcheck for currentBackend %s ", backendID)
checkBackend(backend) checkBackend(backend)
} }
@ -98,33 +98,33 @@ func (hc *HealthCheck) execute(ctx context.Context, backendID string, backend *B
} }
func checkBackend(currentBackend *BackendHealthCheck) { func checkBackend(currentBackend *BackendHealthCheck) {
enabledURLs := currentBackend.lb.Servers() enabledURLs := currentBackend.lb.Servers()
var newDisabledURLs []*url.URL var newDisabledURLs []*url.URL
for _, url := range currentBackend.DisabledURLs { for _, url := range currentBackend.DisabledURLs {
if checkHealth(url, currentBackend) { if checkHealth(url, currentBackend) {
log.Debugf("HealthCheck is up [%s]: Upsert in server list", url.String()) log.Debugf("HealthCheck is up [%s]: Upsert in server list", url.String())
currentBackend.lb.UpsertServer(url, roundrobin.Weight(1)) currentBackend.lb.UpsertServer(url, roundrobin.Weight(1))
} else { } else {
log.Warnf("HealthCheck is still failing [%s]", url.String()) log.Warnf("HealthCheck is still failing [%s]", url.String())
newDisabledURLs = append(newDisabledURLs, url) newDisabledURLs = append(newDisabledURLs, url)
} }
} }
currentBackend.DisabledURLs = newDisabledURLs currentBackend.DisabledURLs = newDisabledURLs
for _, url := range enabledURLs { for _, url := range enabledURLs {
if !checkHealth(url, currentBackend) { if !checkHealth(url, currentBackend) {
log.Warnf("HealthCheck has failed [%s]: Remove from server list", url.String()) log.Warnf("HealthCheck has failed [%s]: Remove from server list", url.String())
currentBackend.lb.RemoveServer(url) currentBackend.lb.RemoveServer(url)
currentBackend.DisabledURLs = append(currentBackend.DisabledURLs, url) currentBackend.DisabledURLs = append(currentBackend.DisabledURLs, url)
} }
} }
} }
func checkHealth(serverURL *url.URL, backend *BackendHealthCheck) bool { func checkHealth(serverURL *url.URL, backend *BackendHealthCheck) bool {
client := http.Client{ client := http.Client{
Timeout: backend.requestTimeout, Timeout: backend.requestTimeout,
} }
resp, err := client.Get(serverURL.String() + backend.URL) resp, err := client.Get(serverURL.String() + backend.Path)
if err == nil { if err == nil {
defer resp.Body.Close() defer resp.Body.Close()
} }

View file

@ -667,7 +667,7 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
interval = time.Second * 30 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: case types.Wrr:
@ -701,7 +701,7 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
interval = time.Second * 30 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 maxConns := configuration.Backends[frontend.Backend].MaxConn

View file

@ -39,7 +39,7 @@ type CircuitBreaker struct {
// HealthCheck holds HealthCheck configuration // HealthCheck holds HealthCheck configuration
type HealthCheck struct { type HealthCheck struct {
URL string `json:"url,omitempty"` Path string `json:"path,omitempty"`
Interval string `json:"interval,omitempty"` Interval string `json:"interval,omitempty"`
} }