Sub topic 4
Ports, targetPort and named ports
This chapter assumes you can apply YAML with kubectl and know what a Pod, a Deployment and a label selector are. It also assumes you know what a TCP port is. A Service has several port fields, and mixing them up is a common cause of "connection refused" errors.
The three port fields
A Service port entry maps a port on the Service to a port on the Pods. Three fields matter:
| Field | Where it applies | Meaning |
|---|---|---|
| `port` | The Service | The port clients connect to on the Service's virtual IP and DNS name. |
| `targetPort` | The Pod | The port on the selected Pods that receives the traffic. It can be a number or a container port name. |
| `nodePort` | Every node | Only for NodePort and LoadBalancer Services. It is covered in a later chapter. |
If you omit targetPort, Kubernetes uses the value of port. That default works only when the container listens on the same number as the Service.
A numeric mapping
Suppose a container listens on 8080 and you want clients to use port 80. The Service would look like this:
apiVersion: v1
kind: Service
metadata:
name: api
spec:
selector:
app: api
ports:
- port: 80
targetPort: 8080A client in the cluster connects to api:80. Kubernetes forwards the connection to port 8080 on one of the ready Pods.
Named ports
A container can give a port a name, and a Service can refer to that name. The Deployment from this tutorial declares the name http like this:
ports:
- name: http
containerPort: 80The Service then uses it:
ports:
- name: http
port: 80
targetPort: httpThe name is resolved separately for each Pod. If some Pods later move their server to port 8080 and keep the name http, the Service still reaches them with no edit. This helps during a rollout where old and new versions use different numbers.
The same name is also useful in a readiness probe, as in port: http. The probe and the Service then follow the container's declared port.
Multiple ports
When a Service exposes more than one port, every entry must have a name. Names must be lowercase alphanumeric or -, and they must be unique within the Service.
apiVersion: v1
kind: Service
metadata:
name: app
spec:
selector:
app: app
ports:
- name: http
port: 80
targetPort: http
- name: metrics
port: 9090
targetPort: metricsYou can also set protocol for each entry. The default is TCP. UDP and SCTP are also supported.
How the mapping flows
Common errors
- Wrong targetPort. The Service exists and has endpoints, but connections are refused because nothing listens on that port in the container.
- Name mismatch. If
targetPort: httpnames a port that no container declares, the Pod gets no usable endpoint for that port. Check the spelling against the Pod spec. - Confusing
containerPortwith the listener.containerPortis mostly documentation. The application must actually listen on that port, on an address other than only127.0.0.1.
Exercise
A container declares containerPort: 3000 with the name web. You want clients to reach it at port 443 on the Service. Which port and targetPort values do you write if you use the name?
Show answer
Set port: 443 and targetPort: web. Kubernetes resolves web to 3000 in each selected Pod.