<p>Firstly, this is my folder:</p>
<p><a href="https://i.stack.imgur.com/0zvFX.png" rel="noreferrer"><img src="https://i.stack.imgur.com/0zvFX.png" alt="enter image description here" /></a></p>
<p>This is my Dockerfile:</p>
<pre><code>FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build-env
WORKDIR /app

COPY *.csproj ./
RUN dotnet restore

COPY . ./
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/aspnet:5.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT [&quot;dotnet&quot;, &quot;PlatformService.dll&quot;]
</code></pre>
<p>platforms-depl.yaml (deployment file)</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: platforms-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: platformservice
  template:
    metadata:
      labels:
        app: platformservice
    spec:
      containers:
        - name: platformservice
          image: hao14102000/platformservice:latest
</code></pre>
<p>platforms-np-srv.yaml (NodePort Service file)</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: platformnpservice-srv
spec:
  type: NodePort
  selector:
    app: platformservice
  ports:
    - name: platformservice
      protocol: TCP
      port: 80
      targetPort: 80

</code></pre>
<p>When I apply 2 files this is what I see:</p>
<p><a href="https://i.stack.imgur.com/Tf5KP.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Tf5KP.png" alt="enter image description here" /></a></p>
<p>When I try to connect on port <code>31023</code> using both options below:</p>
<pre><code>http://localhost:31023/api/platforms
http://10.109.215.230:31023/api/platforms
</code></pre>
<p>It doesn't work. This happens:</p>
<p><a href="https://i.stack.imgur.com/IR7Xg.png" rel="noreferrer"><img src="https://i.stack.imgur.com/IR7Xg.png" alt="enter image description here" /></a></p>
<p><a href="https://i.stack.imgur.com/mjW6Y.png" rel="noreferrer"><img src="https://i.stack.imgur.com/mjW6Y.png" alt="enter image description here" /></a></p>
<p>I don't know what wrong with this...</p>

<h2>What happens with Linux containers:</h2>
<p>Kubernetes on Windows Docker Desktop by default runs its components in WSL2 (Windows subsystem for Linux), it's separate virtual machine with its own IP address and <code>localhost</code>. This is the reason why service is not reachable on <code>localhost</code> from host OS (in this case Windows).</p>
<p>Another option is to disable <code>using WSL2 based engine</code> in settings, instead <code>hyper-v</code> will be used and virtual machine will be created however in Docker Desktop it's said that preferably WSL2 should be used for performance benefits.</p>
<h2>Available options how to access the service using WSL2:</h2>
<ol>
<li>Fastest and easiest (loadbalancer)</li>
</ol>
<p>Set up a <code>service</code> with <code>LoadBalancer</code> type. <code>EXTERNAL-IP</code> will be localhost which solves all questions immediately. For example:</p>
<pre><code>kubectl get svc
NAME         TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP      10.96.0.1      &lt;none&gt;        443/TCP          11m
nginx        LoadBalancer   10.110.15.53   localhost     8080:30654/TCP   4m10s
</code></pre>
<p>Nginx is available in browser on <code>localhost:8080</code>.</p>
<ol start="2">
<li>Using virtual machine's IP and nodeport</li>
</ol>
<p>Another option is to find <code>WSL</code> virtual machine and then access service on this IP and <code>nodeport</code>.</p>
<p>To find WSL VM address, you need to run <code>wsl</code> command to connect to this VM and then find its IP address:</p>
<pre><code>wsl

# ip a | grep eth0

6: eth0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc mq state UP group default qlen 1000
    inet 172.19.xxx.yyy/20 brd 172.19.xxx.yyy scope global eth0
</code></pre>
<p>Nginx is available in browser on <code>172.19.xxx.yyy:30654</code>.</p>
<ol start="3">
<li>Port-forward - for testing purposes</li>
</ol>
<p><code>Port-forward</code> is useful for testing purposes, but it shouldn't be used on production systems.</p>
<p>To start the proxy to the service, run following command:</p>
<pre><code>kubectl port-forward service/nginx 8080:80 &amp;
</code></pre>
<p>Nginx is available in browser on <code>localhost:8080</code></p>
<h2>Assumptions when Hyper-V is used</h2>
<p>First <code>hyper-v</code> should be installed on host machine. Note that not all versions of Windows are supported. Please refer to documentation on which versions and how to enable <code>hyper-v</code> <a href="https://learn.microsoft.com/en-us/virtualization/hyper-v-on-windows/quick-start/enable-hyper-v" rel="noreferrer">here</a>.</p>
<p>When <code>using WSL2 based engine</code> is deselected, <code>hyper-v</code> is used to work with containers. It creates a separate virtual machine which can be found in <code>Hyper-v Manager</code>.</p>
<ul>
<li><code>nodeport</code> works on localhost + nodeport</li>
<li><code>loadbalancer</code> doesn't work, you can't connect to <code>localhost</code> with service port even though <code>External-IP</code> shows <code>localhost</code>.</li>
</ul>
<h2>Windows containers on Windows Docker Desktop</h2>
<p>It's also possible to run Windows containers on Windows Docker Desktop.</p>
<p>It's required to change daemon which will be used. In tray select on <code>switch to Windows containers</code>. <a href="https://docs.docker.com/desktop/windows/#switch-between-windows-and-linux-containers" rel="noreferrer">Switch between linux and windows containers</a>.</p>
<p>However <code>kubernetes</code> option will become unavailable, because <code>control plane</code> components are designed to be run on <code>linux</code> host.</p>
<h2>Environment:</h2>
<p><strong>OS</strong>: Windows 10 Enterprise, build: 19041.1165</p>
<p><strong>Docker Desktop</strong>: 4.0.0 (67817)</p>
<p><strong>Engine</strong>: 20.10.8</p>
<p><strong>Kubernetes</strong>: 1.21.4</p>
<h2>Useful links:</h2>
<ul>
<li><a href="https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types" rel="noreferrer">Service types in Kubernetes</a></li>
<li><a href="https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/" rel="noreferrer">Kubernetes port-forwarding</a></li>
<li><a href="https://learn.microsoft.com/en-us/virtualization/hyper-v-on-windows/about/" rel="noreferrer">Hyper-V</a></li>
<li><a href="https://docs.docker.com/desktop/windows/#switch-between-windows-and-linux-containers" rel="noreferrer">Docker Desktop for Windows users</a></li>
</ul>
