why selector in replicaSet Kubernetes

Viewed 377

enter image description hereI don't' see why adding selector when we are creating replicaSet I thought maybe we can select different pod but we can't so I don't see what is used for

apiVersion: apps/v1
kind: ReplicaSet
metadata:
    name: rs-name
spec:
    replicas: 2
    selector: // here the selector
        matchLabels:
            podptr: my-pod-l
    template:
        metadata:
            name: pods-name
            labels:
                podptr: my-pod-l // here it's lable
        spec:
            containers:
                - name: cons-name
                  image: nginx
                  ports:
                      - containerPort: 50
1 Answers

Because you can have more than one label on the resulting pods and it doesn't know which of them to use for tracking. The selector labels are read-only once initialized, but other labels you can add and remove as needed for other purposes.

Related