skip to Main Content

I deployed a Spring Boot app on AWS Elastic Kubernetes Service. I am facing a 502 Bad Gateway error. I cannot find anything useful from the logs, there is no event to check, it works fine locally and the docker image is also running without any issue.

Right now its just a simple hello world app,
Here are the yaml files files or reference.

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend-deployment
  namespace: my-namespace
  labels:
    app: backend-java
spec:
  replicas: 1
  selector:
    matchLabels:
      app: backend-java
  template:
    metadata:
      labels:
        app: backend-java
    spec:
      containers:
        - name: backend-java
          image: <docker-image-location>
          ports:
            - containerPort: 81
          resources:
            limits:
              cpu: "4000m"
              memory: "2048Mi"
            requests:
              cpu: "100m"
              memory: "1024Mi"

service.yaml

apiVersion: v1
kind: Service
metadata:
  namespace: my-namespace
  name: backend-service
spec:
  type: NodePort
  selector:
    app: backend-java
  ports:
    - port: 81
      targetPort: 8080
      nodePort: 30019

ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: "my-app-ingress"
  namespace: "my-namespace"
  annotations:
    alb.ingress.kubernetes.io/scheme: internal
    alb.ingress.kubernetes.io/backend-protocol: HTTP
    alb.ingress.kubernetes.io/listen-ports: '[{"HTTP": 80}]'
  spec:
   ingressClassName: alb
   rules:
     - host: myapp.aws.com
       http:
         paths:
           - path: /
             pathType: Prefix
             backend:
               service:
                 name: "backend-service"
                 port:
                   number: 81

Similar configuration has worked for deploying a react app, which works as expected. Only while deploying backend it give ‘502 Bad Gateway’

2

Answers


  1. No logs on the pod and 502 indicates load balancer or ingress configuration issue.

    • check the configuration of your ingress controller – make sure that it is correctly routing traffic to your app. If you are using an AWS Application Load Balancer (ALB) as your ingress controller, check the ALB’s logs to see if there are any errors.
    • check the security group settings: Make sure that your security group settings allow traffic to and from the ALB and your Kubernetes nodes.
    Login or Signup to reply.
  2. Your targetPort in the Service and the containerPort in the Deployment do not match. You can fix it by changing the targetPort in the Service

    apiVersion: v1
    kind: Service
    metadata:
      namespace: my-namespace
      name: backend-service
    spec:
      type: NodePort
      selector:
        app: backend-java
      ports:
        - port: 81
          targetPort: 81
          nodePort: 30019
    

    Read more about the difference between port and targetPort here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search