Nginx Ingress控制器返回404。

huangapple go评论86阅读模式
英文:

Nginx ingress controller returns 404

问题

I am trying to create Ingress for my kubernetes service. I am using minikube running on WSL2.
Here are my deployment, service and ingress settings

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: {{.Chart.Name}}-backend
  5. labels:
  6. app: {{.Chart.Name}}-backend
  7. spec:
  8. replicas: {{ .Values.replicas }}
  9. selector:
  10. matchLabels:
  11. service: backend
  12. app: {{.Chart.Name}}-backend
  13. template:
  14. metadata:
  15. labels:
  16. service: backend
  17. app: {{.Chart.Name}}-backend
  18. spec:
  19. containers:
  20. - name: kuberdemo-app
  21. image: {{ .Values.image }}
  22. ports:
  23. - containerPort: 8091
  24. targetPort: 8091
  25. protocol: TCP
  26. name: app-port
  27. envFrom:
  28. - configMapRef:
  29. name: kuberdemo-config
  30. startupProbe:
  31. httpGet:
  32. path: /actuator/health/liveness
  33. port: 14000
  34. failureTreshold: 2
  35. periodSeconds: 10
  36. readinessProbe:
  37. httpGet:
  38. path: /actuator/health/readiness
  39. port: 14000
  40. failureTreshold: 2
  41. periodSeconds: 10
  42. livenessProbe:
  43. httpGet:
  44. path: /actuator/health/liveness
  45. port: 14000
  46. failureTreshold: 2
  47. periodSeconds: 10
  48. resources:
  49. limits:
  50. cpu: "2000m"
  51. memory: "2Gi"
  52. requests:
  53. cpu: "2000m"
  54. memory: "2Gi"
  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: kuberdemo-service
  5. spec:
  6. type: ClusterIP
  7. selector:
  8. app: {{.Chart.Name}}-backend
  9. ports:
  10. - protocol: TCP
  11. port: 8090
  12. targetPort: 8091
  1. apiVersion: networking.k8s.io/v1
  2. kind: Ingress
  3. metadata:
  4. name: kuberdemo-ingress
  5. spec:
  6. rules:
  7. - host: kuberdemo.info
  8. http:
  9. paths:
  10. - path: /
  11. pathType: Prefix
  12. backend:
  13. service:
  14. name: kuberdemo-service
  15. port:
  16. number: 8090

Service is working correctly, because I get answers from it when calling it directly.

  1. kubectl port-forward service/kuberdemo-service -n kuberdemo 8080:8090
  2. curl --location 'http://localhost:8080/users'

the answer is [] (right now there are no data in my db).

But when I am trying to call it using host or after forwarding localhost 8080 port to ingress-nginx port, I am getting 404 error from NGINX.

  1. kubectl port-forward -n ingress-nginx ingress-nginx-controller-6cc5ccb977-zzbcb 8080:80
  2. curl --location 'http://localhost:8080/users'

the answer is

  1. <html>
  2. <head><title>404 Not Found</title></head>
  3. <body>
  4. <center><h1>404 Not Found</h1></center>
  5. <hr><center>nginx</center>
  6. </body>
  7. </html>

It's my first time working with kubernetes, so it will be great if you find some mistakes in my configuration files. I suppose, I have some troubles with nginx configuration, but I don't know how to fix them.

英文:

I am trying to create Ingress for my kubernetes service. I am using minikube running on WSL2.
Here are my deployment, service and ingress settings

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: {{.Chart.Name}}-backend
  5. labels:
  6. app: {{.Chart.Name}}-backend
  7. spec:
  8. replicas: {{ .Values.replicas }}
  9. selector:
  10. matchLabels:
  11. service: backend
  12. app: {{.Chart.Name}}-backend
  13. template:
  14. metadata:
  15. labels:
  16. service: backend
  17. app: {{.Chart.Name}}-backend
  18. spec:
  19. containers:
  20. - name: kuberdemo-app
  21. image: {{ .Values.image }}
  22. ports:
  23. - containerPort: 8091
  24. targetPort: 8091
  25. protocol: TCP
  26. name: app-port
  27. envFrom:
  28. - configMapRef:
  29. name: kuberdemo-config
  30. startupProbe:
  31. httpGet:
  32. path: /actuator/health/liveness
  33. port: 14000
  34. failureTreshold: 2
  35. periodSeconds: 10
  36. readinessProbe:
  37. httpGet:
  38. path: /actuator/health/readiness
  39. port: 14000
  40. failureTreshold: 2
  41. periodSeconds: 10
  42. livenessProbe:
  43. httpGet:
  44. path: /actuator/health/liveness
  45. port: 14000
  46. failureTreshold: 2
  47. periodSeconds: 10
  48. resources:
  49. limits:
  50. cpu: "2000m"
  51. memory: "2Gi"
  52. requests:
  53. cpu: "2000m"
  54. memory: "2Gi"
  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: kuberdemo-service
  5. spec:
  6. type: ClusterIP
  7. selector:
  8. app: {{.Chart.Name}}-backend
  9. ports:
  10. - protocol: TCP
  11. port: 8090
  12. targetPort: 8091
  1. apiVersion: networking.k8s.io/v1
  2. kind: Ingress
  3. metadata:
  4. name: kuberdemo-ingress
  5. spec:
  6. rules:
  7. - host: kuberdemo.info
  8. http:
  9. paths:
  10. - path: /
  11. pathType: Prefix
  12. backend:
  13. service:
  14. name: kuberdemo-service
  15. port:
  16. number: 8090

Service is working correctly, because I get answers from it when calling it directly.

  1. kubectl port-forward service/kuberdemo-service -n kuberdemo 8080:8090
  2. curl --location 'http://localhost:8080/users'

the answer is [] (right now there are no data in my db).

But when I am trying to call it using host or after forwarding localhost 8080 port to ingress-nginx port, I am getting 404 error from NGINX.

  1. kubectl port-forward -n ingress-nginx ingress-nginx-controller-6cc5ccb977-zzbcb 8080:80
  2. curl --location 'http://localhost:8080/users'

the answer is

  1. <html>
  2. <head><title>404 Not Found</title></head>
  3. <body>
  4. <center><h1>404 Not Found</h1></center>
  5. <hr><center>nginx</center>
  6. </body>
  7. </html>

It's my first time working with kubernetes, so it will be great if you find some mistakes in my configuration files. I suppose, I have some troubles with nginx configuration, but I don't know how to fix them.

答案1

得分: 0

你需要包括一个 -H Host 头部。

您的 Ingress 定义如下,Ingress 控制器使用它:

  1. apiVersion: networking.k8s.io/v1
  2. kind: Ingress
  3. metadata:
  4. name: kuberdemo-ingress
  5. spec:
  6. rules:
  7. - host: kuberdemo.info
  8. http:
  9. paths:
  10. - path: /
  11. pathType: Prefix
  12. backend:
  13. service:
  14. name: kuberdemo-service
  15. port:
  16. number: 8090

没有一个针对 localhost 的块,所以当您执行以下操作时:

  1. curl --location 'http://localhost:8080/users'

Nginx 寻找具有 localhost 块的 Ingress,找不到,然后转到默认的 404 后端。

尝试:

  1. curl --location 'http://localhost:8080/users' -H "Host: kuberdemo.info"
英文:

You need to include a -H Host header.

Your ingress definition, which the ingress controller uses:

  1. apiVersion: networking.k8s.io/v1
  2. kind: Ingress
  3. metadata:
  4. name: kuberdemo-ingress
  5. spec:
  6. rules:
  7. - host: kuberdemo.info
  8. http:
  9. paths:
  10. - path: /
  11. pathType: Prefix
  12. backend:
  13. service:
  14. name: kuberdemo-service
  15. port:
  16. number: 8090

Doesn't have a block for localhost so when you do

  1. curl --location 'http://localhost:8080/users'

Nginx looks for an ingress that has a block for localhost, doesn't find one, and goes to the default 404 backend.

Try:

  1. curl --location 'http://localhost:8080/users' -H "Host: kuberdemo.info"

答案2

得分: 0

你错过了Ingress类名,请按照下面的示例添加ingressClassName:

  1. apiVersion: networking.k8s.io/v1
  2. kind: Ingress
  3. metadata:
  4. name: kuberdemo-ingress
  5. spec:
  6. ingressClassName: nginx
  7. rules:
  8. - host: kuberdemo.info
  9. http:
  10. paths:
  11. - path: /
  12. pathType: Prefix
  13. backend:
  14. service:
  15. name: kuberdemo-service
  16. port:
  17. number: 8090
英文:

You missed the ingress class name,add ingressclassname as shown below:

  1. apiVersion: networking.k8s.io/v1
  2. kind: Ingress
  3. metadata:
  4. name: kuberdemo-ingress
  5. spec:
  6. ingressClassName: nginx
  7. rules:
  8. - host: kuberdemo.info
  9. http:
  10. paths:
  11. - path: /
  12. pathType: Prefix
  13. backend:
  14. service:
  15. name: kuberdemo-service
  16. port:
  17. number: 8090

huangapple
  • 本文由 发表于 2023年5月14日 03:28:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244534.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定