Nginx Ingress控制器返回404。

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

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

apiVersion: apps/v1
kind: Deployment
metadata:
    name: {{.Chart.Name}}-backend
    labels:
        app: {{.Chart.Name}}-backend
spec:
    replicas: {{ .Values.replicas }}
    selector:
        matchLabels:
            service: backend
            app: {{.Chart.Name}}-backend
    template:
        metadata:
            labels:
                service: backend
                app: {{.Chart.Name}}-backend
        spec:  
            containers:
            -   name: kuberdemo-app
                image: {{ .Values.image }}
                ports:
                -   containerPort: 8091
                    targetPort: 8091
                    protocol: TCP
                    name: app-port
                envFrom:
                - configMapRef:
                    name: kuberdemo-config
                startupProbe:
                    httpGet:
                        path: /actuator/health/liveness
                        port: 14000
                    failureTreshold: 2
                    periodSeconds: 10
                readinessProbe:
                    httpGet:
                        path: /actuator/health/readiness
                        port: 14000
                    failureTreshold: 2
                    periodSeconds: 10
                livenessProbe:
                    httpGet:
                        path: /actuator/health/liveness
                        port: 14000
                    failureTreshold: 2
                    periodSeconds: 10
                resources:
                    limits:
                        cpu: "2000m"
                        memory: "2Gi"
                    requests:
                        cpu: "2000m"
                        memory: "2Gi"
                
apiVersion: v1
kind: Service
metadata:
  name: kuberdemo-service
spec:
  type: ClusterIP
  selector:
    app: {{.Chart.Name}}-backend
  ports:
    - protocol: TCP
      port: 8090
      targetPort: 8091
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: kuberdemo-ingress
spec:
  rules:
    - host: kuberdemo.info
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: kuberdemo-service
                port:
                  number: 8090

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

kubectl port-forward service/kuberdemo-service -n kuberdemo 8080:8090
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.

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

the answer is

<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</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

apiVersion: apps/v1
kind: Deployment
metadata:
    name: {{.Chart.Name}}-backend
    labels:
        app: {{.Chart.Name}}-backend
spec:
    replicas: {{ .Values.replicas }}
    selector:
        matchLabels:
            service: backend
            app: {{.Chart.Name}}-backend
    template:
        metadata:
            labels:
                service: backend
                app: {{.Chart.Name}}-backend
        spec:  
            containers:
            -   name: kuberdemo-app
                image: {{ .Values.image }}
                ports:
                -   containerPort: 8091
                    targetPort: 8091
                    protocol: TCP
                    name: app-port
                envFrom:
                - configMapRef:
                    name: kuberdemo-config
                startupProbe:
                    httpGet:
                        path: /actuator/health/liveness
                        port: 14000
                    failureTreshold: 2
                    periodSeconds: 10
                readinessProbe:
                    httpGet:
                        path: /actuator/health/readiness
                        port: 14000
                    failureTreshold: 2
                    periodSeconds: 10
                livenessProbe:
                    httpGet:
                        path: /actuator/health/liveness
                        port: 14000
                    failureTreshold: 2
                    periodSeconds: 10
                resources:
                    limits:
                        cpu: "2000m"
                        memory: "2Gi"
                    requests:
                        cpu: "2000m"
                        memory: "2Gi"
                
apiVersion: v1
kind: Service
metadata:
  name: kuberdemo-service
spec:
  type: ClusterIP
  selector:
    app: {{.Chart.Name}}-backend
  ports:
    - protocol: TCP
      port: 8090
      targetPort: 8091
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: kuberdemo-ingress
spec:
  rules:
    - host: kuberdemo.info
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: kuberdemo-service
                port:
                  number: 8090

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

kubectl port-forward service/kuberdemo-service -n kuberdemo 8080:8090
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.

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

the answer is

<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</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 控制器使用它:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: kuberdemo-ingress
spec:
  rules:
    - host: kuberdemo.info
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: kuberdemo-service
                port:
                  number: 8090

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

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

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

尝试:

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:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: kuberdemo-ingress
spec:
  rules:
    - host: kuberdemo.info
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: kuberdemo-service
                port:
                  number: 8090

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

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:

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

答案2

得分: 0

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

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: kuberdemo-ingress
spec:
  ingressClassName: nginx
  rules:
    - host: kuberdemo.info
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: kuberdemo-service
                port:
                  number: 8090
英文:

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

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: kuberdemo-ingress
spec:
  ingressClassName: nginx
  rules:
    - host: kuberdemo.info
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: kuberdemo-service
                port:
                  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:

确定