Service Ports and Target Ports
What you'll learn: How Kubernetes Services map external-facing ports to the actual container ports where your applications listen.
The Bridge Between Outside and Inside
When you expose a Service in Kubernetes, you're creating a bridge between two different port numbers: the Service port (what clients connect to) and the target port (where your container is actually listening). Think of it like a hotel switchboard—callers dial the main number (Service port), but the switchboard routes them to specific room extensions (target ports on Pods).
How Port Mapping Works
In a Service definition, you specify:
port: The port the Service itself exposes to the cluster or external clientstargetPort: The port on the Pod where your container application is listening
For example, your web application might listen on container port 8080, but you want clients to access it on the standard HTTP port 80. The Service handles this translation automatically.
apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
selector:
app: web
ports:
- port: 80 # Service port (what clients use)
targetPort: 8080 # Container port (where app listens)
When traffic arrives at port 80 on the Service, Kubernetes forwards it to port 8080 on the backing Pods matched by the selector.
Why This Matters
This separation gives you flexibility: you can standardize external-facing ports while letting containers use any internal port they need. You can even map multiple Service ports to different target ports in a single Service definition.
Key Takeaway: Service ports define what clients connect to, while target ports specify where containers actually listen—Kubernetes handles the translation between them automatically.