dongxing2015 2018-03-25 22:54
浏览 72
已采纳

http.HandleFunc不起作用

I am working on an application that is hosted on Kubernetes on google cloud, it uses Ingress to manage the routing to each service, then inside each service there's some extra routing using golang.

My problem is: when I try sending an API request to /api/auth/healthz I am expecting to reach the statusHandler() function, instead I always hit the requestHandler() function, even though I have an http.HandleFunc in there: http.HandleFunc("/healthz", statusHandler)

Here's my code in go

func main() {
    flag.Parse()

    // initialize the "database"
    err := store.Initialise()
    if err != nil {
        glog.Error(err)
        glog.Error("Store failed to initialise")
    }

    defer store.Uninitialise()

    // Initialize the default TTL for JWT access tokens to 1 day
    tokenTTL = 24 * time.Hour
    glog.Infof("JWT access tokens time-to-live set to: %s", tokenTTL.String())

    http.HandleFunc("/healthz", statusHandler)
    http.HandleFunc("/", requestHandler)

    glog.Infof("Authentication service started on port 8080...
")
    http.ListenAndServe(":8080", nil)
}

func statusHandler(w http.ResponseWriter, r *http.Request) {
    glog.Infof("Reached the status handler")
    statusObj := serviceStatus{
        GitSHA:         os.Getenv("GIT_SHA"),
        BuildTimestamp: os.Getenv("BUILD_TIMESTAMP"),
        DeployEnv:      os.Getenv("DEPLOY_ENV"),
    }
    statusJSON, _ := json.Marshal(statusObj)

    w.Header().Set("Content-Type", "application/json")
    fmt.Fprintf(w, "%s", statusJSON)
}

func requestHandler(w http.ResponseWriter, r *http.Request) {
    glog.Infof("Reached the request handler")
    ...
}

And here's my Ingress code:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  # https://github.com/kubernetes/ingress-nginx/blob/master/docs/user-guide/annotations.md
  annotations:
    kubernetes.io/ingress.class: nginx
    certmanager.k8s.io/cluster-issuer: letsencrypt-staging
    nginx.ingress.kubernetes.io/enable-cors: "false"
    nginx.ingress.kubernetes.io/cors-enable-origin: "*"
    nginx.ingress.kubernetes.io/auth-type: "basic"
    nginx.ingress.kubernetes.io/auth-secret: "ci-ingress-auth"
    nginx.ingress.kubernetes.io/proxy-body-size: "4g"
  name: ci-api-ingress
  namespace: ci
spec:
  rules:
  - host: ci.omitted.io
    http:
      paths:
      - backend:
          serviceName: auth-service << This is the service I am working on
          servicePort: 8080
        path: /api/auth
      - backend:
          serviceName: data-import-service
          servicePort: 8080
        path: /api/data-import
      - backend:
          serviceName: log-service
          servicePort: 8080
        path: /api/log
      - backend:
          serviceName: project-service
          servicePort: 8080
        path: /api/project
      - backend:
          serviceName: orchestration-service
          servicePort: 8080
        path: /api/orchestration
      - backend:
          serviceName: public
          servicePort: 80
        path: /

Based on Kubernetes' documentation that should work as what they have in there is something similar to my implementation:

http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
    duration := time.Now().Sub(started)
    if duration.Seconds() > 10 {
        w.WriteHeader(500)
        w.Write([]byte(fmt.Sprintf("error: %v", duration.Seconds())))
    } else {
        w.WriteHeader(200)
        w.Write([]byte("ok"))
    }
})
  • 写回答

1条回答 默认 最新

  • donglang5157 2018-03-25 22:59
    关注

    based on your ingress rules the path for requests should be: /api/auth/healthz not /api/auth-service/healthz

    The ingress path /api/auth/ is kept in the request uri passed to the application server.

    Adding rewrite-target to the ingress annotations will ensure the path is passed to your underlying server as you'd expect e.g. as /<path> rather than /api/auth/<path>. Inspecting the application server logs should show this is the case.

    ingress.kubernetes.io/rewrite-target: /

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误