Search code examples
kubernetesnginx-ingress

Kubenetes Ingress-Nginx 404 not found


I'm new to k8s and trying to get a cluster on GKE set up. I had it working close with just services and nginx built into the frontend image, however the routing was not working correctly, so looking online and its clear I should use an ingress. Been trying to get the nginx-ingress set up, but keep getting a 404 response, and I cannot figure out what I'm doing wrong.

I'm pulling the ingress from here:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.0/deploy/static/provider/cloud/deploy.yaml

and have the controller yaml set up like so:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    nginx.ingress.kubernetes.io/user-regex: "true"
    nginx.ingress.kuberenetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/enable-cors: "true"
    nginx.ingress.kubernetes.io/cors-allow-origin: "*"
    nginx.ingress.kubernetes.io/cors-allow-methods: "PUT, GET, POST, OPTIONS, DELETE"
    nginx.ingress.kubernetes.io/cors-allow-headers: "DNT,X-CustomHeader,X-LANG,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,X-Api-Key,X-Device-Id,Access-Control-Allow-Origin"
  
spec:
  rules:
  - http:
      paths:
      - path: /?(.*)
        pathType: Prefix
        backend:
          service:
            name: client-service
            port:
              number: 3000
      - path: /api/?(.*)
        pathType: Prefix
        backend:
          service:
            name: server-service
            port:
              number: 5000
  ingressClassName: nginx

client service:

apiVersion: v1
kind: Service
metadata:
  name: client-service
spec:
  ports:
    - port: 3000
      protocol: TCP
      targetPort: http-port
  selector:
    app: client
  type: ClusterIP

client deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: client-deployment
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: client
    spec:
      containers:
        - name: client
          image: jowz/client:v4.2
          ports:
            - containerPort: 3000
              name: http-port
  selector:
    matchLabels:
      app: client

I have no errors, image pull issues, or anything showing either in the terminal or in the GKE console.

pods:

NAMESPACE       NAME                                                 READY   STATUS      RESTARTS   AGE
default         client-deployment-9ccf8cf87-27pvv                    1/1     Running     0          87m
default         mongodb                                              1/1     Running     0          21h
default         server-deployment-df679664f-q9fjw                    1/1     Running     0          87m
ingress-nginx   ingress-nginx-admission-create-tsp4n                 0/1     Completed   0          9m23s
ingress-nginx   ingress-nginx-admission-patch-q7spm                  0/1     Completed   1          9m23s
ingress-nginx   ingress-nginx-controller-86b55bb769-2bqs5            1/1     Running     0          9m24s
kube-system     event-exporter-gke-d4b7ff94-4st8l                    2/2     Running     0          21h
kube-system     fluentbit-gke-xlv7v                                  2/2     Running     0          21h
kube-system     gke-metrics-agent-fhnmq                              2/2     Running     0          21h
kube-system     konnectivity-agent-697c66b96-bvfdw                   1/1     Running     0          21h
kube-system     konnectivity-agent-autoscaler-864fff96c4-n9tlp       1/1     Running     0          21h
kube-system     kube-dns-autoscaler-758c4689b9-7gzx8                 1/1     Running     0          21h
kube-system     kube-dns-fc686db9b-hjl4d                             4/4     Running     0          21h
kube-system     kube-proxy-gke-octodemo-default-pool-20afc590-94qj   1/1     Running     0          21h
kube-system     l7-default-backend-9db4bd868-zgx8s                   1/1     Running     0          21h
kube-system     metrics-server-v0.5.2-66bbcdbffc-bmzp6               2/2     Running     0          21h
kube-system     pdcsi-node-fqzzl                                     2/2     Running     0          21h

services:

default         client-service                       ClusterIP      10.92.6.30     <none>          3000/TCP                     88m
default         kubernetes                           ClusterIP      10.92.0.1      <none>          443/TCP                      21h
default         mongodb-service                      ClusterIP      10.92.4.232    <none>          27017/TCP                    21h
default         server-service                       ClusterIP      10.92.15.190   <none>          5000/TCP                     88m
ingress-nginx   ingress-nginx-controller             LoadBalancer   10.92.2.242    35.235.98.121   80:30275/TCP,443:30487/TCP   10m
ingress-nginx   ingress-nginx-controller-admission   ClusterIP      10.92.13.144   <none>          443/TCP                      10m
kube-system     default-http-backend                 NodePort       10.92.14.152   <none>          80:30200/TCP                 21h
kube-system     kube-dns                             ClusterIP      10.92.0.10     <none>          53/UDP,53/TCP                21h
kube-system     metrics-server                       ClusterIP      10.92.11.249   <none>          443/TCP                      21h

Curling gets the same response as the browser:

*   Trying 35.235.98.121:80...
* Connected to 35.235.98.121 (35.235.98.121) port 80 (#0)
> GET / HTTP/1.1
> Host: 35.235.98.121
> User-Agent: curl/7.81.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 404 Not Found
< Date: Tue, 18 Jul 2023 21:05:23 GMT
< Content-Type: text/html
< Content-Length: 146
< Connection: keep-alive
< 
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>
* Connection #0 to host 35.235.98.121 left intact

but ping returns find:

--- 35.235.98.121 ping statistics ---
43 packets transmitted, 43 received, 0% packet loss, time 42063ms
rtt min/avg/max/mdev = 21.119/36.551/206.584/33.645 ms

If anyone can see where I'm going wrong, please let me know. Been running in circles trying to wrap my head around this


Solution

  • I think the primary problem here is that you've misspelled some of the annotations on your Ingress resource.

    1. You wrote user where you meant use. Instead of:

      nginx.ingress.kubernetes.io/user-regex: "true"
      

      You need:

      nginx.ingress.kubernetes.io/use-regex: "true"
      
    2. You spelled kubernetes as kuberenetes. Instead of:

      nginx.ingress.kuberenetes.io/rewrite-target: /$1
      

      You need:

      nginx.ingress.kubernetes.io/rewrite-target: /$1
      

    With these two changes, things seem to work as expected. You can see my complete test here.