Skip to content

Commit

Permalink
Adding liveness & readiness probe server endpoint for TalariaDB start…
Browse files Browse the repository at this point in the history
…up server (#22)

* Adding liveliness & readiness probe server endpoint for TalariaDB startup server

* config change

* spell

Co-authored-by: Venu Chitta <[email protected]>
  • Loading branch information
venuchitta and Venu Chitta committed May 12, 2020
1 parent 13b5e10 commit d956737
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
21 changes: 21 additions & 0 deletions deploy/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
5 changes: 5 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion internal/config/sample_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ tables:
name: nodes
statsd:
host: "statsd-host"
port: 8126
port: 8126
k8s: # Optional value to set in k8s env. when server need liveness & readiness probes
probePort: 8082
26 changes: 26 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand All @@ -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)
}
}()
}

0 comments on commit d956737

Please sign in to comment.