【问题标题】:http.HandleFunc not workinghttp.HandleFunc 不工作
【发布时间】:2018-09-04 00:42:41
【问题描述】:

我正在开发一个托管在谷歌云上的 Kubernetes 上的应用程序,它使用 Ingress 来管理到每个服务的路由,然后在每个服务内部都有一些使用 golang 的额外路由。

我的问题是:当我尝试向 /api/auth/healthz 发送 API 请求时,我希望到达 statusHandler() 函数,但我总是点击 requestHandler() 函数,即使我有那里有一个 http.HandleFunc:http.HandleFunc("/healthz", statusHandler)

这是我的代码

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...\n")
    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")
    ...
}

这是我的 Ingress 代码:

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: /

基于Kubernetes' documentation 应该像他们所拥有的那样工作,这与我的实现类似:

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"))
    }
})

【问题讨论】:

  • 在你的入口,尝试将路径形式“/api/auth”更改为“/api/auth/*”,并且就像他的回答中提到的@stacksonsracks kubernetes 将整个路径发送到服务器所以也尝试将路由从“http.HandleFunc("/", requestHandler)" 更改为 "http.HandleFunc("/api/auth/", requestHandler)"

标签: go kubernetes kubernetes-ingress


【解决方案1】:

根据您的入口规则,请求的路径应该是: /api/auth/healthz 不是/api/auth-service/healthz

入口路径 /api/auth/ 保存在传递给应用程序服务器的请求 uri 中。

rewrite-target 添加到入口注释将确保路径按照您的预期传递到您的底层服务器,例如作为/&lt;path&gt; 而不是/api/auth/&lt;path&gt;。检查应用程序服务器日志应该会显示这种情况。

ingress.kubernetes.io/rewrite-target: /

【讨论】:

  • 对不起,我实际上是在调用 /auth 而不是 /auth-service,我编辑了问题
  • 我在注释中添加了ingress.kubernetes.io/rewrite-target: /,但这不起作用,我仍然遇到同样的问题。不过你说的很有道理
猜你喜欢
  • 2021-10-22
  • 1970-01-01
  • 2011-09-27
  • 1970-01-01
  • 2022-11-11
  • 2018-08-18
  • 2022-11-26
  • 2021-01-10
  • 1970-01-01
相关资源
最近更新 更多