Everytime I try to access a NodePort on my machine, it says "Error Connection Refused." I don’t understand since the examples I read online imply that I can run Docker Desktop on my laptop, connect to the cluster, and access services via their nodeport.
My machine:
- Windows 10
- Docker Desktop (tested additionally with
k3s
andminikube
with similar results) - Kubernetes 1.19+
Kubernetes Configuration:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: ngnix-service
spec:
selector:
app: nginx
type: NodePort
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30007
Output and cURL test:
PS C:UsersMEnginx> kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 169m
ngnix-service NodePort 10.108.214.243 <none> 80:30007/TCP 7m19s
PS C:UsersMEnginx> curl.exe http://localhost:30007
curl: (7) Failed to connect to localhost port 30007: Connection refused
I’ve also tried with the node ip:
PS C:UsersMEnginx> kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
docker-desktop Ready master 6d v1.19.7 192.168.65.4 <none> Docker Desktop 5.10.25-linuxkit docker://20.10.5
PS C:UsersMEnginx> curl.exe http://192.168.65.4:30007
curl: (7) Failed to connect to 192.168.65.4 port 30007: Timed out
I get the same response when trying to access a NodePort from my browser (Chrome). ERR_CONNECTION_REFUSED
Is there something I’m missing? Why are all NodePorts inaccessible?
2
Answers
Kubernetes run locally, still runs on its internal network.
Here you use an IP address that is internal Kubernetes network. You must expose your Kubernetes service so that it gets an cluster-external address.
See this part:
You typically expose the service outside the cluster with a Service of
type: Loadbalancer
or use an Ingress-gateway.See this answer on how you can change your Service from
type:NodePort
totype: LoadBalancer
to expose it to your localhost.The easiest way to access your service is to use
kubectl port-forward
, e.g.Then you can access it on
localhost:8080
.See Use Port Forwarding to Access Applications in a Cluster
Not sure if this is helpful for anyone. I was facing similar issues where NodePort was properly created with entrypoint pointing to the correct containerPort and IP. Yet, I wasn’t able to curl the NodePort. After a day of searching, here is what unblocked me. And thought I would share to save people the pain of searching.
After running (for mac)
For windows, the folder is C:/ProgramData/DockerDesktop/pki
Then restart docker desktop
Source (https://github.com/docker/for-mac/issues/3594#issuecomment-621487150)