#
Create, View and Destroy a Replica Set
This tutorial explains to you how to create, view and destroy a Replica Set in a Kubernetes cluster.
#
Replica Set - explanation
When you deploy pods there is no mechanism to monitor that pod. If the pod fails it will not be created again.
A Replica Set is a layer of abstraction over pods. When a Replica Set is created a Controller monitors the pods to ensure that we have the desired number of pods running on Kubernetes cluster.
Info
One Replica Set is associated with only one pod type (created from the same image).
A Replica Set has a selector
which identifies the pods it takes care of. If you have a Pod with label
matching the ReplicaSet label, ReplicaSet will take control over the pod.
For instance, if you deploy Replica Set with 3 replicas and Pod with labels matching the Replica Set labels was deployed before that, then RS will spawn only 2 Pods with the matching labels.
ReplicaSet helps perform load balancing between multiple instances of the pod.
#
Create a Replica Set in a Kubernetes cluster
Info
These examples are done on Minikube.
In order to create a Replica Set, we need to create a file as my-first-replica-set.yml which describes the Replica Set we want to create:
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-rs
spec:
replicas: 2
selector:
matchLabels:
app: app1
template:
metadata:
labels:
app: app1
spec:
containers:
- name: my-container
image: nginx:1.14.2
The following command will create a new Replica Set:
kubectl apply -f my-first-replica-set.yml
Info
kubectl
command POSTs the manifest to the API server.- Because we define what we want to have, this is considered a
Declarative
way of creating Replica Sets. - When we tell Kubernetes what exactly to do this is the
Imperative
approach. Insteadapply
we are usingcreate
orreplace
. Using the imperative approach, you’d need to use thekubectl scale
command to change the replica count of an existing deployment.
#
View Replica Set information
We can run the following command to see the result:
kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-rs 2 2 2 2m47s
Get more information on a specific Replica Set:
kubectl describe rs nginx-rs
In my case the result is:
Name: nginx-rs
Namespace: default
Selector: app=app1
Labels: <none>
Annotations: <none>
Replicas: 2 current / 2 desired
Pods Status: 2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: app=app1
Containers:
my-container:
Image: nginx:1.14.2
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 3m14s replicaset-controller Created pod: nginx-rs-v2gd2
Normal SuccessfulCreate 3m14s replicaset-controller Created pod: nginx-rs-fsf48
#
Destroy a Replica Set in a Kubernetes cluster
Delete the Replica Set created above:
kubectl delete rs nginx-rs