ClusterIP vs NodePort vs LoadBalancer in Kubernetes: Key Differences
ClusterIP exposes a service only inside the cluster, NodePort exposes it on each node's IP at a static port, and LoadBalancer provisions an external load balancer to expose the service outside the cluster. ClusterIP is for internal communication, NodePort allows external access via node IP and port, and LoadBalancer provides a cloud-managed external IP with load balancing.Quick Comparison
This table summarizes the main differences between ClusterIP, NodePort, and LoadBalancer service types in Kubernetes.
| Feature | ClusterIP | NodePort | LoadBalancer |
|---|---|---|---|
| Access Scope | Internal cluster only | External via node IP and port | External via cloud load balancer |
| Default Port Range | Any port inside cluster | Static port 30000-32767 | Any port, managed by cloud |
| Use Case | Pod-to-pod communication | Simple external access | Production-grade external access |
| Requires Cloud Provider | No | No | Yes |
| Load Balancing | Kubernetes internal | Basic node port forwarding | Cloud provider load balancer |
| IP Type | Cluster IP | Node IP | External IP |
Key Differences
ClusterIP is the default service type in Kubernetes. It creates a virtual IP inside the cluster that other pods use to communicate with the service. This IP is not reachable from outside the cluster, making it ideal for internal-only services.
NodePort opens a specific port on every node in the cluster. This port forwards traffic to the service, allowing external clients to access the service by connecting to any node's IP and that port. It is simple but exposes the service on all nodes and uses a fixed port range.
LoadBalancer integrates with cloud providers to provision an external load balancer. This gives the service a public IP address and distributes traffic across nodes and pods. It is the most robust option for exposing services externally but requires cloud support.
ClusterIP Code Example
This example shows a ClusterIP service exposing a simple app on port 80 inside the cluster.
apiVersion: v1 kind: Service metadata: name: my-clusterip-service spec: type: ClusterIP selector: app: myapp ports: - protocol: TCP port: 80 targetPort: 8080
NodePort Equivalent
This example shows the same service exposed as a NodePort on port 30080, accessible externally via node IPs.
apiVersion: v1 kind: Service metadata: name: my-nodeport-service spec: type: NodePort selector: app: myapp ports: - protocol: TCP port: 80 targetPort: 8080 nodePort: 30080
When to Use Which
Choose ClusterIP when your service only needs to be accessed inside the cluster, such as backend APIs or databases.
Choose NodePort for quick external access without cloud load balancers, useful for development or simple setups.
Choose LoadBalancer in cloud environments when you need a stable external IP with load balancing and production-ready access.