Chapter 12 of 47 · Putting it together

Pod vs ReplicaSet vs Deployment vs Service

Four objects that are easy to mix up. Three of them form one chain of ownership; the fourth sits beside them and only deals with traffic. This chapter puts them side by side with one complete example.

In one line each

Pod

Runs your container. Temporary, with a temporary IP.

ReplicaSet

Counts Pods and keeps exactly N alive.

Deployment

Changes versions safely by managing ReplicaSets.

Service

Routes traffic to Pods through one stable name.

An analogy: in a restaurant, Pods are the cooks. The ReplicaSet is the shift manager who makes sure 3 cooks are always on duty. The Deployment is the head chef who switches the kitchen to a new menu one cook at a time, and switches back if diners complain. The Service is the restaurant's phone number: customers call it, never a cook's personal number.

Side by side

PodReplicaSetDeploymentService
JobRun containersKeep N Pods aliveRoll out and roll back versionsStable address + load balancing
apiVersionv1apps/v1apps/v1v1
ContainsContainersPod templatePod template (makes ReplicaSets)Selector + ports, no Pods
Creates—PodsReplicaSetsEndpointSlices (the IP list)
Uses labels tobe foundcount its Podsfind its ReplicaSets' Podspick where traffic goes
Self-healingRestarts its containers only✓ replaces Pods✓ (through ReplicaSet)Skips Pods that are not ready
Scaling✗✓✓—
Rolling update / rollback✗✗✓—
Stable IP / DNS name✗ changes✗✗✓
Do you write it?Rarely (debugging, one-offs)Almost neverYes, for every appYes, for every app that gets called

How they connect

Ownership on the left, traffic on the right, labels in the middleDeployment owns ReplicaSet, which owns three Pods labelled app=posts. Separately, a client calls posts-service, which selects the same Pods by the label app=posts. Ownership (keeps Pods alive) Traffic (finds Pods) Deployment posts owns ReplicaSet posts-7d9f8 owns Pod · app=posts Pod · app=posts Pod · app=posts Client posts-serviceselector app=posts routes to
Deployment and Service never talk to each other. They meet only at the Pod labels.

One complete example

This is the file you will actually write for the posts app: a Deployment and a Service, separated by ---. Click the highlighted parts to see what must match what.

# posts.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: posts
spec:
  replicas: 3
  selector:
    matchLabels:
      app: posts
  template:
    metadata:
      labels:
        app: posts
    spec:
      containers:
        - name: posts
          image: micro-posts:v1
          ports:
            - containerPort: 4000
---
apiVersion: v1
kind: Service
metadata:
  name: posts-service
spec:
  selector:
    app: posts
  ports:
    - port: 80
      targetPort: 4000

The three app: posts

The Deployment's selector, the Pod template's labels and the Service's selector must all agree. The Deployment (and its ReplicaSet) use it to count Pods; the Service uses it to find Pods for traffic. This is the only link between the two halves of the file.

$ kubectl apply -f posts.yaml
deployment.apps/posts created
service/posts-service created

What kubectl shows

Click each line of kubectl get all to see which object it is and who created it:

$ kubectl get all
NAME                         READY   STATUS
pod/posts-7d9f8c6b5d-4kq2p   1/1     Running
pod/posts-7d9f8c6b5d-m7x4t   1/1     Running
pod/posts-7d9f8c6b5d-zt9wn   1/1     Running

NAME                    TYPE        CLUSTER-IP    PORT(S)
service/posts-service   ClusterIP   10.96.40.12   80/TCP

NAME                    READY   UP-TO-DATE   AVAILABLE
deployment.apps/posts   3/3     3            3

NAME                               DESIRED   CURRENT   READY
replicaset.apps/posts-7d9f8c6b5d   3         3         3

Deployment posts

The one you wrote. UP-TO-DATE = Pods running the latest template; AVAILABLE = ready Pods.

Who handles it?

For each event, pick the object that deals with it.

Which one do I write?

SituationWrite
A web app or API (posts, users, comments)Deployment + Service
A background worker nobody callsDeployment only
Quick debugging containerA bare Pod (kubectl run), deleted afterwards
Anything with ReplicaSet in itLet the Deployment create it
A database that needs stable names and disksStatefulSet + Service (later chapter)
The pattern to remember: Deployment → ReplicaSet → Pods keeps your app alive and up to date; Service → Pods keeps it reachable. Labels connect the two.

Pod vs ReplicaSet vs Deployment vs Service: common questions

Do I need both a Deployment and a Service?

Usually yes. The Deployment keeps your Pods running and updated; the Service gives them a stable name so other apps can call them. They connect through matching labels.