<p>I have an alpine docker image to run my raw PHP website on an apache server(PHP 7.4) <code>EXPOSE 80</code>.<br>
I want to run the image on Kubernetes(GKE) with an ingress controller.<br>
I'm pushing the image with gcloud command to the google container registry.<br>
Both the deployment and service have no errors and created successfully as NodePort.<br>
The ingress I deployed is from google tutorials(<a href="https://cloud.google.com/community/tutorials/nginx-ingress-gke" rel="nofollow noreferrer">https://cloud.google.com/community/tutorials/nginx-ingress-gke</a>)<br>
In my ingress now there is:<br></p>
<ul>
<li>34.68.78.46.xip.io/</li>
<li>34.68.78.46.xip.io/hello</li>
<li>34.68.78.46.xip.io/jb(/|$)(.*)</li>
</ul>
<p>The /hello is the same configuration of the tutorial and it is working fine.<br>
The /jb is the same configuration as I mentioned below and always returning 502 Error.<br>
Ingress details in the GCP console show no warnings or errors<br></p>
<p>I have checked:<br>
<a href="https://stackoverflow.com/questions/66110561/kubernetes-gke-ingress-502-server-error">Kubernetes GKE Ingress : 502 Server Error</a><br>
<a href="https://stackoverflow.com/questions/49540280/gke-ingress-502-error-when-downloading-file">GKE Ingress: 502 error when downloading file</a><br>
<a href="https://stackoverflow.com/questions/50368210/502-server-error-google-kubernetes">502 Server Error Google kubernetes</a></p>
<p>Here is the deployment file:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: jomlahbazar-deployment
spec:
  selector:
    matchLabels:
      greeting: jomlah
      department: bazar
  replicas: 1
  template:
    metadata:
      labels:
        greeting: jomlah
        department: bazar
    spec:
      containers:
      - name: jomlah
        image: &quot;us.gcr.io/third-nature-273904/jb-img-1-0:v1&quot;
        ports:
        - containerPort: 80
        env:
        - name: &quot;PORT&quot;
          value: &quot;80&quot;
</code></pre>
<p>Here is the service file:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: jomlahbazar-service
spec:
  type: NodePort
  selector:
    greeting: jomlah
    department: bazar
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
</code></pre>
<p>Here in the ingress file:</p>
<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-resource
  annotations:
    kubernetes.io/ingress.class: &quot;nginx&quot;
    nginx.ingress.kubernetes.io/ssl-redirect: &quot;false&quot;
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    nginx.ingress.kubernetes.io/use-regex: &quot;true&quot;
    nginx.ingress.kubernetes.io/add-base-url : &quot;true&quot;
spec:
  rules:
  - host: 34.68.78.46.xip.io
    http:
      paths:
      - path: /
        backend:
          serviceName: jomlahbazar-service
          servicePort: 80
      - path: /hello
        backend:
          serviceName: hello-app
          servicePort: 8080
      - path: /jb(/|$)(.*)
        backend:
          serviceName: jomlahbazar-service
          servicePort: 80     
</code></pre>
<p>Here is the ingress description:</p>
<pre><code>Name:             ingress-resource
Namespace:        default
Address:          34.68.78.46
Default backend:  default-http-backend:80 (10.20.1.6:8080)
Rules:
  Host                Path  Backends
  ----                ----  --------
  34.68.78.46.xip.io
                      /              jomlahbazar-service:80 (&lt;none&gt;)
                      /hello         hello-app:8080 (10.20.2.61:8080)
                      /jb(/|$)(.*)   jomlahbazar-service:80 (&lt;none&gt;)
Annotations:
  kubernetes.io/ingress.class:                       nginx
  nginx.ingress.kubernetes.io/add-base-url:          true
  nginx.ingress.kubernetes.io/rewrite-target:        /$2
  nginx.ingress.kubernetes.io/ssl-redirect:          false
  nginx.ingress.kubernetes.io/use-regex:             true
  kubectl.kubernetes.io/last-applied-configuration:  {&quot;apiVersion&quot;:&quot;networking.k8s.io/v1beta1&quot;,&quot;kind&quot;:&quot;Ingress&quot;,&quot;metadata&quot;:{&quot;annotations&quot;:{&quot;kubernetes.io/ingress.class&quot;:&quot;nginx&quot;,&quot;nginx.ingress.kubernetes.io/add-base-url&quot;:&quot;true&quot;,&quot;nginx.ingress.kubernetes.io/rewrite-target&quot;:&quot;/$2&quot;,&quot;nginx.ingress.kubernetes.io/ssl-redirect&quot;:&quot;false&quot;,&quot;nginx.ingress.kubernetes.io/use-regex&quot;:&quot;true&quot;},&quot;name&quot;:&quot;ingress-resource&quot;,&quot;namespace&quot;:&quot;default&quot;},&quot;spec&quot;:{&quot;rules&quot;:[{&quot;host&quot;:&quot;34.68.78.46.xip.io&quot;,&quot;http&quot;:{&quot;paths&quot;:[{&quot;backend&quot;:{&quot;serviceName&quot;:&quot;jomlahbazar-service&quot;,&quot;servicePort&quot;:80},&quot;path&quot;:&quot;/&quot;},{&quot;backend&quot;:{&quot;serviceName&quot;:&quot;hello-app&quot;,&quot;servicePort&quot;:8080},&quot;path&quot;:&quot;/hello&quot;},{&quot;backend&quot;:{&quot;serviceName&quot;:&quot;jomlahbazar-service&quot;,&quot;servicePort&quot;:80},&quot;path&quot;:&quot;/jb(/|$)(.*)&quot;}]}}]}}

Events:
  Type    Reason          Age                 From                      Message
  ----    ------          ----                ----                      -------
  Normal  AddedOrUpdated  36m (x6 over 132m)  nginx-ingress-controller  Configuration for default/ingress-resource was added or updated
</code></pre>
<p>The output of kubectl get ing ingress-resource -o yaml</p>
<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {&quot;apiVersion&quot;:&quot;networking.k8s.io/v1beta1&quot;,&quot;kind&quot;:&quot;Ingress&quot;,&quot;metadata&quot;:{&quot;annotations&quot;:{&quot;kubernetes.io/ingress.class&quot;:&quot;nginx&quot;,&quot;nginx.ingress.kubernetes.io/add-base-url&quot;:&quot;true&quot;,&quot;nginx.ingress.kubernetes.io/rewrite-target&quot;:&quot;/$2&quot;,&quot;nginx.ingress.kubernetes.io/ssl-redirect&quot;:&quot;false&quot;,&quot;nginx.ingress.kubernetes.io/use-regex&quot;:&quot;true&quot;},&quot;name&quot;:&quot;ingress-resource&quot;,&quot;namespace&quot;:&quot;default&quot;},&quot;spec&quot;:{&quot;rules&quot;:[{&quot;host&quot;:&quot;34.68.78.46.xip.io&quot;,&quot;http&quot;:{&quot;paths&quot;:[{&quot;backend&quot;:{&quot;serviceName&quot;:&quot;jomlahbazar-service&quot;,&quot;servicePort&quot;:80},&quot;path&quot;:&quot;/&quot;},{&quot;backend&quot;:{&quot;serviceName&quot;:&quot;hello-app&quot;,&quot;servicePort&quot;:8080},&quot;path&quot;:&quot;/hello&quot;},{&quot;backend&quot;:{&quot;serviceName&quot;:&quot;jomlahbazar-service&quot;,&quot;servicePort&quot;:80},&quot;path&quot;:&quot;/jb(/|$)(.*)&quot;}]}}]}}
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/add-base-url: &quot;true&quot;
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    nginx.ingress.kubernetes.io/ssl-redirect: &quot;false&quot;
    nginx.ingress.kubernetes.io/use-regex: &quot;true&quot;
  creationTimestamp: &quot;2021-02-11T06:00:07Z&quot;
  generation: 5
  name: ingress-resource
  namespace: default
  resourceVersion: &quot;2195351&quot;
  selfLink: /apis/extensions/v1beta1/namespaces/default/ingresses/ingress-resource
  uid: 74dc822f-91cb-4902-991b-1ad298f44ae6
spec:
  rules:
  - host: 34.68.78.46.xip.io
    http:
      paths:
      - backend:
          serviceName: jomlahbazar-service
          servicePort: 80
        path: /
      - backend:
          serviceName: hello-app
          servicePort: 8080
        path: /hello
      - backend:
          serviceName: jomlahbazar-service
          servicePort: 80
        path: /jb(/|$)(.*)
status:
  loadBalancer:
    ingress:
    - ip: 34.68.78.46
</code></pre>

<p>I've run some tests on my GKE cluster. I've replicate your behavior using 2 hello world applications v1 and v2.</p>
<h2>Scenario 1</h2>
<p>HW 1</p>
<pre><code>    spec:
      containers:
      - name: hello1
        image: gcr.io/google-samples/hello-app:1.0
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
</code></pre>
<p>SVC HW1</p>
<pre><code>spec:
  type: NodePort
  selector:
    key: app
  ports:
    - port: 80
      targetPort: 8080
</code></pre>
<p>HW 2</p>
<pre><code>    spec:
      containers:
      - name: hello2
        image: gcr.io/google-samples/hello-app:2.0
        env:
        - name: &quot;PORT&quot;
          value: &quot;80&quot;
</code></pre>
<p>SVC HW2</p>
<pre><code>spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 8080
    protocol: TCP
  selector:
    app: hello2
</code></pre>
<p>Ingress</p>
<pre><code>spec:
  rules:
  -  http:
      paths:
      - path: /hello2
        backend:
          serviceName: h2
          servicePort: 80
      - path: /hello
        backend:
          serviceName: fs
          servicePort: 80
</code></pre>
<p>Outputs:</p>
<pre><code>$ curl 34.117.70.75/hello
Hello, world!
Version: 1.0.0
Hostname: fd-c6d79cdf8-7rmmd

$ curl 34.117.70.75/hello2

&lt;html&gt;&lt;head&gt;
&lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html;charset=utf-8&quot;&gt;
&lt;title&gt;502 Server Error&lt;/title&gt;
&lt;/head&gt;
&lt;body text=#000000 bgcolor=#ffffff&gt;
&lt;h1&gt;Error: Server Error&lt;/h1&gt;
&lt;h2&gt;The server encountered a temporary error and could not complete your request.&lt;p&gt;Please try again in 30 seconds.&lt;/h2&gt;
&lt;h2&gt;&lt;/h2&gt;
&lt;/body&gt;&lt;/html&gt;
</code></pre>
<p>In this scenario, deployment was configured to create pod which will listen on <code>port: 80</code>. As you skipped configureing <code>containerPort</code> in deployment, Kubernetes automatically used the same port in <code>containerPort</code> as was set in <code>port</code>. You can verify it using <code>netstat</code> command.</p>
<pre><code>$ kubectl exec -ti h2-deploy-6dbf5b7899-g7rbj -- bin/sh
/ # netstat -plnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 :::80                   :::*                    LISTEN      1/hello-app
/ #
</code></pre>
<p>In your service you set <code>containerPort: 8080</code>, so service expect traffic to go trought port <code>8080</code>. As pod is listening only on <code>80</code> and traffic goes trought <code>8080</code> you are getting <code>502</code> error.</p>
<h2>Scenario 2</h2>
<p>After change value from &quot;80&quot; to &quot;8080&quot; and apply new configuration.</p>
<pre><code>$ curl 34.117.70.75/hello
Hello, world!
Version: 1.0.0
Hostname: fd-c6d79cdf8-7rmmd

$ curl 34.117.70.75/hello2
Hello, world!
Version: 2.0.0
Hostname: h2-deploy-5f5ccfbf9f-fjhrb
</code></pre>
<p>Netstat:</p>
<pre><code>$ kubectl exec -ti f2-deploy-5f5ccfbf9f-fjhrb -- bin/sh
/ # netstat -plnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 :::8080                 :::*                    LISTEN      1/hello-app
</code></pre>
<h2>Solution</h2>
<p><strong>Solution 1</strong></p>
<p>You should change your application deployment to:</p>
<pre><code>        env:
        - name: &quot;PORT&quot;
          value: &quot;8080&quot;
</code></pre>
<p>and apply new configuration.</p>
<p><strong>Solution 2</strong>
Use <code>containerPort</code></p>
<pre><code>    spec:
      containers:
      - name: jb
        image: &quot;us.gcr.io/third-nature-273904/jb-img-1-0:v3&quot;
        ports:
        - containerPort: 8080
</code></pre>
<p><strong>Note</strong></p>
<p>Please note, if you created your own image and used <code>EXPOSE</code> in your <code>Dockerfile</code>, you should configure your Deployment to use this specific port.</p>
<p>Let me know if you still have issue.</p>
