Hello, Can anyone please help me to understand the following scenario. Lets say . . .

Baz:
Hello, Can anyone please help me to understand the following scenario. Lets say I have a deployment with a container running nginx:stable image. When I was creating this pod I did not specify a Port of the container and if I run kubectl describe pod command I get Port: <none> in the description. As we all know that nginx is a web server and it runs on port 80 I assume kubernetes somehow knows it should run this container on port 80. Right? Then when I run kubectl expose deployment command to create a service and did not specify --target-port but only specify --port by default and for convenience, the targetPort is set to the same value as the port field. Let’s say it would be for both 8080. So my question is how it all works? We have the service exposed on port 8080 then it is linked to the pods through targetPort 8080 and nginx web server running on port 80. Does kubernetes clever enough to link all these different ports together so it works? Thank you

Jesus Arechiga Jimenez:
no, if your nginx is running on port 80 you should specify target port as 80.
Nginx config could be different and expect incoming connections on any other port. Kubernetes has no understanding of the nginx config so you need to manually tight them up

Alistair Mackay:
Containers are automatically listening on any ports given by EXPOSE command in the dockerfile that built the image. You can trust that any official base image from dockerhub will run on the expected ports, e.g. httpd , nginx on 80, MySQL on 3306 etc.

If you run up something inside the container after the container has started (i.e was not in the image and didn’t EXPOSE), then you can add additional ports in the pod manifest. Generally not best practice to do that though.

Baz:
Thank you for the answers