Junior
What is NodePort and what functionality does it provide?
sobes.tech AI
Answer from AI
NodePort is a type of Service in Kubernetes that opens a port on each node of the cluster. Traffic sent to this port on any node is forwarded to the Pod(s) associated with this Service.
Functionality of NodePort:
- Behavior change: Provides external access to the service. Traffic from outside the cluster can reach the service through any node IP and the specified NodePort.
- Ports:
- NodePort: Default range 30000-32767. Can be changed with cluster settings.
- TargetPort: The port on the Pod to which traffic is forwarded.
- Port: The port of the Service inside the cluster.
| Port | Description | Usage |
|---|---|---|
| NodePort | Port open on each node | External access to the service |
| TargetPort | Port in the Pod | Forwarding traffic from Service to Pod |
| Port | Service port inside the cluster | Internal interaction between services |
- Availability: The service is accessible via
NodeIP:NodePortaddress from any node in the cluster. - Restrictions:
- Requires opening NodePort on each node.
- Not suitable for production environment due to fixed port range and the need to know the node IP.
- More difficult to scale compared to LoadBalancer or Ingress.
Example YAML for NodePort Service:
apiVersion: v1
kind: Service
metadata:
name: my-nodeport-service
spec:
type: NodePort
selector:
app: my-app # Selector for choosing Pods
ports:
- protocol: TCP
port: 80 # Service port
targetPort: 8080 # Port in Pod
nodePort: 31000 # Specified NodePort (optional, will be chosen automatically if not)