CodeBlanks Logo Icon CodeBlanks

ReplicaSet selector

Learn to match Pods with ReplicaSet selector.

ReplicaSet selector

A ReplicaSet owns pods by their labels, not by their names. If a pod matching the selector and without an owner is created by hand, the ReplicaSet counts it too and deletes one because there are too many. That is why labels should be chosen to be unique across workloads.

selector:
  matchExpressions:
    - key: app
      operator: In
      values: [api]
    - key: katman
      operator: NotIn
      values: [test]

Detaching a pod from a ReplicaSet

If you change a pod's label, it no longer matches the selector; the ReplicaSet lets it go and opens a new one in its place. It's a handy technique for taking a problem pod out of traffic and the ReplicaSet to inspect it without deleting it:

kubectl label pod api-5d8f7c9b4-qrstu app=inceleme --overwrite
kubectl get pods --show-labels
# api-5d8f7c9b4-qrstu   app=inceleme   <- detached
# api-5d8f7c9b4-zzzzz   app=api        <- opened in its place

Practice Detach and inspect a pod

Write three commands: overwrite the app label of the api-5d8f7c9b4-qrstu pod with inceleme; list the pods with their labels; show the logs of the detached pod.

Starter code

terminal
# buraya yaz

Sample solution

terminal
kubectl label pod api-5d8f7c9b4-qrstu app=inceleme --overwrite
kubectl get pods --show-labels
kubectl logs api-5d8f7c9b4-qrstu

Expected output

pod/api-5d8f7c9b4-qrstu labeled
NAME                  READY   STATUS    LABELS
api-5d8f7c9b4-qrstu   1/1     Running   app=inceleme,pod-template-hash=5d8f7c9b4
api-5d8f7c9b4-zzzzz   1/1     Running   app=api,pod-template-hash=5d8f7c9b4
ERROR veritabanı zaman aşımı

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What happens if you create by hand a pod matching a 3-replica ReplicaSet's selector?

  • It errors
  • The ReplicaSet sees 4 pods and deletes one Correct answer
  • The pod is ignored
  • The ReplicaSet switches to 4 replicas

Explanation: A ReplicaSet adopts matching pods that have no owner.

2 Overwrite an existing label.

Question
kubectl label pod api-5d8f7c9b4-qrstu app=inceleme ____
Answer
kubectl label pod api-5d8f7c9b4-qrstu app=inceleme --overwrite

Explanation: --overwrite is needed to change an existing key.

3 Match the operators.

Term Meaning
In One of the values
NotIn None of the values
Exists Key exists
DoesNotExist Key missing

All lessons in Intermediate