I think the question is more subtle and @rob_kodekloud answers sum it all up but allow me to further clarify.
When you deploy a pod like nginx the listening port for the service is defined in the configuration file within the pod, in this case it’s TCP 80. The imperative run command allows to define a --port but that in itself doesn’t change anything within the pod, it’s only a preparation to bind a service to it.
In fact --port exposes the service only if expose is set to true
--port=-1: The port that this container exposes. If --expose is true, this is also the port used by the service that is created.
If you run:
kubectl run nginx --image=nginx --port=8081 --expose=true
You’ll get:
service/nginx created
pod/nginx create
If you run:
kubectl run nginx --image=nginx --port=8081
You’ll only get:
pod/nginx create
As the default behaviour is not to expose the pod.
So specifying the --port implies to also create a separate service afterwards like:
kubectl expose pod nginx --port=8081 --target-port=8081
On a setup like this:
Pods
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
curler 1/1 Running 0 6h38m 10.1.1.71 docker-desktop <none> <none>
nginx 1/1 Running 0 2m54s 10.1.1.74 docker-desktop <none> <none>
Services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d5h <none>
nginx ClusterIP 10.100.82.148 <none> 8081/TCP 27s run=nginx
You can curl/wget from the curler to the nginx pod directly via the 10.1.1.74 only on port 80 (unless you perform the change Rob suggests). At this level the service definition is irrelevant and so is the --port from the the imperative command.
On the same node a pod can communicate with another pod without using services (unless network policies or other iptables tricks are done)
When using the service instead (either the one created with the expose command or with expose=true in the imperative run) you’ll use the cluster IP and then the port becomes relevant. Behind the scenes I suppose iptables creates some NAT rules for it.
I hope this clarifies as Kubernetes networking isn’t trivial at all and does my head in all the time.