Docker Community Forums

Share and learn in the Docker community.

Kubernetes - Why am I able to access my service through 'port' and not 'nodePort'?

I have a simple flask process running on port 8080 in my container according my docker file

FROM python:3.7
RUN mkdir /app
WORKDIR /app
ADD . /app/
RUN pip install flask
RUN pip install gunicorn
CMD ["gunicorn", "-w", "1", "-b", ":8080", "-t", "360", "wsgi:app"]

and I created a deployment for the kubernetes cluster running on docker like this

apiVersion: v1
kind: Service
metadata:
  name: hello-python-service
spec:
  selector:
    app: hello-python
  ports:
  - protocol: "TCP"
    port: 6000
    targetPort: 8080
    nodePort: 30001
  type: LoadBalancer

---


apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-python 
spec:
  replicas: 2
  selector:
    matchLabels:
      app: hello-python
  template:
    metadata:
      labels:
        app: hello-python
    spec:
      containers:
      - name: hello-python
        image: hello-python:latest
        imagePullPolicy: Never
        ports:
        - containerPort: 8080

When I apply this deployment to kubernetes, which port am I supposed to use to access the service? When I tried localhost:6000, I was able to access the service. But that’s not what I expected because I was watching this video where a similar hello world service is deployed to kubernetes and the presenter access the service from a browser on port 30001

According to what I’ve read ( and also from that same video https://youtu.be/1xo-0gCVhTU?t=1607),
port is accessible inside cluster
targetPort is the port traffic for the service gets forwarded to inside the pod
nodePort is the port accessible outside the cluster

I guess I wasn’t expecting to access the service on port 6000 because port says its inside the cluster but am I outside the cluster? or is the cluster my my entire workstation?

when I run

kubectl get service --all-namespaces

I get this

NAMESPACE default
NAME hello-python-service
TYPE LoadBalancer
CLUSTER-IP 10.102.195.223
EXTERNAL IP localhost
PORT(S) 6000:30001/TCP

I’m not clear on the networking aspect of this in relation to my machine.

nodePort
This setting makes the service visible outside the Kubernetes cluster by the node’s IP address and the port number declared in this property. The service also has to be of type NodePort (if this field isn’t specified, Kubernetes will allocate a node port automatically).

port
Expose the service on the specified port internally within the cluster. That is, the service becomes visible on this port, and will send requests made to this port to the pods selected by the service.

targetPort
This is the port on the pod that the request gets sent to. Your application needs to be listening for network requests on this port for the service to work.

Where your Kubernetes cluster running on?

I’m running the cluster on my local workstation in docker (using docker-desktop context)

I noticed you had:

EXTERNAL IP localhost on your service

what happened is you deployed a service of type LoadBalancer,
But because LoadBalancer is not supported by this simple cluster, it seems it set localhost as external ip

by the way you helped me in my own problem in my post :slight_smile:

When I type

kubectl get nodes -o wide

I get

INTERNAL-IP 192.168.65.3
EXTERNAL-IP <none>
OS-IMAGE Docker-Desktop

EDIT: this was a response to your original question

Yeah sorry about that, I hope my answer helped you.
I think as LoadBalancer is not supported, it does a proxy forward with localhost so we can reach the service.

I tried to reach the node from outside but I can’t.

Ok so service of type LoadBalancer is not supported in docker dektop kubernetes cluster, is that right?

Also what is setting external IP to local host? why is external access through port 6000 and not 30001?

We can see the external endpoint here.

Yes good question normally we should reach for :

http://IP-EXTERNAL-NODE:30001

but this IP is set to None

I think Kubernetes of docker created a node without external IP

Hopefully someone from the team will help us.

You can maybe use minikube and compare the node properties there.

try accessing it via localhost:30001