Introduction to Services

A Service gives a stable network endpoint and load balancing to a changing set of Pods.

Pods come and go, and each new Pod gets a fresh IP address. If your frontend talked to a Pod's IP directly, it would break every time that Pod was replaced. A Service solves this.

A stable front door

A Service provides one fixed IP and DNS name. It uses a label selector to find its Pods and load-balances traffic across all of them. As Pods appear and disappear, the Service updates its list automatically.

A Service load-balancing traffic across three PodsClientServiceapp=webPodPodPod
The Service matches Pods by label and spreads traffic across them.

Service types

The type field decides who can reach the Service: ClusterIP (inside only), NodePort, and LoadBalancer — each covered next.

Example

Example · yaml
apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 80

When to use it

  • A team creates a Service so that their application's pods are reachable by a stable DNS name even as pod IPs change during rolling updates.
  • A microservices architecture uses Services to decouple consumer from producer: the consumer calls a stable Service name regardless of which pods are running.
  • An operator checks Service endpoints to verify that all healthy pods are registered after a failed deployment.

More examples

Expose a Deployment as a Service

Creates a Service that routes port 80 to container port 8080 across all pods selected by the Deployment's label.

Example · bash
kubectl expose deployment web --port=80 --target-port=8080
kubectl get service web
kubectl describe service web

Service manifest

Declares a ClusterIP Service that selects pods labelled app=web and forwards port 80 to container port 8080.

Example · yaml
apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 8080
  type: ClusterIP

Verify service endpoints

Shows the live pod IPs behind a Service and tests connectivity from an ephemeral busybox pod using the Service DNS name.

Example · bash
kubectl get endpoints web
# NAME   ENDPOINTS                       AGE
# web    10.244.1.5:8080,10.244.2.3:8080  5m

kubectl run test --image=busybox --rm -it -- wget -qO- http://web

Discussion

  • Be the first to comment on this lesson.