<p>I'm using minikube to test kubernetes on latest MacOS.</p>

<p>Here are my relevant YAMLs:</p>

<p><strong>namespace.yml</strong></p>

<pre><code>apiVersion: v1
kind: Namespace
metadata:
  name: micro
  labels:
    name: micro
</code></pre>

<p><strong>deployment.yml</strong></p>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: adderservice
spec:
  replicas: 1
  template:
    metadata:
      labels:
        run: adderservice
    spec:
      containers:
      - name: adderservice
        image: jeromesoung/adderservice:0.0.1
        ports:
        - containerPort: 8080
</code></pre>

<p><strong>service.yml</strong></p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: adderservice
  labels:
    run: adderservice
spec:
  ports:
    - port: 8080
      name: main
      protocol: TCP
      targetPort: 8080
  selector:
    run: adderservice
  type: NodePort
</code></pre>

<p>After running <code>minikube start</code>, the steps I took to deploy is as follows:</p>

<ol>
<li><p><code>kubectl create -f namespace.yml</code> to create the namespace</p></li>
<li><p><code>kubectl config set-context minikube --namespace=micro</code></p></li>
<li><p><code>kubectl create -f deployment.yml</code></p></li>
<li><p><code>kubectl create -f service.yml</code></p></li>
</ol>

<p>Then, I get the NodeIP and NodePort with below commands:</p>

<ol>
<li><code>kubectl get services</code> to get the NodePort</li>
</ol>

<pre><code>$ kubectl get services
NAME           TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
adderservice   NodePort   10.99.155.255   &lt;none&gt;        8080:30981/TCP   21h
</code></pre>

<ol start="2">
<li><code>minikube ip</code> to get the nodeIP</li>
</ol>

<pre><code>$ minikube ip
192.168.99.103
</code></pre>

<p>But when I do curl, I always get <strong>Connection Refused</strong> like this:</p>

<pre><code>$ curl http://192.168.99.103:30981/add/1/2
curl: (7) Failed to connect to 192.168.99.103 port 30981: Connection refused
</code></pre>

<p>So I checked node, pod, deployment and endpoint as follows:</p>

<pre><code>$ kubectl get nodes
NAME       STATUS    ROLES     AGE       VERSION
minikube   Ready     master    23h       v1.13.3

$ kubectl get pods
NAME                            READY     STATUS    RESTARTS   AGE
adderservice-5b567df95f-9rrln   1/1       Running   0          23h

$ kubectl get deployments
NAME           DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
adderservice   1         1         1            1           23h

$ kubectl get endpoints
NAME           ENDPOINTS         AGE
adderservice   172.17.0.5:8080   21h
</code></pre>

<p>I also checked service list from minikube with:</p>

<pre><code>$ minikube service -n micro adderservice --url
http://192.168.99.103:30981
</code></pre>

<p>I've read many posts regarding accessing k8s service via NodePorts. To my knowledge, I should be able to access the app with no problem. The only thing I suspect is that I'm using a custom namespace. Will this cause the access issue?</p>

<p>I know namespace will change the DNS, so, to be complete, I ran below commands also:</p>

<pre><code>$ kubectl exec -ti adderservice-5b567df95f-9rrln -- nslookup kubernetes.default
Server:     10.96.0.10
Address:    10.96.0.10#53

Name:   kubernetes.default.svc.cluster.local
Address: 10.96.0.1

$ kubectl exec -ti adderservice-5b567df95f-9rrln -- nslookup kubernetes.micro
Server:     10.96.0.10
Address:    10.96.0.10#53

Non-authoritative answer:
Name:   kubernetes.micro
Address: 198.105.244.130
Name:   kubernetes.micro
Address: 104.239.207.44
</code></pre>

<p>Could anyone help me out? Thank you.</p>

<p>The error <code>Connection Refused</code> mostly means that the application inside the container does not accept requests on the targeted interface or not mapped through the expected ports.</p>

<p>Things you need to be aware of:</p>

<ul>
<li>Make sure that your application bind to <code>0.0.0.0</code> so it can receive requests from outside the container either externally as in public or through other containers.</li>
<li>Make sure that your application is actually listening on the <code>containerPort</code> and <code>targetPort</code> as expect</li>
</ul>

<p>In your case you have to make sure that <code>ADDERSERVICE_SERVICE_HOST</code> equals to <code>0.0.0.0</code> and <code>ADDERSERVICE_SERVICE_PORT</code> equals to <code>8080</code> which should be the same value as <code>targetPort</code> in <code>service.yml</code> and <code>containerPort</code> in <code>deployment.yml</code></p>
