Sub topic 11
NodePort: exposing a port on every node
This chapter assumes you know ClusterIP Services, label selectors, and the web Deployment from earlier chapters. You should also be able to find node IP addresses with kubectl.
What NodePort does
A NodePort Service opens the same TCP or UDP port on every node in the cluster. A request to <any-node-IP>:<nodePort> is forwarded to one of the Service's ready Pods. By default the port comes from the range 30000-32767, which the API server's --service-node-port-range flag controls.
NodePort builds on ClusterIP. Kubernetes still allocates a cluster IP, and the node port is an extra entry point to it. Each node's kube-proxy (or equivalent) programs the rules that do the forwarding.
Creating a NodePort Service
This Service selects the same app: web Pods as the earlier examples.
apiVersion: v1
kind: Service
metadata:
name: web-nodeport
spec:
type: NodePort
selector:
app: web
ports:
- name: http
port: 80
targetPort: http
nodePort: 30080Each port entry has three numbers:
portis the port on the Service's cluster IP.targetPortis the container port on the Pod.nodePortis the port opened on every node.
If you omit nodePort, Kubernetes picks a free one from the range. Pinning it is useful for fixed firewall rules. A pinned value must be unique across the cluster and inside the range, or the API server rejects the Service.
Testing it
kubectl apply -f web-nodeport.yaml
kubectl get service web-nodeport
kubectl get nodes -o wide
curl http://<node-ip>:30080Use the INTERNAL-IP or EXTERNAL-IP column from the node list, depending on where you run curl. In local clusters such as minikube or kind, the node IP may not be reachable from your host without extra setup.
Behavior to know
- Any node works as the entry point, even a node that runs no
webPod. Traffic is forwarded across the cluster network to a ready Pod. - By default the forwarding hop replaces the client's source IP with the node's IP (SNAT). Your application sees the node address, not the real client.
- Setting
externalTrafficPolicy: Localsends traffic only to Pods on the node that received it, and this keeps the client IP. A node with no local ready Pod then drops the traffic, so a load balancer must health-check nodes. - Node IPs change when nodes are replaced, so clients should not depend on a single node address.
Common errors
| Symptom | Likely cause |
|---|---|
| API rejects the Service | `nodePort` is outside the range or already used |
| Connection times out from outside | Firewall or cloud security group blocks the port |
| Works on one node, not another | Node-level firewall rules or a failing kube-proxy on that node |
| Connection refused | No ready endpoints; check readiness and the selector |
Exercise
A NodePort Service uses nodePort 30080. A client calls port 30080 on a node that runs no matching Pod. What happens with the default externalTrafficPolicy, and what changes if you set it to Local?
Show answer
With the default (Cluster), the node forwards the request to a ready Pod on another node, and the Pod sees the node's IP as the source. With Local, the node has no local ready Pod, so it drops the request. Local keeps the client source IP but only works on nodes that host a ready Pod.