<p>Hello Im learning kubernetes with the minikube. I can access a service via minikubeip:NodePort on the machine where the minikube is running and now I want to access the Service via LAN from other machine. I tried ingress but it didn't work for me.</p>
<p>Deployment file:</p>
<pre><code>---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: aspnetapp-deployment
  labels:
    app: aspnetapp
spec:
  replicas: 2
  selector:
    matchLabels:
      app: aspnetapp
  template:
    metadata:
      labels:
        app: aspnetapp
    spec:
      containers:
        - name: aspnetapp-cn
          image: localhost:5000/aspnetapp
          ports: 
            - containerPort: 80
</code></pre>
<p>Service file:</p>
<pre><code>---
apiVersion: v1
kind: Service
metadata:
    name: aspnetapp-service
spec:
    type: NodePort
    ports:
    - name: http
      targetport: 80      
      port: 80
      protocol: TCP
    selector:
      app: aspnetapp
</code></pre>
<p>Ingress file:</p>
<pre><code>---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: aspnetapp-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - host:
      http:
        paths:
        - path: /aspnetapp
          backend:
            serviceName: aspnetapp-service
            servicePort: 80
</code></pre>

<p>To expose your application to LAN with Ubuntu with a <code>--docker</code> driver you can use:</p>
<ul>
<li><code>$ kubectl port-forward ...</code></li>
</ul>
<blockquote>
<p>Disclaimer!</p>
<ol>
<li>Your <code>$ kubectl port-forward</code> should be run on a <strong>host</strong> running minikube.</li>
<li>Command above will operate continuously (<code>&amp;</code> can be used to run it in a background)</li>
</ol>
</blockquote>
<p>Example:</p>
<p>Let's assume that you have an Ubuntu machine with IP: <code>192.168.0.115</code>.</p>
<p>I've created an example using <code>nginx</code> image:</p>
<p><code>Deployment.yaml</code></p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
</code></pre>
<p>As for the service exposing your <code>Deployment</code> you can <strong>either</strong>:</p>
<ul>
<li>Use following command:
<ul>
<li><code>$ kubectl expose deployment nginx --port=80 --type=NodePort</code></li>
</ul>
</li>
<li>Use definition below:</li>
</ul>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  type: NodePort
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80   
</code></pre>
<hr />
<hr />
<p>You can expose your <code>nginx</code> in two ways:</p>
<ul>
<li>Directly with <code>$ kubectl port-forward</code>.</li>
<li>Directing the traffic to the <code>Ingress</code> controller.</li>
</ul>
<hr />
<h3>Direct access</h3>
<p>You can expose your <code>Service</code> directly without using <code>Ingress</code> by:</p>
<ul>
<li><code>$ kubectl port-forward --address=0.0.0.0 deployment/nginx 10000:80</code></li>
</ul>
<p>Dissecting above command:</p>
<ul>
<li><code>--address=0.0.0.0</code> - expose outside of localhost</li>
<li><code>deployment/nginx</code> - resource/resource_name</li>
<li><code>10000:80</code> - port on host machine/port on pod to send the traffic to</li>
</ul>
<blockquote>
<p>Assigning local ports under 1024 will need root access!</p>
<p>You will need to login to root and either copy <code>.kube/config</code> to <code>/root/</code> directory or specify where <code>kubectl</code> should look for config!</p>
</blockquote>
<p>After running above command you should be able to run:</p>
<ul>
<li><code>curl 192.168.1.115:10000</code></li>
</ul>
<p>Command <code>$ kubectl port-forward</code> will generate:</p>
<pre class="lang-sh prettyprint-override"><code>Forwarding from 0.0.0.0:10000 -&gt; 80 # AT THE START
Handling connection for 10000 # CURL FROM 192.168.0.2
</code></pre>
<hr />
<h3>Directing the traffic to the <code>Ingress</code> controller</h3>
<blockquote>
<p>You need to run <code>$ minikube addons enable ingress</code> to have functionalities of <code>Ingress</code> resource</p>
</blockquote>
<p>In your example you used <code>Ingress</code> resource. In this situation you should:</p>
<ul>
<li>Create <code>Ingress</code> resource (as you did).</li>
</ul>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress
spec:
  rules:
  - host:
    http:
      paths:
      - path: /
        backend:
          serviceName: nginx
          servicePort: 80
</code></pre>
<ul>
<li>Forward the traffic to the <code>Ingress</code> controller!</li>
</ul>
<p><code>Ingress</code> controller after receiving the traffic will forward it further (to your <code>Service</code> and then to <code>Pod</code>)</p>
<p>To forward the traffic to your <code>Ingress</code> controller run this command:</p>
<ul>
<li><code>kubectl port-forward --address=0.0.0.0 --namespace=kube-system deployment/ingress-nginx-controller 80:80</code></li>
</ul>
<p>Dissecting above command once more:</p>
<ul>
<li><code>--address=0.0.0.0</code> - expose outside of localhost</li>
<li><code>--namespace=kube-system</code> - namespace that the <code>Deployment</code> of <code>Ingress</code> controller resides in</li>
<li><code>deployment/ingress-nginx-controller</code> - resource/resource-name</li>
<li><code>80:80</code> - port on host machine/port on pod to send the traffic to</li>
</ul>
<p>Command <code>$ kubectl port-forward</code> will generate:</p>
<pre class="lang-sh prettyprint-override"><code>Forwarding from 0.0.0.0:80 -&gt; 80 # AT THE START 
Handling connection for 80 # CURL FROM 192.168.0.2
</code></pre>
<hr />
<p>I also encourage you to use different <code>--driver</code> like for example Virtualbox. You will be able to expose your application without <code>$ kubectl port-forward</code> (NAT).</p>
<p>Additional resources:</p>
<ul>
<li><em><a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="nofollow noreferrer">Kubernetes.io: Service</a></em></li>
<li><em><a href="https://stackoverflow.com/questions/51468491/how-kubectl-port-forward-works">Stackoverflow.com: How kubectl port-forward works</a></em></li>
</ul>
