Intermediate LevelDocker & Deployment
What is the difference between ClusterIP, NodePort, and LoadBalancer Kubernetes Services?
❓ Interview Prompt: Explain Kubernetes Service types — ClusterIP (internal), NodePort (node-level), LoadBalancer (cloud LB) — and ExternalName for DNS aliasing.
Kubernetes Services provide stable network endpoints for Pods. The type field determines how the service is exposed:
ClusterIP (Default — Internal Only):
•
Assigns a virtual IP address reachable only from within the cluster.
•
DNS:
service-name.namespace.svc.cluster.local.•
Use case: Service-to-service communication within the cluster (API → Database).
NodePort:
•
Opens a static port (30000–32767) on every node's network interface.
•
Traffic to
<NodeIP>:<NodePort> is forwarded to the Service's ClusterIP.•
Use case: Simple external access without a cloud load balancer (development, bare metal).
•
Problem: Exposes ports on all nodes; users must know a node IP.
LoadBalancer:
•
Provisions a cloud-managed load balancer (AWS ELB, GCP Cloud LB, Azure ALB) in front of the NodePorts.
•
Each LoadBalancer Service gets its own external IP.
•
Use case: Production external traffic. Expensive — one cloud LB per Service.
•
Better alternative: Use Ingress + one LoadBalancer for all services.
ExternalName:
•
DNS alias — resolves the Service name to an external CNAME (
my-db.rds.amazonaws.com).•
No proxying — purely DNS-based.
•
Use case: Gradually migrating external services into the cluster.
Code Implementation Example
yaml
# ClusterIP — internal only (service-to-service)
apiVersion: v1
kind: Service
metadata:
name: postgres-svc
spec:
type: ClusterIP # default
selector:
app: postgres
ports:
- port: 5432
targetPort: 5432
# DNS: postgres-svc.default.svc.cluster.local:5432
---
# NodePort — static port on every node (dev/testing)
apiVersion: v1
kind: Service
metadata:
name: api-nodeport
spec:
type: NodePort
selector:
app: api
ports:
- port: 80
targetPort: 3000
nodePort: 30080 # accessible via <NodeIP>:30080
---
# LoadBalancer — cloud LB with external IP (production)
apiVersion: v1
kind: Service
metadata:
name: api-lb
annotations:
service.beta.kubernetes.io/aws-load-balancer-type: "nlb" # AWS NLB
spec:
type: LoadBalancer
selector:
app: api
ports:
- port: 443
targetPort: 3000💡ClusterIP for internal, NodePort for bare-metal/dev, LoadBalancer for cloud-managed external access.
🎯 Interview Key Takeaway
Use ClusterIP for internal traffic, LoadBalancer for production external traffic (but use Ingress to share one LB across many services), NodePort for development/bare-metal.
#Kubernetes#Services#ClusterIP#NodePort#LoadBalancer#Intermediate
Back to all Docker & Deployment Questions→