davidbaguetta
davidbaguetta

Reputation: 532

Cannot access NodePort service outside Kubernetes cluster

I am on Windows and used Docker Desktop to deploy a local Kubernetes cluster using WSL 2. I tried to deploy a pod and expose it through a NodePort service so I could access it outside the cluster, but it is not working.

Here are the commands to reproduce the scenario:

kubectl create deployment echoserver --image=k8s.gcr.io/echoserver:1.4
kubectl expose deployment echoserver --type=NodePort --port=8080

Trying to open NODE_IP:EXPOSED_PORT in the browser or running the netcat command nc NODE_IP EXPOSED_PORT and trying to send a message (from either WSL or Windows) does not work.

Opening the URL on the browser should be met with this page. However, you will get a generic error response saying the browser couldn't reach the URL.

Sending a message with the netcat command should be met with a 400 Bad Request response, as it will not be a properly formatted HTTP request. However, you will not get any response at all or the TCP connection may not even be made in the 1st place.

Trying to communicate with the service and/or the pod from inside the cluster, for example, through another pod, works perfectly. Using the command kubectl port-forward deployment/echoserver 2311:8080 to port forward the deployment locally and then either accessing localhost:2311 in the browser or through netcat also work perfectly (in both WSL and Windows).

Upvotes: 2

Views: 5028

Answers (1)

kool
kool

Reputation: 3613

If you want to access it not using localhost you should use your <windows_hosts's_IP:NodePort>.

So having your deployment and service deployed:

$kubectl get svc,deploy
NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
service/echoserver   NodePort    10.105.169.2   <none>        8080:31570/TCP   4m12s
service/kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP          5m3s

NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/echoserver   1/1     1            1           4m19s

You can either access it by using localhost:31570 or <windows_hosts's_IP:NodePort>.

In my case 192.168.0.29 is my Windows host's IP:

curl.exe 192.168.0.29:31570
CLIENT VALUES:
client_address=192.168.65.3
command=GET
real path=/
query=nil
request_version=1.1
request_uri=http://192.168.0.29:8080/

Upvotes: 3

Related Questions