代码之家  ›  专栏  ›  技术社区  ›  Naguib Ihab

http。手柄不工作

  •  1
  • Naguib Ihab  · 技术社区  · 7 年前

    我正在开发一个应用程序,该应用程序托管在谷歌云上的Kubernetes上,它使用入口管理到每个服务的路由,然后在每个服务中使用golang进行一些额外的路由。

    我的问题是: 当我尝试向发送API请求时 /api/auth/healthz 我期待着到达 statusHandler() 函数,而我总是按 requestHandler() 函数,即使我有一个http。把手放在那里: 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")
        ...
    }
    

    这是我的入口代码:

    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"))
        }
    })
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   stacksonstacks    7 年前

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

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

    正在添加 rewrite-target 到入口的注释将确保按照预期将路径传递到基础服务器,例如 /<path> 而不是 /api/auth/<path> 。检查应用程序服务器日志应表明情况就是这样。

    ingress.kubernetes.io/rewrite-target: /