Service Type in k8s
Briefing
1 ๐๐ฅ๐ฎ๐ฌ๐ญ๐๐ซ๐๐ (๐๐๐๐๐ฎ๐ฅ๐ญ)
- Exposes the Service internally within the cluster.
- Pods in the same cluster can access the Service using its ClusterIP address or DNS name. Common use case: Microservices communicating with each other.
2 ๐๐จ๐๐๐๐จ๐ซ๐ญ
- Exposes the Service externally by binding a port to each Node in the cluster.
- Accessible using
<NodeIP>:<NodePort>
. Common use case: Direct access for debugging or development.
3 ๐๐จ๐๐๐๐๐ฅ๐๐ง๐๐๐ซ
- Creates a cloud provider’s external load balancer (e.g., AWS ELB, Google Cloud Load Balancer).
- Exposes the Service to the internet with a public IP address. Common use case: Production applications requiring public access
4 ๐๐ฑ๐ญ๐๐ซ๐ง๐๐ฅ๐๐๐ฆ๐
- Maps the Service to an external DNS name.
- Does not create a proxy but resolves the DNS name directly. Common use case: Accessing external services or legacy systems.
5 ๐๐๐๐๐ฅ๐๐ฌ๐ฌ ๐๐๐ซ๐ฏ๐ข๐๐
- A Headless Service is a specialized type of Kubernetes Service that does not assign a ClusterIP.
- Instead of routing traffic through a proxy (like kube-proxy), it directly returns the IP addresses of the Pods backing the Service. Common use case: Used with StatefulSets to ensure each Pod gets its own DNS entry.
Service Type ClusterIP
- ClusterIP is the default service type
- the service is assigned an IP from a designated service CIDR range
- DNS pattern:
<namespace_name>.svc.cluster.local.
e.g.
apiVersion: v1
kind: Service
metadata:
name: web1-svc
spec:
selector:
app: web1
ports:
- port: 80
targetPort: 8080
Service Type NodePort
Service Type ExternalName
e.g.
kind: Service
apiVersion: v1
metadata:
name: prod-mongodb
namespace: prod
spec:
type: ExternalName
externalName: mymongodb.documents.azure.com
Service Type LoadBalancer
e.g.
kind: Service
apiVersion: v1
metadata:
name: web-svc
spec:
type: LoadBalancer
selector:
app: web
ports:
- protocol: TCP
port: 80
targetPort: 8081
loadBalancerIP: 13.12.21.31
loadBalancerSourceRanges:
- "142.43.0.0/16"
Ingress and Ingress Controllers
The Ingress API is basically an HTTP-level router that allows for host-based and path-based rules to direct to specific backend services.
Reference
- Kubernetes Best Practices, Brendan Burns