Kubernetes ingress gives 404 error for a particular service

10,284

Solution 1

It was nginx.ingress.kubernetes.io/rewrite-target: /$ that was causing the issue. I commented it out and it resolved the issue.

Solution 2

As I see in your yaml file, you set the name of the service as ban-simple-service for your pod ban-simple:

kind: Service
apiVersion: v1
metadata:
  name: ban-simple-service
  namespace: ban
spec:
  selector:
    app: ban-simple
  ports:
    - port: 80 # Default port for image

But you set the service name as ban-service in the ingress:

    - path: /ban-simple
      backend:
        serviceName: ban-service
        servicePort: 80

I think it's the possible reason. So you need to change one of the service names to make them the same.

And in my opinion, I will suggest you use the deployment instead of the pod. Because if the pod is down, then it's down. But use deployment, it will recreate a new one for you. Also, I think if you expose the port in the container, the yaml file will be more readable.

Share:
10,284
IHelpPeople
Author by

IHelpPeople

Updated on June 28, 2022

Comments

  • IHelpPeople
    IHelpPeople almost 2 years

    I have set up a kubernetes cluster on Azure with nginx ingress. I am getting a 404 error when navigating to a particular path.

    I have set up some sample applications that return a simple echo that works perfectly fine. My ban api app always returns a 404 error.

    When I navigate to the ingress path e.g.

    http://51.145.17.105/apple
    

    It works fine. However when I navigate to the api application, I get a 404 error using the URL:

    http://51.145.17.105/ban-simple
    

    If I log into the cluster and curl the ban-simple service (not the ingress ip) e.g.

    curl -L 10.1.1.40
    

    I get the correct response. When I try it using the nginx ingress I get the 404 error.

    The ingress mapping looks right to me. Here is a copy of the ingress yaml containing the paths.

    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      name: fruit-ingress
      namespace: ban
      annotations:
        kubernetes.io/ingress.class: nginx
        nginx.ingress.kubernetes.io/ssl-redirect: "false"
        nginx.ingress.kubernetes.io/rewrite-target: /$
    spec:
      rules:
      - http:
          paths:
            - path: /apple
              backend:
                serviceName: apple-service
                servicePort: 5678
            - path: /ban-simple
              backend:
                serviceName: ban-service
                servicePort: 80
    

    A copy of the "good" service is:

    kind: Pod
    apiVersion: v1
    metadata:
      name: apple-app
      namespace: ban
      labels:
        app: apple
    spec:
      containers:
        - name: apple-app
          image: hashicorp/http-echo
          args:
            - "-text=apple"
    
    ---
    
    kind: Service
    apiVersion: v1
    metadata:
      name: apple-service
      namespace: ban
    spec:
      selector:
        app: apple
      ports:
        - port: 5678 # Default port for image
    

    The service that does not work is:

    kind: Pod
    apiVersion: v1
    metadata:
      name: ban-simple
      namespace: ban
      labels:
        app: ban-simple
    spec:
      containers:
        - name: ban-simple
          image: xxxxx.azurecr.io/services/ban
    
    ---
    
    kind: Service
    apiVersion: v1
    metadata:
      name: ban-simple-service
      namespace: ban
    spec:
      selector:
        app: ban-simple
      ports:
        - port: 80 # Default port for image
    

    I have run the container locally and it works on my machine. It does redirect to localhost/index.html if that makes a difference.

    Any suggestions are appreciated.

    • 4c74356b41
      4c74356b41 almost 5 years
      i dont understand, you app returns 404, you get a 404 and somehow you feel that something else should happen? either way, look at the ingress logs first and post them here if you cant figure it out
    • Charles Xu
      Charles Xu almost 5 years
      Any update for the question? Does it work for you?
  • IHelpPeople
    IHelpPeople almost 5 years
    Charles, many thanks for your time. It wasn't the service name it was the ``` nginx.ingress.kubernetes.io/rewrite-target: /$ ``` causing the issue. I commented it out and it worked. Also, thanks for your suggestion on the deployment. I was just testing with the Pod. Definitely will do Deployments when the testing is complete.