NodePort & LoadBalancer

NodePort and LoadBalancer Services expose your app to traffic from outside the cluster.

ClusterIP keeps traffic inside. To let the outside world in, use one of these two types.

NodePort

A NodePort opens the same high-numbered port (30000–32767 by default) on every node. Traffic to nodeIP:nodePort is forwarded to the Service. Simple, but you must know node IPs and the port is ugly.

LoadBalancer

A LoadBalancer asks your cloud provider (AWS, GCP, Azure) to provision a real external load balancer with a public IP that fronts the Service. This is the standard way to expose a service in the cloud.

TypeReachable fromBest for
ClusterIPinside clusterinternal services
NodePortnode IP + portdev/testing
LoadBalancerpublic IPcloud production

Example

Example · yaml
apiVersion: v1
kind: Service
metadata:
  name: web-lb
spec:
  type: LoadBalancer
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 80
# On a cloud provider this gets a public EXTERNAL-IP:
# kubectl get service web-lb

When to use it

  • A developer uses NodePort during local testing to access an application running in a bare-metal cluster through the VM's IP and a static port.
  • A cloud-hosted app uses LoadBalancer type to provision a cloud load balancer that forwards internet traffic to backend pods automatically.
  • An on-premise team uses MetalLB with LoadBalancer Services to get a cloud-like external IP without a cloud provider.

More examples

NodePort Service

Exposes the service on port 30080 of every cluster node, so the app is reachable at <NodeIP>:30080 from outside.

Example · yaml
apiVersion: v1
kind: Service
metadata:
  name: web-nodeport
spec:
  type: NodePort
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 8080
      nodePort: 30080

LoadBalancer Service on cloud

Requests a cloud-provisioned load balancer; the provider assigns an external IP that routes to pod ports automatically.

Example · yaml
apiVersion: v1
kind: Service
metadata:
  name: web-lb
spec:
  type: LoadBalancer
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 8080

Get external IP after creation

Watches the Service until the cloud provider assigns the external IP, then tests connectivity directly to that IP.

Example · bash
kubectl get service web-lb -w
# NAME     TYPE           CLUSTER-IP     EXTERNAL-IP      PORT(S)
# web-lb   LoadBalancer   10.100.20.5    <pending>        80:32456/TCP
# web-lb   LoadBalancer   10.100.20.5    34.120.55.12     80:32456/TCP

curl http://34.120.55.12/

Discussion

  • Be the first to comment on this lesson.