Kubernetes Day 4 - pranavkumarpk01/MD-DevOps GitHub Wiki
๐งฉ Kubernetes Services - End-to-End Guide
In Kubernetes, Pods are short-lived and dynamic. When they restart or scale, their IPs change. To maintain consistent communication across services and ensure accessibility, Kubernetes Services act as a stable endpoint and load balancer.
๐ What is a Kubernetes Service?
A Kubernetes Service is an abstraction layer that defines a logical set of Pods and a policy by which to access them. Services provide:
- A stable DNS name
- A stable IP address
- Optional load balancing for traffic
๐ Why Services Are Needed
- Pods are ephemeral and can die or be recreated with new IPs
- Services allow apps to reliably communicate
- Services offer load balancing across multiple Pods
- You can expose apps inside or outside the cluster
๐งฑ Types of Kubernetes Services
Type | Description | Scope | Example Use Case |
---|---|---|---|
ClusterIP |
Default; exposes the Service on internal IP | Internal Only | Backend to Database communication |
NodePort |
Exposes Service on a static port on nodes | External Access | Quick dev/test exposure |
LoadBalancer |
Provisions external cloud load balancer | Public Access | Production frontend API/UI |
ExternalName |
Maps Service to external DNS | External Redirect | Use RDS or external API from a Pod |
๐๏ธ Real-World Architecture
Example: A full-stack app
- ๐น Frontend (React App) โ needs to be accessed by users over the internet
โLoadBalancer
- ๐น Backend (Node.js/Java Spring) โ accessed only by frontend
โClusterIP
- ๐น Database (PostgreSQL/MongoDB) โ accessed only by backend
โ No Service or Headless Service (StatefulSet)
๐ฆ Service YAML Examples
1๏ธโฃ ClusterIP Service (Default)
apiVersion: v1
kind: Service
metadata:
name: backend-service
spec:
selector:
app: backend
ports:
- protocol: TCP
port: 80 # Service port (inside cluster)
targetPort: 5000 # Container port
type: ClusterIP
Explanation:
- Selects Pods with label
app=backend
- Routes traffic from port
80
to container port5000
- Only accessible within the cluster
2๏ธโฃ NodePort Service
apiVersion: v1
kind: Service
metadata:
name: frontend-service
spec:
selector:
app: frontend
ports:
- port: 80
targetPort: 3000
nodePort: 30080
type: NodePort
Explanation:
- Exposes app externally via
NodeIP:30080
- Great for dev or testing
nodePort
must be between30000-32767
3๏ธโฃ LoadBalancer Service (Cloud Only)
apiVersion: v1
kind: Service
metadata:
name: frontend-loadbalancer
spec:
selector:
app: frontend
ports:
- protocol: TCP
port: 80
targetPort: 3000
type: LoadBalancer
Explanation:
- Creates a cloud load balancer (AWS/GCP/Azure)
- Auto-assigns a public IP
- Ideal for production/public APIs
4๏ธโฃ ExternalName Service
apiVersion: v1
kind: Service
metadata:
name: external-db
spec:
type: ExternalName
externalName: database.example.com
Explanation:
- No selector or endpoint created
- Requests to this service DNS redirect to external FQDN
โ๏ธ How Services Work Internally
- kube-proxy runs on each node.
- It watches for Services and Endpoints.
- It sets up:
iptables
orIPVS
rules for routing- DNS via
kube-dns
orCoreDNS
for name resolution
- Load balances traffic to matching Pods
๐ฐ๏ธ Accessing Services
Method | Usage |
---|---|
kubectl port-forward |
Local testing |
NodePort |
Access via IP of any cluster node |
LoadBalancer |
Get public IP from your cloud provider |
Ingress (with controller) |
Advanced routing with domains and paths |
๐ Useful Commands
# Create a service from a YAML file
kubectl apply -f service.yaml
# List all services
kubectl get svc
# Get detailed info
kubectl describe svc <service-name>
# Delete a service
kubectl delete svc <service-name>
โ Best Practices
- Use ClusterIP for internal microservices communication
- Use NodePort only for local or quick access
- Use LoadBalancer in production environments
- For external APIs (e.g., AWS RDS, Stripe), use ExternalName
- For traffic routing and SSL, consider Ingress + TLS certs
๐งช Testing Locally
# Port forward backend to localhost
kubectl port-forward svc/backend-service 8080:80
# Now you can access it on http://localhost:8080
๐งฉ Summary
Type | Internal | External | Cloud Integration | Use For |
---|---|---|---|---|
ClusterIP | โ | โ | โ | Microservice to microservice |
NodePort | โ | โ | โ | Quick dev testing |
LoadBalancer | โ | โ | โ | Public APIs, websites |
ExternalName | โ | โ | โ (DNS only) | RDS, external APIs |