Table of Contents
In this article I will take you through 5 practical steps to create and use ReplicaSet in kubernetes with examples. ReplicaSet is an important concept brought up in Kubernetes to make sure that the required resources(pods) are always available. It is known to be the successor of Replication Controller. Without ReplicaSet if you have to create replicas of a pod then you need to separately write a manifest file and use it every time for creating the replicas.
This will become an additional overhead in a sense that we need to keep monitoring the pods in case any of the pods go down. While in the case of using ReplicaSet you don’t need to do anything. Controller will create the pod the moment any of the running pod go down. This makes our life easy. So we should all be thankful. Check more about ReplicaSet on Official Documentation.
What is ReplicaSet in Kubernetes
ReplicaSet is known to be the Controller in Kubernetes which makes sure that any given point of time number of replicas are running as per the defined value.
Steps to Create and Use ReplicaSet in Kubernetes with Examples
Also Read: Horizontal Scale Up/Down the Pods Based on CPU Utilization in Kubernetes
Step 1: Create ReplicaSet in Kubernetes Using YAML File
Using YAML file to create ReplicaSet in Kubernetes is quite popular and is also the most preferable way. The controller that Kubernetes going to create through YAML file will be defined by the parameter kind
which will be set as ReplicaSet
in this case. Then you need to define the other important parameters like name, labels, app, spec information where you need to set the number of replicas, image which you need to use and so on.
[root@localhost ~]# vi replica.yaml apiVersion: apps/v1 kind: ReplicaSet metadata: name: web-app labels: app: web-app tier: webserver spec: replicas: 2 selector: matchLabels: tier: webserver template: metadata: labels: tier: webserver spec: containers: - name: nginx image: nginx
Once the YAML file is created then you can simply use kubectl apply -f replica.yaml
command to create the web-app
ReplicaSet as shown below.
[root@localhost ~]# kubectl apply -f replica.yaml replicaset.apps/web-app created
NOTE:
root
user to run all the below commands. You can use any user with sudo
access to run all these commands. For more information Please check Step by Step: How to Add User to Sudoers to provide sudo
access to the User.Step 2: Check All the ReplicaSets in Kubernetes
Now that you have the ReplicaSet created you can verify the list of ReplicaSet using kubectl get rs
command.
[root@localhost ~]# kubectl get rs NAME DESIRED CURRENT READY AGE web-app 2 2 2 70s
To verify the replicas created by the ReplicaSet YAML file you can use kubectl get pods
commands and it will show all the pods needs to be in running state.
[root@localhost ~]# kubectl get pods NAME READY STATUS RESTARTS AGE web-app-g6ftg 1/1 Running 0 26s web-app-k9gdn 1/1 Running 0 26s
NOTE:
Step 3: Verify ReplicaSet By Deleting Pod
The next important thing is to verify if the ReplicaSet is working as expected. To verify this you can simply delete any of the create pods and check if you are getting pod replicas. For example here we are deleting web-app-g6ftg
pod using kubectl delete pod web-app-g6ftg
command.
[root@localhost ~]# kubectl delete pod web-app-g6ftg pod "web-app-g6ftg" deleted
Then if we again check the pod status you can see that it is creating replica pod immediately after deleting the pod.
[root@localhost ~]# kubectl get pods NAME READY STATUS RESTARTS AGE web-app-9p5j9 0/1 ContainerCreating 0 9s web-app-k9gdn 1/1 Running 0 2m44s
After creating the container it will come to running state as you can see below.
[root@localhost ~]# kubectl get pods NAME READY STATUS RESTARTS AGE web-app-9p5j9 1/1 Running 0 57s web-app-k9gdn 1/1 Running 0 3m32s
Step 4: Check more info about ReplicaSet
If you are looking to through the complete information about the create ReplicaSet then you can see it by using kubectl describe rs/<replicaset_name>
command. Here we have our web-app
ReplicaSet created so to describe this you need to run kubectl describe rs/web-app
command as used below.
[root@localhost ~]# kubectl describe rs/web-app Name: web-app Namespace: cyberithub Selector: tier=webserver Labels: app=web-app tier=webserver Annotations: kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"apps/v1","kind":"ReplicaSet","metadata":{"annotations":{},"labels":{"app":"web-app","tier":"webserver"},"name":"web-app","n... Replicas: 2 current / 2 desired Pods Status: 2 Running / 0 Waiting / 0 Succeeded / 0 Failed Pod Template: Labels: tier=webserver Containers: nginx: Image: nginx Port: <none> Host Port: <none> Environment: <none> Mounts: <none> Volumes: <none> Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal SuccessfulCreate 4m18s replicaset-controller Created pod: web-app-k9gdn Normal SuccessfulCreate 4m18s replicaset-controller Created pod: web-app-g6ftg Normal SuccessfulCreate 103s replicaset-controller Created pod: web-app-9p5j9
Step 5: Delete ReplicaSet in Kubernetes
Once you are done with web-app
ReplicaSet you can also delete it by using kubectl delete rs web-app
command.
[root@localhost ~]# kubectl delete rs web-app replicaset.apps "web-app" deleted
So if you again check the list you will not able to see as it is deleted successfully.
[root@localhost ~]# kubectl get rs
Conclusion
In this article, we have seen the basic steps required to create ReplicaSet in Kubernetes with the help of example. We also looked at the important Kubernetes commands required to create, list and delete the ReplicaSet. Hopefully these steps were useful for you.
Popular Recommendations:-
3 Easy Methods to Deploy/Create Pods in Kubernetes Cluster
How to Create New Custom Namespaces in Kubernetes{3 Best Methods}
Create a Service to Expose Your Apps on Kubernetes(v1.16)
How to Install and Configure Kubernetes on Redhat/CentOS 7 with Best Example
Best 15 Kubectl and Kubeadm Commands
How to Check Stateful and Stateless Pods in Kubernetes Cluster