🎓 A1Learner
🔍
0 of 51 lessons completed

Kubernetes: What Is a Service?

A Service is a stable address for a set of pods. Pods die and get new addresses, and a Service hides that change from everyone who calls the app.

Pods move, the Service stays

A pod can die and be replaced in seconds. The new pod has a new address. A Service keeps one address that always points at the live pods.

kubectl get pods -l app=web

A ClusterIP Service

ClusterIP is the default type. It gives the app an address that only pods inside the cluster can reach.

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

Read the Service back

The Service gets a cluster address that never changes while the Service exists. The pods behind it can come and go.

kubectl get service web

Example

kubectl apply -f web-service.yaml
kubectl get service web

Key points

  • Service: a stable address that forwards traffic to a set of pods
  • ClusterIP: a Service that is only reachable inside the cluster
  • NodePort: a Service that opens a port on every node for outside traffic
  • LoadBalancer: a Service that asks the cloud for a public address
  • Use a selector to choose which pods a Service sends traffic to
  • Use a ClusterIP Service for traffic that stays inside the cluster

In this example, the first command creates a ClusterIP Service called web from the file. The second command reads it back. The Service has a cluster address of 10.96.120.45 and no external address, which is exactly what ClusterIP means.

Note

a Service does not run your app, it only points at the pods that do

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

Why does an app need a Service when its pods already have addresses?