What is NodePort?
NodePort extends the ClusterIP service by exposing the service on each Node's IP at a static port. This allows external access to your service using any node's IP address and the assigned NodePort.
NodePort Range: 30000-32767
Ports are either statically defined or dynamically assigned from this range
External Access
Accessible from outside the cluster using any node's IP address
Extends ClusterIP
Includes all ClusterIP functionality plus external NodePort exposure
Multiple Access Points
Service can be accessed via any cluster node's IP address
Important: Nodes must have public IP addresses for external access. NodePort is ideal for development, testing, and when you don't have cloud provider load balancers available.
kubectl Commands for NodePort
Expose a Pod
--target-port=8080 --type=NodePort
Create a NodePort service to expose a pod
Expose a Deployment
--port=80 --target-port=8080 \
--type=NodePort --name=frontend
Create a NodePort service to expose a deployment
Apply from YAML
Deploy the service from configuration file
List Services
kubectl get svc -o wide
Get the services list with basic or extended info
Service Details
Describe the service configuration and endpoints
Delete Service
kubectl delete svc [serviceName]
Delete the service using YAML file or service name
NodePort Configuration
Service Definition
apiVersion: v1
kind: Service
metadata:
name: svc-example
spec:
type: NodePort
selector:
app: nginx
env: prod
ports:
- nodePort: 32410
protocol: TCP
port: 80
targetPort: 80
Port Configuration
- nodePort: External port (30000-32767)
- port: Internal service port
- targetPort: Pod container port
- protocol: TCP or UDP
Port Types Explained
NodePort
The port exposed on each node's IP address. Accessible externally from anywhere.
# Range: 30000-32767
Access: http://node-ip:32410
Service Port
The port that the service exposes internally within the cluster.
Internal access: http://service-name:80
Target Port
The port on the pod containers where the application is running.
Application listens on this port
Advanced Configuration
apiVersion: v1
kind: Service
metadata:
name: web-service
namespace: production
labels:
app: web-app
tier: frontend
spec:
type: NodePort
selector:
app: web-app
version: "2.1"
ports:
- name: http
nodePort: 31000
protocol: TCP
port: 80
targetPort: 8080
- name: https
nodePort: 31001
protocol: TCP
port: 443
targetPort: 8443
externalTrafficPolicy: Local
sessionAffinity: ClientIP
External Traffic Policy
"Local" preserves client IP but may cause uneven traffic distribution.
Multiple Ports
NodePort services can expose multiple ports for different protocols.
NodePort Architecture
Access Patterns
http://192.168.1.10:32410
Access via Node 1
http://192.168.1.11:32410
Access via Node 2
http://192.168.1.12:32410
Access via Node 3
When to Use NodePort
- Development & Testing: Quick external access during development
- On-premise Clusters: When cloud load balancers aren't available
- Static IP Requirements: When you need predictable external IPs
- Bare Metal: Kubernetes clusters running on bare metal
- Prototyping: Quick prototypes and proof of concepts
Considerations
- Port range limitation (30000-32767)
- Manual load balancing across nodes
- Security exposure on high-numbered ports
- Node IP changes may require DNS updates
- Not ideal for production web services
Best Practices for NodePort
Security & Access
- Use firewall rules to restrict access to NodePort range
- Consider using Ingress controllers for production web traffic
- Use LoadBalancer service type in cloud environments
- Implement Network Policies to restrict pod communication
- Monitor NodePort services for unauthorized access attempts
Configuration & Management
- Use static NodePort assignments for predictable ports
- Document which NodePorts are assigned to which services
- Use external DNS to map domains to node IPs
- Consider externalTrafficPolicy: Local for client IP preservation
- Plan for port conflicts in multi-team environments
Example: Complete Development Setup
# Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
spec:
replicas: 3
selector:
matchLabels:
app: web-app
template:
metadata:
labels:
app: web-app
spec:
containers:
- name: web
image: nginx:latest
ports:
- containerPort: 80
---
# NodePort Service
apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
type: NodePort
selector:
app: web-app
ports:
- nodePort: 31000
port: 80
targetPort: 80
Access the application at: http://any-node-ip:31000