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

Time:10-11

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 im getting the root page only ( "Rock Bands!" )

for exmaple http://ingress(ip)/ , when im trying to put after the / some of the pathes i wrote in the app.js im still getting the root .

i want to enter in the browser http://ingress(ip)/acdc and see what i configed 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

CodePudding user response:

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

CodePudding user response:

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
          
  • Related