Chapter 4.2 - Service Types
Learning Objectives
By the end of this chapter, you will be able to:
- Understand the 4 types of Services
- Choose the right type based on the use case
- Configure each Service type
- Understand the differences
Service Types
Kubernetes offers 4 types of Services:
1. ClusterIP (Default)
Internal cluster access only.
apiVersion: v1
kind: Service
metadata:
name: clusterip-service
spec:
type: ClusterIP # Default
selector:
app: web
ports:
- port: 80
Use case: Inter-service communication within the cluster.
2. NodePort
Exposes the Service on a static port on each node (30000-32767).
apiVersion: v1
kind: Service
metadata:
name: nodeport-service
spec:
type: NodePort
selector:
app: web
ports:
- port: 80
targetPort: 8080
nodePort: 30080 # Optional
Use case: External access without a cloud load balancer.
3. LoadBalancer
Exposes via a cloud provider load balancer.
apiVersion: v1
kind: Service
metadata:
name: loadbalancer-service
spec:
type: LoadBalancer
selector:
app: web
ports:
- port: 80
Use case: Public-facing applications in production.
4. ExternalName
Alias to an external service.
apiVersion: v1
kind: Service
metadata:
name: external-service
spec:
type: ExternalName
externalName: database.example.com
Use case: Reference external services as if they were internal services.
Comparison
| Type | Access | IP | Port | Use Case |
|---|---|---|---|---|
| ClusterIP | Internal | Cluster IP | 80 | Internal services |
| NodePort | External | Node IP | 30000-32767 | Dev/Test |
| LoadBalancer | External | Cloud IP | 80 | Production |
| ExternalName | Internal | CNAME | - | External services |
Summary
In this chapter, you learned:
ClusterIP: Internal access only (default)
NodePort: Exposes on a port on each node
LoadBalancer: Via cloud load balancer
ExternalName: Alias to an external service
Choosing: Based on the use case (internal/external, dev/prod)
Next Steps
Now that you know the types:
Chapter 4.3: Service Discovery
Lab 4.1: ClusterIP and NodePort Services
Chapter created: December 2024