<p>i'm new to kubernetes , i'm trying to learn it using minikube and i'm facing a problem with accessing apps outside the cluster. i created a  deployment:</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx

  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 8080
</code></pre>

<p>To access it i need to expose it decoratively or imperatively. In the imperative way it works  :</p>

<pre><code>kubectl expose deployment nginx-deployment --port 80 --type NodePort
</code></pre>

<p>When i create a service declaratively i always end up with a <strong>connection refused error</strong>  :</p>

<pre><code>apiVersion: v1

kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  type : NodePort
  ports:
  - port : 8080
    nodePort : 30018
    protocol : TCP
  selector:
    app: nginx
</code></pre>

<p>curl -k <a href="http://NodeIP:NodePort" rel="nofollow noreferrer">http://NodeIP:NodePort</a> returns :</p>

<blockquote>
  <p>curl: (7) Failed to connect to Node IP port NodePORT: Connection
  refused</p>
</blockquote>

Elias
<p>As @Ansil suggested, your <strong>nginx</strong> should be configured to listen on port <code>8080</code> if you want to refer to this port in your <code>Service</code> definition. By default it listens on port <code>80</code>. </p>

<p>You cannot make it listen on different port like <code>8080</code> simply by specifying different <code>containerPort</code> in your <code>Deployment</code> definition as in your example:</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx

  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 8080
</code></pre>

<p>You can easily verify it on your own by attaching to such <code>Pod</code>:</p>

<pre><code>kubectl exec -ti &lt;nginx-pod-name&gt; -- /bin/bash
</code></pre>

<p>Once you're there, run:</p>

<pre><code>ss -ntlp
</code></pre>

<p>And you should see on which port your <strong>nginx</strong> actually listens on.</p>

<p>Additionally you may:</p>

<pre><code>cat /etc/nginx/conf.d/default.conf
</code></pre>

<p>It will also tell you on which port your <strong>nginx</strong> is configured to listen. That's all. It's really simple. You changed <code>containerPort</code> to <code>8080</code> but inside your container nothing actually listens on such port.</p>

<p>You can still expose it as a <code>Service</code> (no matter declaratively or imperatively) but it won't change anything as eventually it points to the wrong port on your container, on which nothing listens and you'll see message similar to this one:</p>

<pre><code>curl: (7) Failed to connect to 10.1.2.3 port 30080: Connection refused
</code></pre>
