Member-only story
k8s for Beginners — port vs targetPort vs NodePort
When I first started working with Kubernetes, configuring Services was one of the more challenging aspects for me.
I remember feeling overwhelmed by the different port-related terms: port
, targetPort
, and NodePort
.
Each seemed to have its own role and impact, and it took me some time to grasp how they worked together.
To simplify things, I decided to break down these concepts into more manageable pieces and see how they interacted in a typical setup.
In Kubernetes, when you set up a Service to expose your applications, you’ll work with three key types of ports: port, targetPort, and NodePort. Here’s a simple explanation of each:
Port
- Definition: This is the port on which the Service is exposed within the Kubernetes cluster.
- Usage: Other components within the cluster use this port to communicate with the Service. It acts as a stable entry point for Pods.
- Example: If the Service has a port set to 80, it means the Service can be accessed at port 80 within the cluster.
TargetPort
- Definition: This is the port on the Pod where the application is actually listening.
- Usage: The Service forwards traffic to this port on the Pod. It allows the Service to direct traffic to the correct Pod port, even if the port numbers differ.
- Example: If the…