What Is ReplicaSet In Kubernetes
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed ReplicationController in Kubernetes.
https://cloudaffaire.com/what-is-replicationcontroller-in-kubernetes/
In this blog post, we will discuss ReplicaSet which is the new version of ReplicationController with some added features. In real-world scenarios, we hardly deploy a pod directly. Instead, pods are deployed using the pod controller. ReplicaSet is one of the pod controllers which ensures that a specified number of pod replicas are running at any one time.
What Is ReplicaSet?
A ReplicaSet’s purpose is to maintain a stable set of replica Pods running at any given time. As such, it is often used to guarantee the availability of a specified number of identical Pods. A ReplicaSet is defined with fields, including a selector that specifies how to identify Pods it can acquire, a number of replicas indicating how many Pods it should be maintaining, and a pod template specifying the data of new Pods it should create to meet the number of replicas criteria. A ReplicaSet then fulfills its purpose by creating and deleting Pods as needed to reach the desired number. When a ReplicaSet needs to create new Pods, it uses its Pod template.
A ReplicaSet is linked to its Pods via the Pods’ metadata.ownerReferences field, which specifies what resource the current object is owned by. All Pods acquired by a ReplicaSet have their owning ReplicaSet’s identifying information within their ownerReferences field. It’s through this link that the ReplicaSet knows of the state of the Pods it is maintaining and plans accordingly. A ReplicaSet identifies new Pods to acquire by using its selector. If there is a Pod that has no OwnerReference or the OwnerReference is not a Controller and it matches a ReplicaSet’s selector, it will be immediately acquired by said ReplicaSet.
ReplicationController Vs ReplicaSet:
- Replication Controller only supports equality-based selector. for eg: environment = production
- Replica Set supports the new set-based selector. This gives more flexibility. for eg: environment in (production, qa)
- rolling-update command is used for updating the replication controller.
- rollout command is used for updating the replica set.
ReplicaSet manifest file syntax:
1 2 3 4 5 6 7 8 9 10 |
## ---------------------- ## ReplicaSet YAML syntax ## ---------------------- apiVersion: apps/v1 kind: ReplicaSet metadata: spec: |
replicasetMetadata attributes:
- annotations: Key value map stored with a resource for arbitrary metadata.
- clusterName: The name of the cluster which the object belongs to.
- creationTimestamp: Timestamp representing the server time when this object was created.
- deletionGracePeriodSeconds: Seconds allowed for gracefully terminate before forcefully removed from the system.
- deletionTimestamp: RFC 3339 date and time at which this resource will be deleted. Is not directly settable by a client.
- finalizers: Must be empty before the object is deleted from the registry.
- generateName: Optional prefix, used by the server, to generate a unique name ONLY IF the Name field has not been provided.
- generation: A sequence number representing a specific generation of the desired state. Populated by the system. Read-only.
- initializers: An initializer is a controller which enforces some system invariant at object creation time.
- labels: Map of string keys and values that can be used to organize and categorize (scope and select) objects.
- name: Name must be unique within a namespace.
- namespace: Namespace defines the space within each name must be unique, default namespace is default.
- ownerReferences: List of objects depended by this object.
- resourceVersion: An opaque value that represents the internal version of this object used to determine when objects have changed.
- selfLink: SelfLink is a URL representing this object. Populated by the system. Read-only.
- uid: UID is the unique in time and space value for this object
replicasetSpec attributes:
- minReadySeconds: Minimum number of seconds for which a newly created pod should be ready without any of its container crashing, for it to be considered available. Defaults to 0
- replicas: Replicas is the number of desired replicas. This is a pointer to distinguish between explicit zero and unspecified. Defaults to 1.
- selector: Selector is a label query over pods that should match the Replicas count. If Selector is empty, it is defaulted to the labels present on the Pod template. Label keys and values that must match in order to be controlled by this replication controller, if empty defaulted to labels on Pod template.
- template: Template is the object that describes the pod that will be created if insufficient replicas are detected.
ReplicaSet Demo:
Step 1: Create the replicaset.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
############################## ## ReplicaSet In Kubernetes ## ############################## ## Prerequisites ## One Kubernetes Cluster with at least one node ## You can install your own or use online versions using below links ## https://cloudaffaire.com/how-to-install-kubernetes-cluster-in-linux/ ## https://www.katacoda.com/courses/kubernetes/playground ## http://labs.play-with-k8s.com/ ## ----------------- ## Create replicaset ## ----------------- ## Create the replicaset manifest file mkdir myapp && vi myapp/myreplicasetv1.yaml -------------------- apiVersion: apps/v1 kind: ReplicaSet metadata: name: myreplicaset labels: app: myapp spec: replicas: 2 selector: matchLabels: app: myapp template: metadata: labels: app: myapp spec: containers: - image: nginx name: mycontainer ports: - containerPort: 80 name: http --------------------- :wq ## Note: for evaluation of multiple lebels, use matchExpressions instead of matchLabels. ## For simplicity, we are using a single label app: myapp ## Validate the yaml configuration file kubectl create --dry-run --validate -f myapp/myreplicasetv1.yaml ## Create the replicaset kubectl apply -f myapp/myreplicasetv1.yaml |
Step 2: View replicaset details.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
## ----------------------- ## View replicaset details ## ----------------------- ## View the replicaset kubectl get replicaset myreplicaset kubectl get rs ## abbreviation of replicaset kubectl get rs -l app=myapp ##list rs by lebel kubectl get rs -o wide ##get detailed output ## View rs configuration kubectl get rs myreplicaset -o yaml ## View rs details kubectl describe rs myreplicaset ## View rs lebels kubectl get rs --show-labels ## View the pod kubectl get pod -l app=myapp -o wide kubectl get pods -l 'app in (myapp)' -o wide ## NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES ## myreplicaset-g7rp4 1/1 Running 0 5m25s 10.244.1.14 system3 ## myreplicaset-vh79k 1/1 Running 0 5m25s 10.244.2.18 system2 |
Step 3: Scale up and scale down the number of replicas.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
## ------------------------------------------- ## Scale up and scale down numbers of replicas ## ------------------------------------------- ## Scale up number of pods to three kubectl scale rs myreplicaset --replicas=3 ## View pods kubectl get pods -l 'app in (myapp)' -o wide ## Scale down number of pods to one kubectl scale rs myreplicaset --replicas=1 ## View pods kubectl get pods -l 'app in (myapp)' -o wide ## Check the difference between configuration file and actual state kubectl diff -f myapp/myreplicasetv1.yaml ## Note the count of replica is 1 in actual but 2 in config file. ## Edit the rs to make consistant with config file ## vi editor will be opned, change replicas: 1 from 2 and save kubectl edit -f myapp/myreplicasetv1.yaml ## View pods kubectl get pods -l 'app in (myapp)' -o wide |
Step 4: Delete a pod managed by replicaset.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
## -------------------------- ## Delete a pod managed by rs ## -------------------------- ## View pods kubectl get pod ## NAME READY STATUS RESTARTS AGE ## myreplicaset-6cfsb 1/1 Running 0 112s ## myreplicaset-g7rp4 1/1 Running 0 20m ## Delete a pod kubectl delete pod myreplicaset-6cfsb #replace pod name ## Get the pod kubectl get pod ## NAME READY STATUS RESTARTS AGE ## myreplicaset-78rth 1/1 Running 0 10s ## myreplicaset-g7rp4 1/1 Running 0 21m ## One new pod has been autometically spinned up to make the replica count 2 |
Step 5: Make a connection to the pod managed by replicaset.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
## ------------------------------------- ## Connect to your pods under replicaset ## ------------------------------------- ## Expose your rs kubectl expose rs myreplicaset --type=NodePort --port=80 ## Get pod ip address kubectl get pods -l 'app in (myapp)' -o wide ## NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES ## myreplicaset-78rth 1/1 Running 0 2m37s 10.244.1.15 system3 ## myreplicaset-g7rp4 1/1 Running 0 23m 10.244.1.14 system3 ## Call your pod uisng pod ip address (nginx web server) curl 10.244.1.15:80 curl 10.244.1.14:80 |
Step 6: Cleanup.
1 2 3 4 5 6 7 8 9 10 11 12 |
## ------- ## Cleanup ## ------- ## Delete the nodeport service kubectl delete service myreplicaset ## delete the replicaset kubectl delete rs myreplicaset ## delete myapp directory rm -rf myapp |
Hope you enjoyed this article. In real world sceanrios we hardly deploy a pod directly. Instead, pods are deployed using pod controller. In the next blog post, we will discuss one of the pod controller deployment.
To get more details on Kubernetes you can follow the below link.
https://kubernetes.io/docs/home/
To get more details on Docker you can follow the below links.
https://cloudaffaire.com/category/devops/docker/