Issue in configuring ingress path based routing

Hello Everyone , Need some help .
I have been trying to configure nginx ingress path based routing , but iam getting 404 error.

Below is the ingress resource details i see

Name: example-ingress
Namespace: default
Address: 20.101.174.186
Default backend: default-http-backend:80 (<error: endpoints “default-http-backend” not found>)
Rules:
Host Path Backends


  •       /first    service1:80 (10.244.0.12:8080)
          /second   service2:80 (10.244.0.13:8080)
    

Annotations: nginx.ingress.kubernetes.io/rewrite-target: /
Events:
Type Reason Age From Message


Normal Sync 43m (x11 over 16h) nginx-ingress-controller Scheduled for sync

when i try to access http://20.101.174.186/first , getting the 404 error

Below is the ingress resource yaml file

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:

  • http:
    paths:
    • path: /first
      pathType: Prefix
      backend:
      service:
      name: service1
      port:
      number: 80
    • path: /second
      pathType: Prefix
      backend:
      service:
      name: service2
      port:
      number: 80

Can someone please guide me what i am missing here

How did you configure your Ingress-NGINX controller ?

From Ingress | Kubernetes

There are some ingress controllers, that work without the definition of a default IngressClass. For example, the Ingress-NGINX controller can be configured with a flag --watch-ingress-without-class. It is recommended though, to specify the default IngressClass as shown below.
If the ingressClassName is omitted, a default Ingress class should be defined.

Missing ingressclass ?

sauron@kind:~/Projects/kind$ k create deployment first --image nginx:1.25.2 --port 80
deployment.apps/first created

sauron@kind:~/Projects/kind$ k create deployment second --image nginx:1.25.2 --port 80
deployment.apps/second created

sauron@kind:~/Projects/kind$ k expose deployment first --name service1 --port 80
service/service1 exposed

sauron@kind:~/Projects/kind$ k expose deployment second --name service2 --port 80
service/service2 exposed

sauron@kind:~/Projects/kind$ k get ingressclass
NAME            CONTROLLER             PARAMETERS   AGE
ingress-nginx   k8s.io/ingress-nginx   <none>       22m

sauron@kind:~/Projects/kind$ k create ingress example-ingress \
                                  --class ingress-nginx \
                                  --rule=/first*=service1:80 \
                                  --rule=/second*=service2:80 \
                                  --annotation nginx.ingress.kubernetes.io/rewrite-target="/"
ingress.networking.k8s.io/example-ingress created

sauron9@kind:~/Projects/kind$ k describe ingress example-ingress
Name:             example-ingress
Labels:           <none>
Namespace:        default
Address:          10.254.254.240
Ingress Class:    ingress-nginx
Default backend:  <default>
Rules:
  Host        Path  Backends
  ----        ----  --------
  *
              /first    service1:80 (192.168.110.135:80)
              /second   service2:80 (192.168.110.136:80)
Annotations:  nginx.ingress.kubernetes.io/rewrite-target: /
Events:
  Type    Reason  Age                    From                      Message
  ----    ------  ----                   ----                      -------
  Normal  Sync    3m51s (x2 over 3m54s)  nginx-ingress-controller  Scheduled for sync

sauron@kind:~/Projects/kind$ k get ingress example-ingress -oyaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
  creationTimestamp: "2023-09-12T12:41:38Z"
  generation: 1
  name: example-ingress
  namespace: default
  resourceVersion: "8416"
  uid: a6759c94-f625-4f8b-9fbc-5f92ed45d847
spec:
  ingressClassName: ingress-nginx
  rules:
  - http:
      paths:
      - backend:
          service:
            name: service1
            port:
              number: 80
        path: /first
        pathType: Prefix
      - backend:
          service:
            name: service2
            port:
              number: 80
        path: /second
        pathType: Prefix
status:
  loadBalancer:
    ingress:
    - ip: 10.254.254.240

sauron@kind:~/Projects/kind$curl -s 10.254.254.240/first | grep "<h1>"
<h1>Welcome to nginx!</h1>

sauron@kind:~/Projects/kind$ curl -s 10.254.254.240/second | grep "<h1>"
<h1>Welcome to nginx!</h1>

sauron@kind:~/Projects/kind$

Thank you for your detailed reply . i have followed the installation guide [Installation Guide - Ingress-Nginx Controller (kubernetes.github.io)] (Installation Guide - Ingress-Nginx Controller) and deployed ingress-nginx controller through helm .

Below is the yaml file i have used . if i comment everything under rules and just use the defaultBackend . i can see the application is working if i browse just the IP ADDRESS alone without any prefix. ( http:ingressipaddress )

when i tried to access http://ingressipaddress/first , i see error as
“Cannot GET /first”

my understanding was since i have configured service1 as default , browsing anypath should fetch me the same result , but that is not happening.

i feel something is missing for the path prefix to work .

=================================================================

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: test-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx

rules:

- http:

paths:

- path: /first

pathType: Prefix

backend:

service:

name: service1

port:

number: 80

defaultBackend:
service:
name: service1
port:
number: 80