Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat sicedar: added readiness prober #1395

Merged
merged 7 commits into from
Aug 28, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ We use *breaking* word for marking changes that are not backward compatible (rel

- [#1358](https://github.com/thanos-io/thanos/pull/1358) Added `part_size` configuration option for HTTP multipart requests minimum part size for S3 storage type
- [#1363](https://github.com/thanos-io/thanos/pull/1363) Thanos Receive now exposes `thanos_receive_hashring_nodes` and `thanos_receive_hashring_tenants` metrics to monitor status of hash-rings
- [#1395](https://github.com/thanos-io/thanos/pull/1395) Added `/-/ready` and `/-/healthy` endpoints to Thanos sidecar.

### Changed

Expand Down
13 changes: 6 additions & 7 deletions cmd/thanos/compact.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ func (cs compactionSet) maxLevel() int {
}

func registerCompact(m map[string]setupFunc, app *kingpin.Application) {
comp := component.Compact
cmd := app.Command(comp.String(), "continuously compacts blocks in an object store bucket")
cmd := app.Command(component.Compact.String(), "continuously compacts blocks in an object store bucket")

haltOnError := cmd.Flag("debug.halt-on-error", "Halt the process if a critical compaction error is detected.").
Hidden().Default("true").Bool()
Expand Down Expand Up @@ -112,7 +111,7 @@ func registerCompact(m map[string]setupFunc, app *kingpin.Application) {
compactionConcurrency := cmd.Flag("compact.concurrency", "Number of goroutines to use when compacting groups.").
Default("1").Int()

m[comp.String()] = func(g *run.Group, logger log.Logger, reg *prometheus.Registry, tracer opentracing.Tracer, _ bool) error {
m[component.Compact.String()] = func(g *run.Group, logger log.Logger, reg *prometheus.Registry, tracer opentracing.Tracer, _ bool) error {
return runCompact(g, logger, reg,
*httpAddr,
*dataDir,
Expand All @@ -127,7 +126,7 @@ func registerCompact(m map[string]setupFunc, app *kingpin.Application) {
compact.ResolutionLevel5m: time.Duration(*retention5m),
compact.ResolutionLevel1h: time.Duration(*retention1h),
},
comp,
component.Compact,
*disableDownsampling,
*maxCompactionLevel,
*blockSyncConcurrency,
Expand Down Expand Up @@ -170,9 +169,9 @@ func runCompact(

downsampleMetrics := newDownsampleMetrics(reg)

readinessProber := prober.NewProber(component, logger, prometheus.WrapRegistererWithPrefix("thanos_", reg))
statusProber := prober.NewProber(component, logger, prometheus.WrapRegistererWithPrefix("thanos_", reg))
// Initiate default HTTP listener providing metrics endpoint and readiness/liveness probes.
if err := defaultHTTPListener(g, logger, reg, httpBindAddr, readinessProber); err != nil {
if err := defaultHTTPListener(g, logger, reg, httpBindAddr, statusProber); err != nil {
return errors.Wrap(err, "create readiness prober")
}

Expand Down Expand Up @@ -327,7 +326,7 @@ func runCompact(
})

level.Info(logger).Log("msg", "starting compact node")
readinessProber.SetReady()
statusProber.SetReady()
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/thanos/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func main() {
tracingConfig := regCommonTracingFlags(app)

cmds := map[string]setupFunc{}
registerSidecar(cmds, app, "sidecar")
registerSidecar(cmds, app)
registerStore(cmds, app, "store")
registerQuery(cmds, app, "query")
registerRule(cmds, app, "rule")
Expand Down
42 changes: 34 additions & 8 deletions cmd/thanos/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,29 @@ import (
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/oklog/run"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/prometheus/tsdb/labels"
"github.com/thanos-io/thanos/pkg/block/metadata"
"github.com/thanos-io/thanos/pkg/component"
"github.com/thanos-io/thanos/pkg/objstore/client"
"github.com/thanos-io/thanos/pkg/prober"
"github.com/thanos-io/thanos/pkg/promclient"
"github.com/thanos-io/thanos/pkg/reloader"
"github.com/thanos-io/thanos/pkg/runutil"
"github.com/thanos-io/thanos/pkg/shipper"
"github.com/thanos-io/thanos/pkg/store"
"github.com/thanos-io/thanos/pkg/store/storepb"
"google.golang.org/grpc"
kingpin "gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/alecthomas/kingpin.v2"
)

const waitForExternalLabelsTimeout = 10 * time.Minute

func registerSidecar(m map[string]setupFunc, app *kingpin.Application, name string) {
cmd := app.Command(name, "sidecar for Prometheus server")
func registerSidecar(m map[string]setupFunc, app *kingpin.Application) {
cmd := app.Command(component.Sidecar.String(), "sidecar for Prometheus server")

grpcBindAddr, httpBindAddr, cert, key, clientCA := regCommonServerFlags(cmd)

Expand All @@ -54,7 +55,7 @@ func registerSidecar(m map[string]setupFunc, app *kingpin.Application, name stri

uploadCompacted := cmd.Flag("shipper.upload-compacted", "[Experimental] If true sidecar will try to upload compacted blocks as well. Useful for migration purposes. Works only if compaction is disabled on Prometheus.").Default("false").Hidden().Bool()

m[name] = func(g *run.Group, logger log.Logger, reg *prometheus.Registry, tracer opentracing.Tracer, _ bool) error {
m[component.Sidecar.String()] = func(g *run.Group, logger log.Logger, reg *prometheus.Registry, tracer opentracing.Tracer, _ bool) error {
rl := reloader.New(
log.With(logger, "component", "reloader"),
reloader.ReloadURLFromBase(*promURL),
Expand All @@ -77,6 +78,7 @@ func registerSidecar(m map[string]setupFunc, app *kingpin.Application, name stri
objStoreConfig,
rl,
*uploadCompacted,
component.Sidecar,
)
}
}
Expand All @@ -96,6 +98,7 @@ func runSidecar(
objStoreConfig *pathOrContent,
reloader *reloader.Reloader,
uploadCompacted bool,
comp component.Component,
) error {
var m = &promMetadata{
promURL: promURL,
Expand All @@ -117,6 +120,12 @@ func runSidecar(
uploads = false
}

statusProber := prober.NewProber(comp, logger, prometheus.WrapRegistererWithPrefix("thanos_", reg))
// Initiate default HTTP listener providing metrics endpoint and readiness/liveness probes.
if err := defaultHTTPListener(g, logger, reg, httpBindAddr, statusProber); err != nil {
return errors.Wrap(err, "create readiness prober")
}

// Setup all the concurrent groups.
{
promUp := prometheus.NewGauge(prometheus.GaugeOpts{
Expand All @@ -139,6 +148,12 @@ func runSidecar(
}
}

// When the heartbeat to Prometheus fails, the sidecar is marked as not ready.
// But after `sinceLastSuccessfulHeartbeatLimit` duration of consequential fails it's marked also not healthy,
// so the orchestrator (if any) can try restarting it if it would help.
sinceLastSuccessfulHeartbeat := 0 * time.Minute
FUSAKLA marked this conversation as resolved.
Show resolved Hide resolved
sinceLastSuccessfulHeartbeatLimit := 3 * time.Minute

// Blocking query of external labels before joining as a Source Peer into gossip.
// We retry infinitely until we reach and fetch labels from our Prometheus.
err := runutil.Retry(2*time.Second, ctx.Done(), func() error {
Expand All @@ -148,6 +163,10 @@ func runSidecar(
"err", err,
)
promUp.Set(0)
statusProber.SetNotReady(err)
if sinceLastSuccessfulHeartbeat >= sinceLastSuccessfulHeartbeatLimit {
statusProber.SetNotHealthy(err)
FUSAKLA marked this conversation as resolved.
Show resolved Hide resolved
}
return err
}

Expand All @@ -156,6 +175,8 @@ func runSidecar(
"external_labels", m.Labels().String(),
)
promUp.Set(1)
statusProber.SetReady()
sinceLastSuccessfulHeartbeat = 0
lastHeartbeat.Set(float64(time.Now().UnixNano()) / 1e9)
return nil
})
Expand All @@ -176,8 +197,15 @@ func runSidecar(
if err := m.UpdateLabels(iterCtx, logger); err != nil {
level.Warn(logger).Log("msg", "heartbeat failed", "err", err)
promUp.Set(0)
statusProber.SetNotReady(err)
if sinceLastSuccessfulHeartbeat >= sinceLastSuccessfulHeartbeatLimit {
statusProber.SetNotHealthy(err)
}
sinceLastSuccessfulHeartbeat = 0
} else {
promUp.Set(1)
statusProber.SetReady()
sinceLastSuccessfulHeartbeat = 0
lastHeartbeat.Set(float64(time.Now().UnixNano()) / 1e9)
}

Expand All @@ -195,9 +223,7 @@ func runSidecar(
cancel()
})
}
if err := metricHTTPListenGroup(g, logger, reg, httpBindAddr); err != nil {
return err
}

{
l, err := net.Listen("tcp", grpcBindAddr)
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions pkg/prober/prober.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ type Prober struct {
func NewProber(component component.Component, logger log.Logger, reg prometheus.Registerer) *Prober {
initialErr := fmt.Errorf(initialErrorFmt, component)

// From Kubernetes documentation https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/ :
FUSAKLA marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit but this should part of struct TBH (for GoDocs)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, is it ok this way?

//
// liveness: Many applications running for long periods of time eventually transition to broken states,
// (healthy) and cannot recover except by being restarted.
// Kubernetes provides liveness probes to detect and remedy such situations.
//
// readiness: Sometimes, applications are temporarily unable to serve traffic.
// (ready) For example, an application might need to load large data or configuration files during startup,
// or depend on external services after startup. In such cases, you don’t want to kill the application,
// but you don’t want to send it requests either. Kubernetes provides readiness probes to detect
// and mitigate these situations. A pod with containers reporting that they are not ready
// does not receive traffic through Kubernetes Services.

p := &Prober{
component: component,
logger: logger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ spec:
containerPort: 10902
- name: grpc
containerPort: 10901
livenessProbe:
httpGet:
port: 10902
path: /-/healthy
readinessProbe:
httpGet:
port: 10902
path: /-/ready
volumeMounts:
- name: prometheus
mountPath: /var/prometheus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ spec:
containerPort: 10902
- name: grpc
containerPort: 10901
livenessProbe:
httpGet:
port: 10902
path: /-/healthy
readinessProbe:
httpGet:
port: 10902
path: /-/ready
volumeMounts:
- name: prometheus
mountPath: /var/prometheus
Expand Down