K8S Nodejs App with multiple routes - how to Ingress it?

10/8/2021

I have a NodeJS Simple web app with couple of routes.

fs = require("fs");
http = require("http");
url = require("url");

http
  .createServer(function (req, res) {
    var request = url.parse(req.url, true);
    var action = request.pathname;

    // Root Folder
    if (action == "/") {
      res.writeHead(200);
      res.end("Rock Bands!");
    }

    // ACDC
    else if (action == "/acdc") {
      var img = fs.readFileSync("./bands/acdc.jpg");
      res.writeHead(200, { "Content-Type": "image/jpg" });
      res.end(img, "binary");
    }

    // Pink Floyd
    else if (action == "/pinkfloyd") {
      var img = fs.readFileSync("./bands/pinkfloyd.jpg");
      res.writeHead(200, { "Content-Type": "image/jpg" });
      res.end(img, "binary");
    }

    // Queen
    else if (action == "/queen") {
      var img = fs.readFileSync("./bands/queen.jpg");
      res.writeHead(200, { "Content-Type": "image/jpg" });
      res.end(img, "binary");
    } else {
      res.writeHead(200, { "Content-Type": "text/plain" });
      res.end("Rock Band Not Found \n");
    }
  })
  .listen(8081);
console.log("Server Listen On 8081 : http://localhost:8081");

And I have a deployment for it:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: bands-deployment
  labels:
    app: bands
spec:
  replicas: 1
  selector:
    matchLabels:
      app: bands
  template:
    metadata:
      labels:
        app: bands
    spec:
      imagePullSecrets:
      - name: regcred   
      containers:
       - name: bands
         image: xxxx/xxxx
         ports:
           - containerPort: 8081
             protocol: TCP

and NodePort Service:

apiVersion: v1
kind: Service
metadata:
  name: bands
spec:
  type: NodePort
  selector:
    app: bands
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8081
      nodePort: 30036

I created an Ingress but when I access the Ingress I'm getting the root page only ("Rock Bands!").

For example: http://ingress(ip)/, when I'm trying to put after the / some of the paths I wrote in the app.js I'm still getting the root.

I want to enter in the browser http://ingress(ip)/acdc and see what I have configured in the app.js.

Ingress:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - host: bands.com
      http:
        paths:
          - path: /*
            pathType: Prefix
            backend:
              service:
                name: bands
                port:
                  number: 8080
-- user9142877
kubernetes
kubernetes-ingress
node.js

2 Answers

10/8/2021

Found the Issue , The Ingress should be like this :

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: bands
spec:
  defaultBackend:
    service:
      name: bands
      port:
        number: 8081
-- user9142877
Source: StackOverflow

10/11/2021

I created your NodeJS web application using the code you provided

The Ingress resource definition from the question is almost correct. Since you are using a Prefix path type, you just need to remove the nginx.ingress.kubernetes.io/rewrite-target: /$1 annotation and change path: /* to path: / to make it work.

You can use the following Ingress resource definition:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
  name: ingress
spec:
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: bands
            port:
              number: 8080
          
-- matt_j
Source: StackOverflow