Trying to expose an endpoint from a kubernetes pod to the internet/ browser/ API

1/5/2022

What am I trying to do

Trying to expose an endpoint from a kubernetes pod to the internet/ browser/ API on a Windows 11 platform with WSL 2 enabled and using Powershell, Docker on Windows, kubectl and minikube. This is essential for resolving my dev environment.

What happens

Based on whatever I could find in the docs and online, I saw Loadbalancer as the option used for <>. The tunneling never seemed to happen. I tested using the browser and using curl.

Environment Information

  • Windows: Windows 11 Pro
  • Docker on Windows: Docker Desktop 4.3.2 (72729)
  • Kubernetes: v1.22.3
  • Minikube: minikube version: v1.24.0

Commands - executed

Here are the commands that I executed to create the service.

1. Create the deployment

~ kubectl create deployment hello-world3 --image=nginx:mainline-alpine
deployment.apps/hello-world3 created

~ kubectl get deployment
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
hello-world3   1/1     1            1           19s

2. Expose outbound

~ kubectl expose deployment hello-world3 --type=LoadBalancer --port=8080
service/hello-world3 exposed

~ kubectl get svc
NAME           TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
hello-world3   LoadBalancer   10.103.203.156   127.0.0.1     8080:30300/TCP   14s
kubernetes     ClusterIP      10.96.0.1        <none>        443/TCP          6d8h

3. Create tunnel service

~ minikube service hello-world3
|-----------|--------------|-------------|---------------------------|
| NAMESPACE |     NAME     | TARGET PORT |            URL            |
|-----------|--------------|-------------|---------------------------|
| default   | hello-world3 |        8080 | http://192.168.49.2:30300 |
|-----------|--------------|-------------|---------------------------|
* Starting tunnel for service hello-world3.
|-----------|--------------|-------------|------------------------|
| NAMESPACE |     NAME     | TARGET PORT |          URL           |
|-----------|--------------|-------------|------------------------|
| default   | hello-world3 |             | http://127.0.0.1:62864 |
|-----------|--------------|-------------|------------------------|
* Opening service default/hello-world3 in default browser...
! Because you are using a Docker driver on windows, the terminal needs to be open to run it.

I expected to get the “Nginx welcome” page when I connect to http://127.0.0.1:8080

But it was

This site can’t be reached. The connection was reset.
Try:
Checking the connection
Checking the proxy and the firewall
Running Windows Network Diagnostics
ERR_CONNECTION_RESET

Same occurs with: http://127.0.0.1:62864/

Output when I use curl

~ curl http://127.0.0.1:8080/* -v
VERBOSE: GET with 0-byte payload
curl : The underlying connection was closed: An unexpected error occurred on a receive.
At line:1 char:1
+ curl http://127.0.0.1:8080/ -v
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
~ curl http://127.0.0.1:62864/ -v
VERBOSE: GET with 0-byte payload
curl : The underlying connection was closed: An unexpected error occurred on a receive.
At line:1 char:1
+ curl http://127.0.0.1:62864/ -v
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
-- dsosale
docker
kubernetes
minikube

1 Answer

1/17/2022
kubectl expose deployment hello-world3 --type=LoadBalancer --port=8080

You can check what happened when you used command above using kubectl get svc hello-world3 -o yaml and look on ports section:

ports:
- nodePort: 30514
  port:8080
  protocol: TCP
  targetPort:8080

As you can see targetPort has been set to the same port as port. You can read more here

Note: A Service can map any incoming port to a targetPort. By default and for convenience, the targetPort is set to the same value as the port field.

You couldn't see nginx page because targetPort should be set to 80 which is listening by pods by default instead of 8080.

To solve your issue you can set a targetPort to port 80 , by adding --target-port=80 to your command as below:

kubectl expose deployment hello-world3 --type=LoadBalancer --port=8080 --target-port=80

More convenient option using Kubernetes on Windows machine is set up Enable Kubernetes option in Docker Desktop in Settings>Kubernetes. Cluster will be created automatically and you will be able use kubectl commands in few minutes in terminal or powershell. If something goes wrong, you will be able easily reset your cluster by clicking Reset Kubernetes Cluster button which is in the same place when you enabled Kubernetes in Docker Desktop.

-- RadekW
Source: StackOverflow