From d956737976d6c02967dd2b1d5af9d2eeaa9a4ead Mon Sep 17 00:00:00 2001 From: stack_underFlow Date: Mon, 11 May 2020 18:59:00 -0700 Subject: [PATCH] Adding liveness & readiness probe server endpoint for TalariaDB startup server (#22) * Adding liveliness & readiness probe server endpoint for TalariaDB startup server * config change * spell Co-authored-by: Venu Chitta --- deploy/application.yaml | 21 +++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ internal/config/config.go | 5 +++++ internal/config/sample_config.yaml | 4 +++- main.go | 26 ++++++++++++++++++++++++++ 6 files changed, 58 insertions(+), 1 deletion(-) diff --git a/deploy/application.yaml b/deploy/application.yaml index 4c5aa032..44015137 100644 --- a/deploy/application.yaml +++ b/deploy/application.yaml @@ -39,6 +39,9 @@ spec: protocol: TCP - containerPort: 7946 protocol: UDP + - name: liveness-port + containerPort: 8082 + hostPort: 8082 imagePullPolicy: Always env: - name: TALARIA_CONF_URI @@ -50,6 +53,24 @@ spec: volumeMounts: - mountPath: /data name: data + readinessProbe: + httpGet: + scheme: HTTP + path: /healthz + httpHeaders: + - name: x-talaria-readiness + value: healthz + port: liveness-port + initialDelaySeconds: 3 + livenessProbe: + httpGet: + scheme: HTTP + path: /healthz + httpHeaders: + - name: x-envoy-livenessprobe + value: healthz + port: liveness-port + initialDelaySeconds: 10 resources: requests: memory: "1000Mi" diff --git a/go.mod b/go.mod index 621ebc16..5bea47ec 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,7 @@ require ( github.com/gogo/protobuf v1.3.1 github.com/golang/snappy v0.0.1 github.com/gopherjs/gopherjs v0.0.0-20200209183636-89e6cbcd0b6d // indirect + github.com/gorilla/mux v1.7.4 github.com/grab/async v0.0.5 github.com/hako/durafmt v0.0.0-20191009132224-3f39dc1ed9f4 github.com/hashicorp/go-immutable-radix v1.1.0 // indirect diff --git a/go.sum b/go.sum index bd0babf3..06cd5671 100644 --- a/go.sum +++ b/go.sum @@ -153,6 +153,8 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gopherjs/gopherjs v0.0.0-20200209183636-89e6cbcd0b6d h1:vr95xIx8Eg3vCzZPxY3rCwTfkjqNDt/FgVqTOk0WByk= github.com/gopherjs/gopherjs v0.0.0-20200209183636-89e6cbcd0b6d/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= +github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/grab/async v0.0.5 h1:QESBFAKiq5ocTtIcRh8gdha5Xgvu0yStGUefZsOWLPc= github.com/grab/async v0.0.5/go.mod h1:8zY9m1tryEmU2px8GLmWrHt7QXSQOhyurytRQ3LrzjQ= github.com/hako/durafmt v0.0.0-20191009132224-3f39dc1ed9f4 h1:60gBOooTSmNtrqNaRvrDbi8VAne0REaek2agjnITKSw= diff --git a/internal/config/config.go b/internal/config/config.go index b00d9672..529c9ae0 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -22,6 +22,11 @@ type Config struct { Tables Tables `json:"tables" yaml:"tables" env:"TABLES"` Statsd *StatsD `json:"statsd,omitempty" yaml:"statsd" env:"STATSD"` Computed []Computed `json:"computed" yaml:"computed" env:"COMPUTED"` + K8s *K8s `json:"k8s,omitempty" yaml:"k8s" env:"K8S"` +} + +type K8s struct { + ProbePort int32 `json:"probePort" yaml:"probePort" env:"PROBEPORT"` // The port which is used for liveness and readiness probes (default: 8080) } // Tables is a list of table configs diff --git a/internal/config/sample_config.yaml b/internal/config/sample_config.yaml index d9825793..edbe72c3 100644 --- a/internal/config/sample_config.yaml +++ b/internal/config/sample_config.yaml @@ -26,4 +26,6 @@ tables: name: nodes statsd: host: "statsd-host" - port: 8126 \ No newline at end of file + port: 8126 +k8s: # Optional value to set in k8s env. when server need liveness & readiness probes + probePort: 8082 diff --git a/main.go b/main.go index 70fee024..9673eb30 100644 --- a/main.go +++ b/main.go @@ -5,11 +5,15 @@ package main import ( "context" + "fmt" + "net/http" "os" "os/signal" "syscall" "time" + "github.com/gorilla/mux" + "github.com/kelindar/lua" "github.com/kelindar/talaria/internal/config" "github.com/kelindar/talaria/internal/config/env" @@ -96,6 +100,11 @@ func main() { monitor.Info("server: joining cluster on %s...", conf.Domain) gossip.JoinHostname(conf.Domain) + // run HTTP server for readiness and liveness probes if k8s config is set + if conf.K8s != nil { + startHTTPServerAsync(conf.K8s.ProbePort) + } + // Start listen monitor.Info("server: starting...") monitor.Count1(logTag, "start") @@ -114,3 +123,20 @@ func onSignal(callback func(sig os.Signal)) { } }() } + +func startHTTPServerAsync(portNum int32) { + go func() { + handler := mux.NewRouter() + handler.HandleFunc("/healthz", func(resp http.ResponseWriter, req *http.Request) { + _, _ = resp.Write([]byte(`talaria-health-check`)) + }).Methods(http.MethodGet, http.MethodHead) + + server := &http.Server{ + Addr: fmt.Sprintf(":%d", portNum), + Handler: handler, + } + if err := server.ListenAndServe(); err != nil { + panic(err) + } + }() +}