node.jskubernetesdeploymentkubernetes-ingresshttp-status-code-502

502 Bad gateway on Nodejs application deployed on Kubernetes cluster


I am deploying nodejs application on kubernetes, After deployment pod is up and running, but when I am trying to access the application through ingress it is giving 502 bad gateway error.

Dockerfile

FROM node:14

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
COPY package*.json ./

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

# Bundle app source
COPY . .

EXPOSE 3123
CMD [ "node", "index.js" ]

Deployment.yaml

---
  apiVersion: "apps/v1"
  kind: "Deployment"
  metadata: 
    name: "node-development"
    namespace: "development"
  spec: 
    selector: 
      matchLabels: 
        app: "node-development"
    replicas: 1
    template: 
      metadata: 
        labels: 
          app: "node-development"
      spec: 
        containers: 
          - 
            name: "node-development"
            image: "xxx"
            imagePullPolicy: "Always"
            env: 
              - 
                name: "NODE_ENV"
                value: "development"
            ports: 
              - 
                containerPort: 47033

service.yaml

---
  apiVersion: "v1"
  kind: "Service"
  metadata: 
    name: "node-development-service"
    namespace: "development"
    labels: 
      app: "node-development"
  spec: 
    ports: 
      - 
        port: 47033
        targetPort: 3123
    selector: 
      app: "node-development"

ingress.yaml

---
  apiVersion: "networking.k8s.io/v1"
  kind: "Ingress"
  metadata: 
    name: "node-development-ingress"
    namespace: "development"
    annotations: 
      nginx.ingress.kubernetes.io/rewrite-target: "/$1"
  spec: 
    rules: 
      - 
        host: "xxxx"
        http: 
          paths: 
            - 
              backend: 
                service:
                  name: "node-development"
                  port: 
                    number: 47033
              path: "/node-development/(.*)"
              pathType: "ImplementationSpecific"

With ingress or even with the pod cluster ip I am not being able to access application it is throwing 502 bad gateway nginx


Solution

  • Issue got resolved, I am using SSL in my application as a result it was not re-directing with the given ingress url.

    Need to add below annotation in ingress.yaml file.

    nginx.ingress.kubernetes.io/ssl-passthrough: "true"
    nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"