<p>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.</p>
<p>Right now its just a simple hello world app,
Here are the yaml files files or reference.</p>
<p>deployment.yaml</p>
<pre><code>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: &lt;docker-image-location&gt;
          ports:
            - containerPort: 81
          resources:
            limits:
              cpu: &quot;4000m&quot;
              memory: &quot;2048Mi&quot;
            requests:
              cpu: &quot;100m&quot;
              memory: &quot;1024Mi&quot;
</code></pre>
<p>service.yaml</p>
<pre><code>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
</code></pre>
<p>ingress.yaml</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: &quot;my-app-ingress&quot;
  namespace: &quot;my-namespace&quot;
  annotations:
    alb.ingress.kubernetes.io/scheme: internal
    alb.ingress.kubernetes.io/backend-protocol: HTTP
    alb.ingress.kubernetes.io/listen-ports: '[{&quot;HTTP&quot;: 80}]'
  spec:
   ingressClassName: alb
   rules:
     - host: myapp.aws.com
       http:
         paths:
           - path: /
             pathType: Prefix
             backend:
               service:
                 name: &quot;backend-service&quot;
                 port:
                   number: 81
</code></pre>
<p>Similar configuration has worked for deploying a react app, which works as expected. Only while deploying backend it give '502 Bad Gateway'</p>

Prajul Aaditya
<p>Your <code>targetPort</code> in the <code>Service</code> and the <code>containerPort</code> in the <code>Deployment</code> do not match. You can fix it by changing the targetPort in the Service</p>
<pre><code>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
</code></pre>
<p>Read more about the difference between port and targetPort <a href="https://stackoverflow.com/questions/49981601/difference-between-targetport-and-port-in-kubernetes-service-definition">here</a>.</p>
