0

I have node microservice application running in Kubernetes

I want https://app.domain.com/vehicle/api/v1/.... must go to https://app.domain.com/api/v1/....

if i use rewrite-target annotation as shown below my homepage is coming blank as you can see hereenter image description here

if i remove that annotation then my homepage is coming as expected

this is my ingress yaml file with rewrite annotation

apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: ingress-srv
      annotations:
        kubernetes.io/ingress.class: nginx
        nginx.ingress.kubernetes.io/proxy-body-size: "0"
        nginx.ingress.kubernetes.io/proxy-connect-timeout: "360"
        nginx.ingress.kubernetes.io/proxy-read-timeout: "360"
        nginx.ingress.kubernetes.io/proxy-send-timeout: "360"
        nginx.ingress.kubernetes.io/rewrite-target: /$2
    spec:
      tls:
      - hosts:
        - app.domain.com
        secretName: app-ssl
      defaultBackend:
        service:
          name: app-webapp-service
          port:
            number: 80
      rules:
      - host: app.domain.com
        http:
          paths:
          - path: /vehicle(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: app-vehicle-service
                port:
                  number: 5001
           - path: /*
        pathType: Prefix
        backend:
          service:
            name: app-webapp-service
            port:
              number: 80

this is my Dockerfile for React (front end), if any changes need to be done please suggest

FROM node:12.18.3 AS build

ENV CI=false
ENV WDS_SOCKET_PORT=0

WORKDIR /app

COPY ["package.json", "package-lock.json", "./"]

RUN npm install --production

COPY . .

RUN npm run build:development

FROM nginx:alpine

COPY --from=build /app/build /usr/share/nginx/html

COPY --from=build /app/nginx-custom.conf /etc/nginx/conf.d/default.conf

this is my nginx-custom.conf

server {
  listen 80;
  location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
    try_files $uri $uri/ /index.html =404;
  }
}

if any changes need to be done in any file please suggest

Harshith
  • 73
  • 8

1 Answers1

0

You will need to write two separate manifests, First for Socket Application and another for path based routing.

  1. I want https://app.domain.com/vehicle/socket.io/eio..... to go to https://app.domain.com/socket.io/eio.....
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-socket
  annotations:
    nginx.ingress.kubernetes.io/server-snippets: |
      location / {
        proxy_set_header Upgrade $http_upgrade;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header Connection "upgrade";
        proxy_cache_bypass $http_upgrade;
      }
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.org/websocket-services: socket-svc-name
    nginx.ingress.kubernetes.io/proxy-connect-timeout: "360"
    nginx.ingress.kubernetes.io/proxy-read-timeout: "360"
    nginx.ingress.kubernetes.io/proxy-send-timeout: "360"
spec:
  ingressClassName: nginx
  rules:
  - host: app.domain.com
    http:
      paths:
      - backend:
          service:
            name: socket-svc-name
            port:
              number: 80
        path: /.*
        pathType: Prefix

Note: In your LoadBalancer listener configuration it must be listening on TCP/80, SSL/443 not HTTP/80, HTTPS/443 and forwarding requests over TCP/SSL protocol, not HTTP/HTTPS because Socket applications run on WSS protocol.

  1. https://app.domain.com/vehicle/api/v1/.... must go to https://app.domain.com/vehicle/api/v1/....

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-srv
  annotations:
    nginx.ingress.kubernetes.io/configuration-snippet: |
      more_set_headers "Access-Control-Allow-Origin: *";
    nginx.ingress.kubernetes.io/cors-allow-credentials: "true"
    nginx.ingress.kubernetes.io/cors-allow-methods: PUT, GET, POST, OPTIONS, DELETE, PATCH
    nginx.ingress.kubernetes.io/cors-allow-origin: '*'
    nginx.ingress.kubernetes.io/enable-cors: "true"
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  ingressClassName: nginx
  rules:
  - host: app.domain.com
    http:
      paths:
      - backend:
          service:
            name: app-vehicle-service
            port:
              number: 5001
        path: /vehicle/api/v1/.*
        pathType: Prefix

  • i have added copied socket manifest and applied whole application went down, homepage also not opening, it is showing as {"status":"fail","message":"Can't find / on this server"} – Harshith Jul 28 '22 at 10:27
  • If you've deployed multiple microservices in a single subdomain in that you can't deploy socket-based API in same sub domain. (You might able to do so but till now I didn't got success in doing this) You would require a separate subdomain to deploy socket based API – Anurag Tamrakar Jul 28 '22 at 12:07
  • I would recommand you to deploy socket based API in a different sub domain, In that sub domain nothing should be deployed other than Socket API – Anurag Tamrakar Jul 28 '22 at 12:10
  • code can't be changed right now, if you can help me like all requests going to https://app.domain.com/vehicle/any/.... go to https://app.domain.com/any/...... https://app.domain.com/audit/any/..... go to https://app.domain.com/any/....... https://app.domain.com/payments/any/..... go to https://app.domain.com/any/..... etc that will be helpful i think – Harshith Jul 28 '22 at 13:15