From ffd55152e57aeccb76b9cb7aaa178210da59db62 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Tue, 26 Sep 2017 11:10:51 +0800 Subject: [PATCH] add healthz check (#286) * add healthz check Signed-off-by: Bo-Yi Wu * add testing Signed-off-by: Bo-Yi Wu --- gorush/server.go | 5 +++++ gorush/server_test.go | 10 ++++++++++ k8s/gorush-deployment.yaml | 6 ++++++ k8s/gorush-service.yaml | 10 ++++++---- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/gorush/server.go b/gorush/server.go index eb68bf3..58c8b95 100644 --- a/gorush/server.go +++ b/gorush/server.go @@ -32,6 +32,10 @@ func rootHandler(c *gin.Context) { }) } +func heartbeatHandler(c *gin.Context) { + c.AbortWithStatus(200) +} + func pushHandler(c *gin.Context) { var form RequestPush var msg string @@ -107,6 +111,7 @@ func routerEngine() *gin.Engine { r.GET(PushConf.API.SysStatURI, sysStatsHandler) r.POST(PushConf.API.PushURI, pushHandler) r.GET(PushConf.API.MetricURI, metricsHandler) + r.GET("/healthz", heartbeatHandler) r.GET("/", rootHandler) return r diff --git a/gorush/server_test.go b/gorush/server_test.go index 6515b3e..233bb47 100644 --- a/gorush/server_test.go +++ b/gorush/server_test.go @@ -258,6 +258,16 @@ func TestMetricsHandler(t *testing.T) { }) } +func TestHeartbeatHandler(t *testing.T) { + initTest() + + r := gofight.New() + + r.GET("/healthz"). + Run(routerEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { + assert.Equal(t, http.StatusOK, r.Code) + }) +} func TestDisabledHTTPServer(t *testing.T) { initTest() PushConf.Core.Enabled = false diff --git a/k8s/gorush-deployment.yaml b/k8s/gorush-deployment.yaml index 1f6cd22..5b923b5 100644 --- a/k8s/gorush-deployment.yaml +++ b/k8s/gorush-deployment.yaml @@ -19,3 +19,9 @@ spec: args: ["-c", "config.yml", "-e", "redis", "--redis-addr", "redis:6379"] ports: - containerPort: 8088 + livenessProbe: + httpGet: + path: /healthz + port: 8088 + initialDelaySeconds: 3 + periodSeconds: 3 diff --git a/k8s/gorush-service.yaml b/k8s/gorush-service.yaml index f63f11d..6fe818b 100644 --- a/k8s/gorush-service.yaml +++ b/k8s/gorush-service.yaml @@ -7,12 +7,14 @@ metadata: app: gorush tier: frontend spec: + selector: + app: gorush + tier: frontend # if your cluster supports it, uncomment the following to automatically create # an external load-balanced IP for the frontend service. type: LoadBalancer # type: NodePort ports: - - port: 8088 - selector: - app: gorush - tier: frontend + - protocol: TCP + port: 80 + targetPort: 8088