Sub topic 4
Matching port, targetPort, and the container port
This chapter assumes you can read Deployment and Service manifests and run kubectl get, describe, and exec. It also assumes you know what a TCP port is.
Three numbers, three jobs
A Service has no process of its own. It forwards traffic, and three different port values decide where that traffic goes.
| Field | Where it lives | What it means |
|---|---|---|
| `port` | Service | The port clients connect to on the Service IP. |
| `targetPort` | Service | The port on the backend Pod that receives the forwarded traffic. |
| `containerPort` | Pod spec | Documentation of the port the container listens on. |
The important part is that containerPort is informational. Kubernetes does not use it to open or close anything. The port that matters is the one your process actually listens on. If targetPort does not match that port, connections fail.
A working pair
Here is the Deployment and Service from this tutorial's running example.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27
ports:
- containerPort: 80
readinessProbe:
httpGet:
path: /
port: 80
---
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- port: 8080
targetPort: 80Clients call web:8080. The Service forwards each connection to port 80 on a ready Pod, where nginx is listening.
How a mismatch looks
If you change targetPort to 8081, the Service still has endpoints, because the selector and readiness are unaffected. The Pods stay ready. Requests to web:8080 then fail with "connection refused" or a timeout, because nothing listens on 8081.
This is why a port problem is easy to misread. The Service exists, DNS resolves, and endpoints are listed, but traffic still fails. Check ports after you have confirmed that endpoints exist.
Checking each number
- Read the Service ports and the endpoint ports.
- Read what the container declares.
- Confirm what the process listens on, from inside the Pod.
kubectl get service web -o jsonpath='{.spec.ports}'
kubectl get endpointslices -l kubernetes.io/service-name=web -o jsonpath='{.items[*].ports}'
kubectl get pods -l app=web -o jsonpath='{.items[0].spec.containers[0].ports}'
kubectl exec deploy/web -- cat /proc/net/tcpThe EndpointSlice ports show the targetPort that the Service resolved. The last command reads the kernel's TCP table. Listening sockets have state 0A, and the local port is in hex after the colon. Port 80 appears as 0050. Many images lack ss or netstat, so this works when those tools are missing.
You can also test the Pod directly, bypassing the Service. Take a Pod IP from kubectl get pods -o wide and request it from a temporary Pod on the port you expect. If the direct request works and the Service request fails, the Service's targetPort is the likely cause.
Named ports
targetPort can be a name instead of a number. The name must match a name in the container's ports list.
ports:
- name: http
containerPort: 80ports:
- port: 8080
targetPort: httpA typo in the name leaves the Service with no usable port for those Pods. Named ports let different Pods behind one Service listen on different numbers.
Exercise
A Service has port: 80 and targetPort: 8080. The Pods are ready and listed as endpoints, but requests are refused. The container image runs a server on port 3000. What is wrong, and which value do you change?
Show answer
targetPort points at 8080, but the process listens on 3000. Change targetPort to 3000. The Service port can stay at 80, because clients use that value, and the containerPort is only documentation.