
<p>I have a kubernetes setup with the configuration like below:</p>
<pre class="lang-yaml prettyprint-override"><code>#---
kind: Service
apiVersion: v1
metadata:
  name: myservice
spec:
  selector:
    app: my-service
  ports:
    - protocol: &quot;TCP&quot;
      # Port accessible inside cluster
      port: 8080
      # Port to forward to inside the pod
      targetPort: 80



---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-service
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: my-service
    spec:
      containers:
        - name: my-service
          image: my-custom-docker-regisry/my-service:latest
          imagePullPolicy: Always
          ports:
            - containerPort: 8080
      imagePullSecrets:
      - name: regcred
</code></pre>
<p>and my ingress:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  annotations:
    nginx.ingress.kubernetes.io/ssl-redirect: &quot;false&quot;
spec:
  rules:
  - http:
      paths:
      - path: /myservice
        backend:
          serviceName: myservice
          servicePort: 80
</code></pre>
<p>What I tried to do is pulling the image from my docker registry and run it in the kubernetes. I have configured here one deployment and one service and expose the service to the outside with the ingress.</p>
<p>My minikube is running under ip 192.168.99.100 and when I tried to access my application with address: curl 192.168.99.100:80/myservice, I got 502 Bad Gateway.</p>
<p>Does anyone have an idea why it happended or did I do something wrong with the configuration? Thank you in advanced!</p>

Ock
<p>Your ingress targets this service:</p>

<pre><code>     serviceName: myservice
     servicePort: 80
</code></pre>

<p>but the service named <code>myservice</code> exposes port <code>8080</code> rather than <code>80</code>:</p>

<pre><code>  ports:
    - protocol: "TCP"
      # Port accessible inside cluster
      port: 8080
      # Port to forward to inside the pod
      targetPort: 80
</code></pre>

<p>Your ingress should point to one of the ports exposed by the service.</p>

<p>Also, the service itself targets port 80, but the pods in your deployment seem to expose port 8080, rather than 80:</p>

<pre><code>  containers:
    - name: my-service
      image: my-custom-docker-regisry/my-service:latest
      imagePullPolicy: Always
      ports:
        - containerPort: 8080
</code></pre>

<p>So long story short, looks like you could swap <code>port</code> with <code>targetPort</code> in your <code>service</code> so that:</p>

<ul>
<li>the pods expose port <code>8080</code></li>
<li>the service exposes port <code>8080</code> of all the pods under service name <code>myservice</code> port <code>80</code>,</li>
<li>the ingress configures nginx to proxy your traffic to service <code>myservice</code> port <code>80</code>.</li>
</ul>
