Chapter 4.1 - Introduction to Services
Learning Objectives
By the end of this chapter, you will be able to:
- Understand the problem that Services solve
- Explain the role of Services
- Understand how Services work
- Create a simple Service
The Problem
Pods have ephemeral IPs that change:
Problems:
- IPs change when Pods restart
- How to find the backend Pods?
- How to load balance?
The Solution: Services
A Service exposes a set of Pods as a network service with a stable IP:
Advantages:
- Stable IP
- Automatic load balancing
- Service discovery
- Backend Pod abstraction
How Does It Work?
Selectors
The Service uses selectors to find Pods:
apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
selector:
app: web
tier: frontend
ports:
- port: 80
targetPort: 8080
YAML Declaration
apiVersion: v1
kind: Service
metadata:
name: my-service
labels:
app: my-app
spec:
selector:
app: my-app
tier: frontend
ports:
- protocol: TCP
port: 80 # Service port
targetPort: 8080 # Container port
type: ClusterIP # Service type
Useful Commands
# Create a Service
kubectl apply -f service.yaml
# List Services
kubectl get services
# Details of a Service
kubectl describe service my-service
# View Endpoints
kubectl get endpoints my-service
# Delete a Service
kubectl delete service my-service
Summary
In this chapter, you learned:
Problem: Ephemeral Pod IPs
Solution: Services with stable IP
Selectors: Identify backend Pods
Load Balancing: Automatic across Pods
Abstraction: Clients do not need to know about Pods
Next Steps
Now that you understand Services:
Chapter 4.2: Types of Services
Lab 4.1: ClusterIP and NodePort Services
Chapter created: December 2024