How to expose a Kubernetes service on a specific Nodeport?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
A Kubernetes NodePort service exposes an application on the IP address of every node using a fixed port. If you need a specific port instead of one assigned automatically, set spec.ports[].nodePort explicitly in the Service manifest.
Define the Service as NodePort
A NodePort service needs three important pieces:
- '
type: NodePort' - a service port exposed inside Kubernetes
- a chosen
nodePortvalue inside the cluster's allowed range
Apply it with kubectl apply -f service.yaml, then verify it:
Traffic sent to node-ip:30080 is forwarded to the matching pods on container port 8080.
Know the Port Mapping
The three ports in the manifest have different jobs:
- '
targetPortis the port your container actually listens on' - '
portis the port exposed inside the cluster for clients using the Service name' - '
nodePortis the port opened on every node for external access'
Confusing these values is a common source of broken connectivity. If the pod listens on 8080, then setting only port: 80 does not magically change the container. targetPort must still match the application.
The Port Must Be Allowed by the Cluster
By default, Kubernetes reserves a range such as 30000 through 32767 for NodePort values. The API server can be configured differently, but most clusters keep the default range.
If you request a port outside the allowed range, the Service creation fails. If you request a port already in use by another NodePort service, the API server also rejects it.
That means the safest workflow is:
- choose a port in the cluster's NodePort range
- make sure no existing service already uses it
- apply the manifest and verify with
kubectl get svc -A
End-to-End Example
Assume you already have a deployment running an HTTP app on 8080.
If the container really serves traffic on 8080, the earlier Service manifest exposes it externally on 30080. You can then test with:
If the request fails, inspect the pod, service, and endpoints rather than assuming the NodePort itself is wrong.
When NodePort Is the Right Choice
NodePort is most useful in simple environments:
- local clusters such as Minikube or Kind
- lab environments
- bare-metal setups with an external reverse proxy
- debugging situations where you want a predictable node-level port
For production internet traffic, LoadBalancer or Ingress is usually a better abstraction. Those options handle routing and edge concerns more cleanly than exposing a raw port on every node.
Firewall and Network Rules Still Matter
Kubernetes opening the service does not guarantee the port is reachable from your laptop. Node firewalls, cloud security groups, on-prem network ACLs, or corporate VPN rules can still block the traffic.
In cloud environments, confirm both levels:
- Kubernetes has created the
NodePortservice - the infrastructure firewall allows inbound traffic to that node port
If one is missing, the application remains inaccessible.
Common Pitfalls
The most common mistake is setting nodePort correctly but leaving targetPort wrong. The Service exists, but traffic goes nowhere useful.
Another mistake is assuming NodePort exposes the application on only one node. It opens the chosen port on every node in the cluster.
Teams also forget infrastructure firewalls. A correct manifest does not override cloud security groups or host firewalls.
Finally, avoid choosing NodePort for internet-facing production traffic by default. It works, but it is usually not the cleanest or safest exposure model.
Summary
- Set
type: NodePortand specifyspec.ports[].nodePortto choose the exact port. - Keep
port,targetPort, andnodePortconceptually separate. - Use a value inside the cluster's allowed NodePort range.
- Verify both Kubernetes objects and external firewall rules.
- Prefer
LoadBalancerorIngressfor most production-facing workloads.
Related reading
- How to expose Kubernetes DNS externally
- How to expose kubernetes service on prem using 443/80
- How to expose multiple kubernetes services trough single azure load balancer?
- How to expose multiple port using a load balancer services in Kubernetes
- How to extract custom header value?
- How to find an available port?
- How to extract the helm values.yaml of my existing helm deployment Name prime-gitlab
- How to find out the ip address of the nodePort

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.