Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes Ingress non-root path 404 Not Found

I have the following that config that works when I try <NodeIP>:30080

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: app-deployment
spec:
  replicas: 3
  template:
    metadata:
      labels:
        name: app-node
    spec:
      containers:
        - name: app
          image: myregistry.net/repo/app:latest
          imagePullPolicy: Always
          ports:
            - containerPort: 8080
          env:
            - name: NODE_ENV
              value: production
---
apiVersion: v1
kind: Service
metadata:
  name: app-service
spec:
  selector:
    name: app-node
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
      nodePort: 30080
  type: NodePort

I am trying to use an Ingress:

 apiVersion: extensions/v1beta1
 kind: Ingress
 metadata:
   name: nginx-ingress
 spec:
   rules:
   - host: myhost.com
     http:
       paths:
       - path: /app
         backend:
           serviceName: app-service
           servicePort: 80

myhost.com works with the nginx intro screen, but myhost.com/app gives 404 Not Found. Where is the issue in my setup?


UPDATE:

   - path: /
     backend:
       serviceName: app-service
       servicePort: 80

If I do root path it works, but how come /app doesn't?

like image 389
atkayla Avatar asked Aug 25 '18 23:08

atkayla


3 Answers

Your ingress definition creates rules that proxy traffic from the {path} to the {backend.serviceName}{path}. In your case, I believe the reason it's not working is that /app is proxied to app-service:80/app but you're intending on serving traffic at the / root. Try adding this annotation to your ingress resource: nginx.ingress.kubernetes.io/rewrite-target: /

Source: https://github.com/kubernetes/ingress-nginx/tree/master/docs/examples/rewrite

like image 125
brandon-barnett Avatar answered Oct 23 '22 11:10

brandon-barnett


As specified by brandon-barnett the issue is coming from the path but after reading the link he shared, big ups to you Brandon, I realised that a more specific rewrite rule would have to specified for it to work. In my case

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  ingressClassName: nginx
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - path: /something(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: http-svc
            port: 
              number: 80

The trick is with /$2 at annotation and (/|$)(.*) at the path. So what this rewrite does it picks everything that comes after something/ and replaces $2 with it, so:

  • rewrite.bar.com/something rewrites to http-svc:80/
  • rewrite.bar.com/something/ rewrites to http-svc:80/
  • rewrite.bar.com/something/new rewrites to http-svc:80/new
  • rewrite.bar.com/something/new/old rewrites to http-svc:80/new/old
like image 42
KwakuCsc Avatar answered Oct 23 '22 12:10

KwakuCsc


In case it's useful to others:

I had the same thing, and it was nothing to do with Kubernetes Ingress Nginx. It was the Nginx server in my pod running the UI (Vue.JS in my case).

Solution:

  1. Add an nginx.conf file to my UI app.
     user  nginx;
     worker_processes  auto;
     error_log  /var/log/nginx/error.log warn;
     pid        /var/run/nginx.pid;
     events {
         worker_connections  1024;
     }
     http {
         include       mime.types;
         default_type  application/octet-stream;
         sendfile        on;
         keepalive_timeout  65;
         log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                           '$status $body_bytes_sent "$http_referer" '
                           '"$http_user_agent" "$http_x_forwarded_for"';
         access_log  /var/log/nginx/access.log  main;
         server {
             listen       80;
             root /usr/share/nginx/html;
             index index.html;
             location / {
                 try_files $uri $uri/ /index.html;
             }
         }
     }
    
  2. Add this line to my Dockerfile for the Vue.js app to copy the nginx.conf to your container.
     # production stage
     FROM nginx:stable-alpine as production-stage
     COPY nginx.conf /etc/nginx/
     COPY --from=build-stage /app/dist /usr/share/nginx/html
     EXPOSE 80
     CMD ["nginx", "-g", "daemon off;"]
    

Ref: https://stackoverflow.com/a/54193517/4059810

like image 1
ChrisE Avatar answered Oct 23 '22 12:10

ChrisE