1

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'

Prajul Aaditya
  • 109
  • 2
  • 9
  • Please provide much more details (deployment, service, ingress spec) about how you deployed your application by [editing](https://stackoverflow.com/posts/75802235/edit) your question. No one would be able to provide you any solution by the details you shared so far – Sibtain Mar 21 '23 at 14:48
  • I made the changes, provided the details, Thank you for your suggestion. – Prajul Aaditya Mar 22 '23 at 07:33

2 Answers2

1

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.

Sibtain
  • 1,436
  • 21
  • 39
  • I made the changes, and yes in React side, I have the same targetPort and containerPort, but the 502 error still exists. – Prajul Aaditya Mar 22 '23 at 10:46
  • 1
    Try to do curl on the backend-service from your react pod, something like this. `kubectl exec react-pod -- curl http://backend-service:81` – Sibtain Mar 22 '23 at 10:53
  • It worked, sorry my CICD pipeline was failing, so the alb I tried didn't had these changes. Now it is fixed, I can see the deployment is successful. – Prajul Aaditya Mar 22 '23 at 11:32
  • Feel free to accept/upvote this answer please. – Sibtain Mar 22 '23 at 12:25
0

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.