When working with Kubernetes, you often need to expose an application for testing or debugging without altering external ingress. kubectl offers port-forward, a command that bridges a local port to a port inside the cluster. This is where the concept of an internal port becomes critical.
The internal port is the port number your Kubernetes Service or Pod listens on inside the cluster network. In a deployment spec or a service YAML, this is defined as targetPort for Services and containerPort for containers. External ports, defined as port in a Service, are what other in-cluster resources connect to. When you run:
kubectl port-forward svc/my-service 8080:80
you map your local machine’s port 8080 to the internal port 80 of that Service. Kubernetes routes the request from your terminal into the cluster network, hitting the Pod or Pods behind the Service on their internal port.
For Pods, the mapping is direct:
kubectl port-forward pod/my-pod 9090:9090
This lets you hit localhost:9090 to talk straight to the Pod’s process, bypassing any Service routing. This is essential when testing an application before exposing it more broadly or when confirming that an internal port is open and responding.