<p>I am using kubernetes. I have Ingress service which talks my container service. We have exposed a webapi which works all fine. But we keep getting 502 bad gateway error. I am new to kubernetes and i have no clue how to go about debugging this issue. Server is a nodejs server connected to database. Is there anything wrong with configuration?</p>

<p>My Deployment file--</p>

<hr>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-pod
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: my-pod
    spec:
      containers:
      - name: my-pod
        image: my-image
        ports:
        - name: "http"
          containerPort: 8086
        resources:
          limits:
            memory: 2048Mi
            cpu: 1020m
---
apiVersion: v1
kind: Service
metadata:
  name: my-pod-serv
spec:
  ports:
    - port: 80
      targetPort: "http"
  selector:
     app: my-pod
</code></pre>

<hr>

<p>My Ingress Service:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: gateway
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: abc.test.com
    http:
      paths:
      - path: /abc
        backend:
          serviceName: my-pod-serv
          servicePort: 80
</code></pre>

Hacker
<p><strong>In Your case:</strong></p>
<p>I think that you get this 502 gateway error because you don't have Ingress controller configured correctly.
Please try do do it with installed Ingress like in example below. It will do all automatically.</p>
<p><strong>Nginx Ingress step by step:</strong></p>
<p><strong>1)</strong> <a href="https://helm.sh/docs/using_helm/#installing-helm" rel="nofollow noreferrer">Install helm</a></p>
<p><strong>2)</strong> Install nginx controller using helm</p>
<pre><code>$ helm install stable/nginx-ingress --name nginx-ingress
</code></pre>
<p>It will create 2 services. You can get their details via</p>
<pre><code>$ kubectl get svc
NAME                            TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)                      AGE
kubernetes                      ClusterIP      10.39.240.1     &lt;none&gt;          443/TCP                      29d
nginx-ingress-controller        LoadBalancer   10.39.243.140   35.X.X.15   80:32324/TCP,443:31425/TCP       19m
nginx-ingress-default-backend   ClusterIP      10.39.252.175   &lt;none&gt;          80/TCP                       19m
</code></pre>
<p><a href="https://github.com/kubernetes/ingress-nginx" rel="nofollow noreferrer">nginx-ingress-controller</a> - in short, it's dealing with requests to Ingress and directing</p>
<p><a href="https://kubernetes.github.io/ingress-nginx/user-guide/default-backend/" rel="nofollow noreferrer">nginx-ingress-default-backend</a> - in short, default backend is a service which handles all URL paths and hosts the nginx controller doesn't understand</p>
<p><strong>3)</strong> Create 2 deployments (or use yours)</p>
<pre><code>$ kubectl run my-pod --image=nginx
deployment.apps/my-pod created

$ kubectl run nginx1 --image=nginx
deployment.apps/nginx1 created
</code></pre>
<p><strong>4)</strong> Connect to one of the pods</p>
<pre><code>$ kubectl exec -ti my-pod-675799d7b-95gph bash
</code></pre>
<p>And add additional line to the output to see which one we will try to connect later.</p>
<pre><code>$ echo &quot;HELLO THIS IS INGRESS TEST&quot; &gt;&gt; /usr/share/nginx/html/index.html 
$ exit
</code></pre>
<p><strong>5)</strong> Expose deployments.</p>
<pre><code>$ kubectl expose deploy nginx1 --port 80
service/nginx1 exposed
$ kubectl expose deploy my-pod --port 80
service/my-pod exposed
</code></pre>
<p>This will automatically create service and will looks like</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  labels:
    app: my-pod
  name: my-pod
  selfLink: /api/v1/namespaces/default/services/my-pod
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: my-pod
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}
</code></pre>
<p><strong>6)</strong> Now its the time to create Ingress.yaml and deploy it. Each rule in ingress need to be specified. Here I have 2 services. Each service specification starts with -host under rule parameter.</p>
<p><strong>Ingress.yaml</strong></p>
<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: two-svc-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: my.pod.svc
    http:
      paths:
      - path: /pod
        backend:
          serviceName: my-pod
          servicePort: 80
  - host: nginx.test.svc
    http:
      paths:
      - path: /abc
        backend:
          serviceName: nginx1
          servicePort: 80

$ kubectl apply -f Ingress.yaml
ingress.extensions/two-svc-ingress created
</code></pre>
<p><strong>7)</strong> You can check Ingress and hosts</p>
<pre><code>$ kubectl get ingress
NAME              HOSTS                       ADDRESS        PORTS     AGE
two-svc-ingress   my.pod.svc,nginx.test.svc   35.228.230.6   80        57m
</code></pre>
<p><strong>8)</strong> Eplanation why I installed Ingress.</p>
<p>Connect to the ingress controller pod</p>
<pre><code>$ kubectl exec -ti nginx-ingress-controller-76bf4c745c-prp8h bash
www-data@nginx-ingress-controller-76bf4c745c-prp8h:/etc/nginx$ cat /etc/nginx/nginx.conf
</code></pre>
<p>Because I have installed nginx ingress earlier, after deploying Ingress.yaml, the nginx-ingress-controller found changes and automatically added necessary code.
In this file you should be able to find whole configuration for two services. I will not copy configuration but only headers.</p>
<blockquote>
<p>start server my.pod.svc</p>
<p>start server nginx.test.svc</p>
</blockquote>
<p>www-data@nginx-ingress-controller-76bf4c745c-prp8h:/etc/nginx$ exit</p>
<p><strong>9)</strong> Test</p>
<pre><code>$ kubectl get svc to get your nginx-ingress-controller external IP
$ curl -H &quot;HOST: my.pod.svc&quot; http://35.X.X.15/
default backend - 404

$ curl -H &quot;HOST: my.pod.svc&quot; http://35.X.X.15/pod
&lt;!DOCTYPE html&gt;
...
&lt;/html&gt;
HELLO THIS IS INGRESS TEST
</code></pre>
<p>Please keep in mind Ingress needs to be in the same namespace like services. If you have a few services in many namespace you need to create Ingress for each namespace.</p>
