Answer a question

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 and minikube 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:\Users\ME\nginx> 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:\Users\ME\nginx> 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:\Users\ME\nginx> 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:\Users\ME\nginx> 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?

Answers

Kubernetes run locally, still runs on its internal network.

curl.exe http://192.168.65.4:30007

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:

EXTERNAL-IP
<none>

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 to type: LoadBalancer to expose it to your localhost.

The easiest way to access your service is to use kubectl port-forward, e.g.

kubectl port-forward ngnix-service 8080:80

Then you can access it on localhost:8080.

See Use Port Forwarding to Access Applications in a Cluster

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐