What is ReplicaSets?
Like ReplicationController, ReplicaSet also helps to maintain number of Pods of specific types.
It’s a new generation of ReplicationController and replaces it completely.

(ReplicationControllers will eventually be deprecated)
What is the main difference between ReplicaSet and ReplicationController?

The key difference between the ReplicaSet and the ReplicationController is, the replication controller only supports equality-based selector whereas the replica set have more options for selector.
ReplicationController’s label selector only allows matching pods that include a certain label, a ReplicaSet’s selector also allows matching pods that lack a certain label or pods that include a certain label key, regardless of its value.
- For example, a single ReplicationController can’t match pods with the label env=production and those with the label env=devel at the same time. It can only match either pods with the env=production label or pods with the env=devel label. But a single ReplicaSet can match both sets of pods and treat them as a single group.
- Similarly, a ReplicationController can’t match pods based merely on the presence of a label key, regardless of its value, whereas a ReplicaSet can. For example, a Replica-Set can match all pods that include a label with the key env, whatever its actual value is(you can think of it as env=*).
How to create ReplicaSet using definition file?
Create a definition file:
apiVersion: apps/v1beta2 kind: ReplicaSet metadata: name: think-rs labels: app-replica: think-app spec: replicas: 3 selector: matchLabels: app: think-app template: metadata: name: think-app-pod labels: app: think-app spec: containers: - name: think-container image: thinkshcolar
The only difference is in the selector. Instead of listing labels the pods need to have directly under the selector property, you’re specifying them under selector.matchLabels.
kubectl create -f replicaset-def.yml
Check number of pods running after running above command:
$ kubectl get pods NAME READY STATUS RESTARTS AGE think-app-pod-53thy 0/1 ContainerCreating 0 2s think-app-pod-k0xz6 0/1 ContainerCreating 0 2s think-app-pod-q3vkg 0/1 ContainerCreating 0 2s
Why Selector if we are already giving Pod definition?
Good question.

ReplicaSet can manage Pods not created by itself. So when you create a ReplicaSet but there are Pods already present in cluster which match the conditions of selector in ReplicaSet and say total number of such Pods are equivalent to replicas defined in ReplicaSet, then ReplicaSet don’t do anything.
How to check ReplicaSet present in Cluster?
Command:
kubectl get replicaset
Output:
$ kubectl get rs NAME DESIRED CURRENT READY AGE think-rs 3 3 3 3s
How to get detailed information ReplicaSets ?
To get detailed information about all ReplicaSet, use Command:
kubectl describe replicaset
To get detailed information about specific ReplicaSet, use Command:
kubectl describe rs <replicaset-name>
How to delete specific ReplicaSet ?
Command:
kubectl delete replicaset <replicaset-name>
Example:
kubectl delete replicaset think-rs
How to scale Pods using existing ReplicaSet?

Update replicas in replicaset-def.yml:
apiVersion: apps/v1beta2 kind: ReplicaSet metadata: name: think-rs labels: app-replica: think-app spec: replicas: 9 selector: matchLabels: app: think-app template: metadata: name: think-app-pod labels: app: think-app spec: containers: - name: think-container image: thinkshcolar
And then run: kubectl replace -f replicaset-def.yml
Or there options to do it without editing definition file:
Option 1:
kubectl scale --replicas=6 -f replica-def.yaml
Option 2:
kubectl scale --replicas=6 replicaset think-rs
How to express better label matches in ReplicaSet?
You can use matchExpressions instead of matchLabels, under selector. Example:
select: matchExpressions: - key: app operator: In values: - think-app - another-app
Valid operators:
- In—Label’s value must match one of the specified values.
- NotIn—Label’s value must not match any of the specified values.
- Exists—Pod must include a label with the specified key (the value isn’t important). When using this operator, you shouldn’t specify the values field.
- DoesNotExist—Pod must not include a label with the specified key. The values property must not be specified.
If you specify multiple expressions, all those expressions must evaluate to true for the selector to match a pod. If you specify both matchLabels and matchExpressions, all the labels must match and all the expressions must evaluate to true for the pod to match the selector.
0 Comments