What if you want, to scale your application, in order to enable higher request processing capacity. You would scale your application, probably by copy pasting the configuration of a Pod but doing so is error-prone and too manual. Technically, what you want is a replicated set of Pods and that’s what a Replica Set is. It is a Pod manager which works at a cluster level, ensuring that the number of Replica Sets which you have specified, that number of Pods are healthy and running at all times. It is perfectly valid to mention Replica set as 0, in which case there is no pod running.
Though Replica Sets create and manage Pods, they do not own the pods they create. If replica sets owned the Pods they created, then the only way to replicate the Pod after deleting it, would be via Replica Set. That's why the Replica Sets and Pods are decoupled.
Reconciliation Loops
Let’s say, the replica set which we want to maintain is mentioned as 2. Kubernetes will make sure that 2 pods are running at any given point of time. If we delete one of the pods, kubernetes will again create 1 more pod, to maintain the desired state. The actual act of managing the replicated pods is an example of a reconciliation loop.
The concept behind a reconciliation loop is the notion of actual state versus the desired state. The desired state is what you mention as code and thus the term Infrastructure as code. The loop is constantly running, observing the actual state and taking action to maintain the desired state.
ReplicaSets and Label Selectors
Replica Sets manage the pods using the label selectors. Let’s say, the Replica Set is managing 2 pods and one of the pods misbehaves but somehow the health check for this pod gets passed. One way to deal with this situation is to kill the pod which is misbehaving, but then the only way to find the root cause will be via logs. Another way to deal with this is to change the label of the pod, so that the replica set will find only one pod which is running and it will spawn a new Pod. Now since the faulty pod is still running, it can be debugged far easily.
To find the set of Pods for a ReplicaSet, below command can be used.
kubectl get pods -l app=my-application,version=1
ReplicaSet Spec
All the objects in Kubernetes are defined using a specification and the same is the case with ReplicaSet.
kind: ReplicaSet
metadata:
labels:
app: my-application
version: "1"
name: my-application
spec:
replicas: 1
selector:
matchLabels:
app: my-application
version: "1"
template:
metadata:
labels:
version: "1"
app: my-application
spec:
containers:
- name: my-application
image: "some-repo/some-image:latest"
The above configuration, will ensure only 1 Pod is running at any given point of time. ReplicaSet can be created by using the below command, this uses the above config file.
kubectl apply -f my-application-rs.yml
As usual with any Kubernetes object, if you need to find the details about a ReplicaSet, describe command can be used to get all the information about it.
kubectl describe rs my-application
Summary
Use ReplicaSets for any pod, even if it is a single Pod!