Kubernetes NodePort Custom Port
Codemia Knowledge Hub ·
Introduction
In Kubernetes, one way to expose services externally is through the NodePort
service type. This mechanism allows users to access services from outside the Kubernetes cluster by opening a specific port on each node. By default, Kubernetes assigns a range of ports (usually between 30000 and 32767) for these NodePort
allocations. However, there are cases where a custom port is needed, whether for compliance, legacy system requirements, or simply for consistency. This article explores how to configure and utilize Kubernetes NodePort
with a custom port.
Understanding NodePort
The NodePort
service type is one of several ways Kubernetes facilitates external exposure of services:
- ClusterIP: The default type, only accessible within the cluster.
- NodePort: Exposes the service on each node's IP at a static port.
- LoadBalancer: Provides a load-balanced IP, often cloud provider-specific.
- ExternalName: Maps a service to an external DNS name.
A NodePort
service maps an externally accessible port on all nodes to a corresponding port on the service. The NodePort then forwards traffic to the respective ClusterIP
service, which handles routing within the cluster.
Configuring a Custom NodePort
Step-by-Step Configuration
- Prerequisites: Ensure that you have enough permissions to define services and modify configurations within your Kubernetes cluster.
- Custom Port Range: Ensure that your kube-apiserver is configured to allow custom ports outside the default range. This involves setting the
--service-node-port-rangeflag on your API server. For example, to include port 8080, start your API server with:- port: 80
- Port Management: Carefully manage which ports are exposed. Opening ports that aren't tightly controlled can pose security risks.
- Ingress and Firewalls: When dealing with NodePorts, ensure that firewall rules and ingress controllers are configured to allow traffic from external networks.
- Logging and Monitoring: Set up logging to monitor access to the NodePort service, which can facilitate early detection of unauthorized access attempts.
- Simple to Configure: Using a NodePort service to expose an application requires minimal initial setup and configuration.
- Direct Access: Allows direct access to services through the external IPs of the nodes without additional resources.
- Custom Port Flexibility: Facilitates working with legacy systems or compliance regulations requiring specific port ranges.
- Limited Port Range: By default, NodePorts are limited to the specified range unless changed, potentially causing collisions in large deployments.
- Non-Ideal for Production: NodePorts can expose your entire node to traffic and don't come with advanced load-balancing features like those offered by LoadBalancer services.
- Scalability Issues: Managing and scaling services with NodePorts can become labor-intensive as the number of services increases.
Related reading
- Kubernetes nodes behind NAT service exposure
- kubernetes on AWS get region name in pod
- Kubernetes on docker creates containers with empty serviceaccount and no tokens leading to container crash and restarts
- Kubernetes on Mesos
- Kubernetes persistent volume overriding existing data in the pod/container
- Kubernetes pod distribution amongst nodes
- Kubernetes pod cannot connect to external Database
- Kubernetes pods can't ping each other using ClusterIP
System design interview problems on this topic
- Design a Container Orchestration System: solution and walkthrough
- Design a Code Deployment System: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a GPU Cluster Scheduler for ML Training: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.