Kubernetes
NodePort
Custom Port
Container Orchestration
Cloud Networking

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

  1. Prerequisites: Ensure that you have enough permissions to define services and modify configurations within your Kubernetes cluster.
  2. 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-range flag 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.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design