Skip to main content

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 balance the load?

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
  • Abstraction of backend Pods

How Does it Work?​

Selectors​

The Service uses selectors to find the 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

# Service Details
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 IPs of Pods
Solution: Services with stable IP
Selectors: Identify backend Pods
Load Balancing: Automatic between Pods
Abstraction: Clients don't need to know the Pods


Next Steps​

Now that you understand Services:

Chapter 4.2: Types of Services
Lab 4.1: ClusterIP and NodePort Services


Chapter created on: December 2024