🌐 Service Discovery = Microservices
Services need to find each other. Service discovery and load balancing are essential for microservices.
📝 Service Types
# ClusterIP (Default)
apiVersion: v1
kind: Service
metadata:
name: app-service
spec:
selector:
app: myapp
ports:
- port: 8080
targetPort: 8080
type: ClusterIP
# NodePort
apiVersion: v1
kind: Service
spec:
type: NodePort
ports:
- port: 8080
nodePort: 30080
# LoadBalancer
apiVersion: v1
kind: Service
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 8080
# ExternalName
apiVersion: v1
kind: Service
spec:
type: ExternalName
externalName: external.example.com
🎯 Advanced Patterns
# Headless Service
apiVersion: v1
kind: Service
metadata:
name: stateful-service
spec:
clusterIP: None
selector:
app: stateful-app
ports:
- port: 8080
# External Load Balancer (AWS)
apiVersion: v1
kind: Service
metadata:
name: web-service
annotations:
service.beta.kubernetes.io/aws-load-balancer-type: nlb
spec:
type: LoadBalancer
# Ingress with Service Discovery
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: main-ingress
spec:
rules:
- http:
paths:
- path: /api
pathType: Prefix
backend:
service:
name: api-service
port:
number: 8080
# Service with Session Affinity
apiVersion: v1
kind: Service
spec:
sessionAffinity: ClientIP
sessionAffinityConfig:
clientIP:
timeoutSeconds: 10800
💡 Service Discovery Tips
- Use ClusterIP for internal services
- Use LoadBalancer for external access
- Use Headless service for stateful apps
- Use Ingress for HTTP routing
- Monitor service health
Service discovery and load balancing are the backbone of microservices architecture. They enable scalable, resilient applications.
