Hey there everyone, i'm a little confused as to why i should specify the selecto . . .

Rediane Zemmouri:
Hey there everyone, i’m a little confused as to why i should specify the selector to match the labels of the pod defined in the replicaset… I mean, the pod is already defined in the replicaset… it looks like an extra step to me

Mariusz Gomse:
The reason is that RS (ReplicaSet) can control also pods not created by this RS itself. This gives more flexibility IMHO. e.g :
• create some pod(s) with label tier: my-tmp-tier
• create RS with matching selector
RS won’t create pods according to its pod’s template if there will be already correct pods (maching labels)

Of course when “old” pod will die then RS will create new according to its template.

Tob e honest it is theory because I never had such a case (or others) where this functionality will be useful. However as you can see it gives more flexibility.

Before RS was introduced there was only RC (Replication Controller) and its manifest don’t have selector - less confusing but also less flexible.

Madhan Kumar:
RS does have a selector , but it is is optional .

Mariusz Gomse:
IMHO you are wrong @Madhan Kumar. I even tried to create RS without selector:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: tmp-rs
  labels:
    key: value
spec:
  replicas: 2
  template:
    metadata:
      labels:
        tier: frontend
    spec:
      containers:
      - name: nginx
        image: nginx:alpine

the result is:

error: error validating "rs.yaml": error validating data: ValidationError(ReplicaSet.spec): missing required field "selector" in io.k8s.api.apps.v1.ReplicaSetSpec; if you choose to ignore these errors, turn validation off with --validate=false

Madhan Kumar:
sry it was a typo :slightly_smiling_face: . I was referring to RC , not RS .