Search code examples
kubernetesminikubenginx-ingresswindows-subsystem-for-linux

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.


Solution

  • 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"