kuberneteskubernetes-ingressnginx-ingresskubernetes-networking

NGINX Ingress is not reaching Service(clusterIP)


I am trying to deploy a node js app inside a Kubernetes cluster. I have created deplyment and service(clusterIp) for that and added a role inside nginx ingress config file but seems nginx is failing to reach that service.

NodeJs app:

'use strict';

const express = require('express');

// Constants
const PORT = 5678;
const HOST = '0.0.0.0';

// App
const app = express();
app.get('/', (req, res) => {
  console.log("request received");
  res.send('Hello World');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

Dockerfile:

FROM node:14

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 5678
CMD [ "node", "server.js" ]

Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: apple-app-deployment
  labels:
    app: apple
spec:
  replicas: 2
  selector:
    matchLabels:
      app: apple
  template:
    metadata:
      labels:
        app: apple
    spec:
      containers:
      - name: apple-app
        image: anudcker/node-web-appp:1
        ports:
        - containerPort: 5678

Service:

kind: Service
apiVersion: v1
metadata:
  name: apple-service
spec:
  selector:
    app: apple
  ports:
    - port: 5678 # Default port for image
      targetPort: 5678

Ingress:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    # kubernetes.io/ingress.class: azure/application-gateway
    ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
        - path: /apple
          pathType: Prefix
          backend:
            service:
                name: apple-service
                port:
                    number: 5678

Now whenever I am trying to reach the service by hitting /apple I am getting a response Cannot GET /apple from Nginx ingress. I found bellow log inside nginx ingress controller pod.

10.244.1.1 - - [07/May/2021:06:22:29 +0000] "GET /apple HTTP/1.1" 404 144 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36" 465 0.004 [demo-apple-service-5678] [] 10.244.0.24:5678 144 0.004 404 5a5bdf8f61d59b665218be0a18a6ab4c

Any Idea?


Solution

  • The issue is that you are using rewrite-target of networking.k8s.io/v1beta1, with the apiVersion: networking.k8s.io/v1.

    You need to update your annotation to the new one:

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