<p>I'm running a local kubernetes bundled with docker on Mac OS.</p>

<p>How can I expose a service, so that I can access the service via a browser on my Mac?</p>

<p>I've created:</p>

<p>a) deployment including apache httpd.</p>

<p>b) service via yaml:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: apaches
spec:
  selector:
    app: web
  type: NodePort
  ports:
  - protocol: TCP
    port: 80
  externalIPs:
  - 192.168.1.10 # Network IP of my Mac
</code></pre>

<p>My service looks like:</p>

<pre><code>$ kubectl get service apaches
NAME      TYPE       CLUSTER-IP       EXTERNAL-IP    PORT(S)        AGE
apaches   NodePort   10.102.106.158   192.168.1.10   80:31137/TCP   14m
</code></pre>

<p>I can locally access the service in my kubernetes cluster by <code>wget $CLUSTER-IP</code></p>

<p>I tried to call <a href="http://192.168.1.10/" rel="noreferrer">http://192.168.1.10/</a> on my Mac, but it doesn't work.  </p>

<p>This <a href="https://stackoverflow.com/questions/46618683/kubernetes-minikube-cannot-expose-service-on-public-ip-range">question</a> deals to a similar issue. But the solution does not help, because I do not know which IP I can use.</p>

<p><strong>Update</strong></p>

<p>Thanks to Michael Hausenblas I worked out a solution using <a href="https://kubernetes.io/docs/concepts/services-networking/ingress" rel="noreferrer">Ingress</a>.
Nevertheless there are still some open questions:</p>

<ul>
<li>What is the meaning of a service's externalIP? Why do I need an externalIP when I do not directly access a service from external?</li>
<li>What is the meaning of the service port 31137?</li>
<li><ul>
<li>The kubernetes docs describe a method to [publish a service in minikube via NodePort][4]. Is this also possible with kubernetes bundled on docker?</li>
</ul></li>
</ul>

<p>There are several solutions to expose services in kubernetes:
<a href="http://alesnosek.com/blog/2017/02/14/accessing-kubernetes-pods-from-outside-of-the-cluster/" rel="noreferrer">http://alesnosek.com/blog/2017/02/14/accessing-kubernetes-pods-from-outside-of-the-cluster/</a></p>

<p>Here are my solutions according to alesnosek for a local kubernetes bundled with docker:</p>

<p><strong>1. hostNetwork</strong></p>

<pre><code>hostNetwork: true
</code></pre>

<p>Dirty (the host network should not be shared for security reasons) => I did not check this solution.</p>

<p><strong>2. hostPort</strong></p>

<pre><code>hostPort: 8086
</code></pre>

<p>Does not apply to services => I did not check this solution.</p>

<p><strong>3. NodePort</strong></p>

<p>Expose the service by defining a nodePort:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: apaches
spec:
  type: NodePort
  ports:
    - port: 80
      nodePort: 30000
  selector:
    app: apache
</code></pre>

<p><strong>4. LoadBalancer</strong></p>

<p><strong>EDIT</strong>
@MathObsessed posted the <a href="https://stackoverflow.com/a/60111530/1909531">solution in his anwer</a>.</p>

<p><strong>5. Ingress</strong></p>

<p><strong>a. Install <a href="https://github.com/jnewland/local-dev-with-docker-for-mac-kubernetes" rel="noreferrer">Ingress Controller</a></strong></p>

<pre><code>git clone https://github.com/jnewland/local-dev-with-docker-for-mac-kubernetes.git

kubectl apply -f nginx-ingress/namespaces/nginx-ingress.yaml -Rf nginx-ingress
</code></pre>

<p><strong>b. Configure Ingress</strong></p>

<p><code>kubectl apply -f apache-ing.yaml</code></p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: apache-ingress
spec:
  rules:
  - host: localhost
    http:
      paths:
      - path: /
        backend:
          serviceName: apaches
          servicePort: 80
</code></pre>

<p>Now I can access my apache deployed with kubernetes by calling <a href="http://localhost/" rel="noreferrer">http://localhost/</a></p>

<p><strong>Remarks for using <a href="https://github.com/jnewland/local-dev-with-docker-for-mac-kubernetes" rel="noreferrer">local-dev-with-docker-for-mac-kubernetes</a></strong></p>

<ul>
<li>The repo simplifies the <a href="https://github.com/kubernetes/ingress-nginx/blob/master/deploy/README.md" rel="noreferrer">deployment of the offical ingress-nginx controller</a> </li>
<li>For production use I would follow the <a href="https://github.com/kubernetes/ingress-nginx/blob/master/deploy/README.md" rel="noreferrer">official guide</a>.</li>
<li>The repos ships with a <a href="https://github.com/jnewland/local-dev-with-docker-for-mac-kubernetes/tree/master/httpbin" rel="noreferrer">tiny full featured ingress example</a>. Very useful for getting quickly a working example application.</li>
</ul>

<p><strong>Further documentation</strong></p>

<ul>
<li><a href="https://kubernetes.io/docs/concepts/services-networking/ingress" rel="noreferrer">https://kubernetes.io/docs/concepts/services-networking/ingress</a></li>
</ul>
