Kubernetes ReplicaSets
Hello there! In this blog post, we will explore Kubernetes ReplicaSets - what they are, how we can create the manifest file, and how we can deploy it in our own Kubernetes cluster. If you have not set up your own Kubernetes cluster yet, check out my blog post!
What is a ReplicaSet?
The main purpose of a ReplicaSet is to maintain a given number of replicas of pods at any given time in the cluster. A ReplicaSet guarantees the number of pods that will be available at any given time.
Creating a ReplicaSet Manifest
A sample of the ReplicaSet manifest file is provided below.
# Filename: nginx-replicaset.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-replicaset
labels:
app: nginx
tier: frontend
spec:
# modify replicas according to your case
replicas: 3
selector:
matchLabels:
tier: frontend
template:
metadata:
labels:
tier: frontend
spec:
containers:
- name: nginx-container
image: nginx:1.14.2
ports:
- containerPort: 80
Keep in mind:
As with all other Kubernetes API objects, a ReplicaSet needs theapiVersion
,kind
, andmetadata
fields. For ReplicaSets, the kind is always a ReplicaSet. In Kubernetes 1.9 the API versionapps/v1
on the ReplicaSet kind is the current version and is enabled by default. The API versionapps/v1beta2
is deprecated.
The name of a ReplicaSet object must be a valid DNS subdomain name.
Pod Template
The.spec.template
is a pod template which is also required to have labels in place. In ournginx-replicaset.yaml
example we had onelabel: tier: frontend
. Be careful not to overlap with the selectors of other controllers, lest they try to adopt this Pod.
For the template's restart policy field,
.spec.template.spec.restartPolicy
, the only allowed value isAlways
, which is the default.
Pod Selector
The.spec.selector field
is a label selector. As discussed earlier these are the labels used to identify potential Pods to acquire. In ournginx-replicaset.yaml
example, the selector was:matchLabels: tier: frontend
In the ReplicaSet,
.spec.template.metadata.labels
must matchspec.selector
, or it will be rejected by the API.
Working with ReplicaSets
In order to apply the replicaset on to the Kubernetes Cluster, use the following command after placing the manifest file in the directory of your choice.kubectl apply -f nginx-replicaset.yaml
In order to view the nginx ReplicaSet on the cluster, use the following command.kubectl get rs
If you want to see details of the replicaset that you created, use the following command.kubectl describe rs/nginx-replicaset
You can check the pods that are brought up by the ReplicaSet using the following command.kubectl get pods -o wide
If you want to verify the ownership status of a pod with your ReplicaSet, then use the following commands.
meraki@k8s-node1:~/replicaset$ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-replicaset-ghz98 1/1 Running 0 11m 10.42.0.1 k8s-node3 <none> <none>
nginx-replicaset-gpsg5 1/1 Running 0 11m 10.44.0.1 k8s-node2 <none> <none>
nginx-replicaset-j42kx 1/1 Running 0 11m 10.36.0.1 k8s-node4 <none> <none>
mukul@k8s-node1:~/replicaset$ kubectl get pods nginx-replicaset-ghz98 -o json
In the output of this command, you will see this information:
"ownerReferences": [
{
"apiVersion": "apps/v1",
"blockOwnerDeletion": true,
"controller": true,
"kind": "ReplicaSet",
"name": "nginx-replicaset",
"uid": "565f6a57-37cc-4dd5-89d1-c13ee5be8cfb"
}
Important to remember
If you are creating additional pods that are not part of the ReplicaSet, then make sure that the label does not match that of the selector for the ReplicaSet. If it does, the ReplicaSet controller will take over these pods and immediately kill them if the required number of replicas are met. You can find more information on this here.
In order to delete a ReplicaSet, use the following command.kubectl delete -f nginx-replicaset.yaml
Note:
If you want to delete the ReplicaSet controller without affecting the pods that it was managing, then use kubectl delete with the --cascade=orphan option.
And now, you know how to create and deploy replica sets in Kubernetes.
Hope this helps and you learned something new! ☺️