Skip to main content
Welcome to this tutorial on Kubernetes Services. In this guide, we’ll focus on the NodePort type, which enables external traffic to reach Pods through a port on each Node. Kubernetes Services provide stable network endpoints for Pods, enabling reliable communication both within the cluster and from outside clients.

Why Use Kubernetes Services?

Kubernetes Services decouple the front-end, back-end, and data-layer Pods, offering:
  • Stable endpoints: Consistent IPs or DNS names for Pods that may be recreated.
  • Load balancing: Distributes traffic evenly across multiple Pods.
  • Discoverability: Native service discovery within the cluster network.
Applications typically consist of:
  • Front-end Pods serving user interfaces
  • Back-end Pods processing business logic
  • Pods connecting to external data sources
With Services, these components can communicate without hardcoding Pod IPs.

External Access Use Case

By default, Pod IPs (e.g., 10.244.0.2) are only reachable inside the cluster network. To access a web server Pod from your laptop (192.168.1.10) without SSH’ing into the Node (192.168.1.2), you need a NodePort Service which maps a port on the Node to the Pod’s port.
NodePort ranges from 30000 to 32767 by default. You can customize this in the API server flags.
The image illustrates a Kubernetes NodePort service setup, showing a service routing traffic from port 30008 to a pod at IP 10.244.0.2 on port 80.

NodePort Service Ports Explained

A NodePort Service uses three port definitions:
  • targetPort: Port on the Pod (e.g., 80)
  • port: Virtual Service port inside the cluster (e.g., 80)
  • nodePort: Port on each Node, accessible externally (e.g., 30008)
Traffic to <NodeIP>:<nodePort> → Service → port → Pod at targetPort.

Defining a NodePort Service

  1. Create a Pod with labels:
  1. Define the NodePort Service, matching the Pod labels:
  1. Deploy and Verify:
Expected output:
Access the application:
Exposing high ports on Nodes can pose security risks. Ensure proper firewall rules and network policies are in place.

Scaling with Multiple Pods and Nodes

In production, you’ll run multiple Pod replicas for high availability. A NodePort Service automatically load-balances incoming traffic across all Pods that match its selector, even when spread across multiple Nodes.
The image illustrates a Kubernetes NodePort service setup, showing how a user connects to a service that routes traffic to different pods across multiple nodes.
Simply scale your Deployment or Pod replicas:
Now, requests to any Node at NodeIP:30008 are distributed across all 3 Pods.

Summary

  • NodePort Services expose Pod ports on each Node for external access.
  • Key fields: type: NodePort, port, targetPort, and nodePort.
  • Match Services to Pods via label selector.
  • Kubernetes handles load balancing across Pods and Nodes automatically.
Next, explore the demo to see NodePort Services in action!

Watch Video