Nginx Ingress Controller Returns 404 对于“/”AKS 以外的路径 - Kubernetes 1.17

Nginx Ingress Controller Returns 404 for path other than '/' AKS - Kubernetes 1.17

我正在使用 kubernetes nginx 入口控制器(不是 nginx 支持的控制器,而是 k8s supported one), with AKS (k8s version 1.17 at the moment), for which the corresponding apiversion for Ingress is networking.k8s.io/v1beta1, therefore using this 文档。

对于最基本的 Ingress,都可以,即:

# singlebackend-ingress-rule.yaml
apiVersion: networking.k8s.io/v1beta1 #  AKS is with 1.17, and supports only apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-rules
spec:
  rules:
  - host: ingress-domain.westeurope.cloudapp.azure.com
    http:      
      paths:
      - backend:
          serviceName: backend-webapi
          servicePort: 80

部署为:

kubectl apply -f singlebackend-ingress-rule.yaml

curl http://ingress-domain.westeurope.cloudapp.azure.com.westeurope.cloudapp.azure.com/health
200
{                                                         
   "me" : {                                               
      "status" : "ok",                                    
      "version" : "v15",                                  
      "appName" : "Backend.WebApi",  
      "aspNetCore_Environment" : "aks"                    
   }                                                      
}                                                         

但是,如果我 添加 backend: /api,并执行完全相同的操作:

# multiplebackend-ingress-rule.yaml
apiVersion: networking.k8s.io/v1beta1 #  AKS is with 1.17, and supports only apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-rules
spec:
  rules:
  - host: ingress-domain.westeurope.cloudapp.azure.com
    http:      
      paths:
      - backend:
          serviceName: backend-webapi
          servicePort: 80
        path: /api

部署为:

kubectl apply -f multiplebackend-ingress-rule.yaml

curl http://ingress-domain.westeurope.cloudapp.azure.com.westeurope.cloudapp.azure.com/api/health

HTTP/2 404
server: nginx/1.19.1
date: Tue, 01 Sep 2020 09:24:36 GMT
content-length: 0
strict-transport-security: max-age=15724800; includeSubDomains

我得到 404。我错过了什么?

您定义的路线是 /health 而不是 /api/health

您在 Ingress 中定义的路径必须由您的 back-end 服务器处理。

您的 Ingress 将所有前缀为 /api 的请求发送到您的服务 backend-webapi,但它不会丢弃路由本身的路径 /api

因此,当您向 http://ingress-domain.westeurope.cloudapp.azure.com.westeurope.cloudapp.azure.com/api/health 发送请求时,您的服务必须处理 /api/health 而不是 /health

默认情况下,nginx 将流量发送到 path.You 中提到的任何地方,可以使用 rewrite-target 注释来指定必须重定向流量的目标 URI。

# singlebackend-ingress-rule.yaml
apiVersion: networking.k8s.io/v1beta1 #  AKS is with 1.17, and supports only apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-rules
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: ingress-domain.westeurope.cloudapp.azure.com
    http:      
      paths:
      - backend:
          serviceName: backend-webapi
          servicePort: 80
        path: /api

作为 rewrite-target 的替代方法,您可以使用 app-root 注释

# singlebackend-ingress-rule.yaml
apiVersion: networking.k8s.io/v1beta1 #  AKS is with 1.17, and supports only apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-rules
  annotations:
    nginx.ingress.kubernetes.io/app-root: /
spec:
  rules:
  - host: ingress-domain.westeurope.cloudapp.azure.com
    http:      
      paths:
      - backend:
          serviceName: backend-webapi
          servicePort: 80
        path: /api

通过上面的更改,curl 应该可以工作,而无需在后端处理 /api/health 的 http 处理程序。您可以在 /health

处简单地设置一个 http 处理程序
curl http://ingress-domain.westeurope.cloudapp.azure.com.westeurope.cloudapp.azure.com/health