NodePort
- Docker & Kubernetes course
- Intermediate
- Lesson 39/82
- 4 min
Learn the NodePort service type.
NodePort
NodePort exposes the service on a fixed port (default range 30000–32767) on every node's IP. It's built on top of ClusterIP; traffic goes from the node port to the service, and from there to the pods:
apiVersion: v1
kind: Service
metadata:
name: web
spec:
type: NodePort
selector:
app: web
ports:
- port: 80
targetPort: 8080
nodePort: 30080
In Minikube, minikube service web --url gives the address. In production, NodePort is usually used not directly but behind a load balancer.
Summary of the port fields
A NodePort service has three ports and they're often confused:
external client ──► node_ip:nodePort (30080)
└─► service ClusterIP:port (80)
└─► pod_ip:targetPort (8080)If nodePort isn't written, one is picked at random from the range; it's often left to Kubernetes to avoid conflicts.
Practice A NodePort service
Write a Service YAML: name web, type: NodePort; selector app: web; port port: 80, targetPort: 8080, nodePort: 30080.
Starter code
# buraya yazSample solution
apiVersion: v1
kind: Service
metadata:
name: web
spec:
type: NodePort
selector:
app: web
ports:
- port: 80
targetPort: 8080
nodePort: 30080Expected output
service/web created NAME TYPE CLUSTER-IP PORT(S) AGE web NodePort 10.96.45.12 80:30080/TCP 1s
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 How is a service with nodePort: 30080 reached from outside the cluster?
- Only with the pod IP
- With the ClusterIP
- It can't be
- With any node's IP and port 30080 Correct answer
Explanation: Even if the pod isn't on that node, kube-proxy routes the traffic.
2 Make the service type NodePort.
spec:
type: ____
spec:
type: NodePort
Explanation: Type names are case-sensitive.
3 Match the ports.
| Term | Meaning |
|---|---|
nodePort |
External port on the node |
port |
Service port |
targetPort |
Container port |
30000–32767 |
Default NodePort range |