CKAD Preparation - ReplicationSets
April 07, 2021
kubernetes | ckad |
Controllers (yes multiple) keep track of all the activities and monitors each of them to respond accordingly in case of change of behavior. All the controllers run inside kube-control-manager
which act as reflexes in the cluster.
Replication Controller
To prevent users from loosing connection to a running pod that crashes. So either by creating a replicate of existing POD or by redeploying the crashed POD. This nature therefore helps in addressing High Availability, Load Balancing and Scaling.
Replication Controller is one of the Master node components that spans across all nodes in the cluster.
Definition file
Start with the base definition keywords:
apiVersion |
kind |
metadata |
spec |
---|---|---|---|
v1 | ReplicationController | name labels |
template → pod-definition replicas → integer |
Note The pods created using ReplicationController definition is from its name not from the metadata under spec → template → metadata
Replica Set
Replica Set is a new recommended way of working as it originates from Replication Controller.
Additional feature of a ReplicaSet is managing Pods those are not part the current replica set. Use the selector
tag in the definition file under spec
section to add other pods.
apiVersion |
kind |
metadata |
spec |
---|---|---|---|
apps/v1 | ReplicaSet | name labels |
template → pod-definition replicas → integer selector |
Learning with example
Consider, within a K8S cluster there are already PODs, labeled login-api
, running even before there exists a ReplicaSet. Now when a replicaSet, named authentication-rs
, is created with template corresponding to same container image, as the running POD but labeled authentication-api
on the selector.
POD from ReplicaSet Perspective
The definition file for the ReplicaSet contains template of POD and selector fields. Based on this information, a replicaSet instance keep track of pods. In above scenario by default, the authentication-rs
keeps track of only the PODs labeled authentication-api
to make sure desired number of these PODs are running but not the PODs labeled login-api
. By using the command kubectl describe replicaset authentication-rs
, the Events sections will list all the PODs which are being tracked by the replicaSet. All the pods created by the replicaSet adds a metadata label ownerreferences
.
ReplicaSet from POD perspective
The pods deployed from the replicaSet can be identified by the names, as the start with the replicaSet name. In the above scenario, the pods names turnup something similar to authentication-rs-ab1c2d
. Another option to find the details are by using the command kubectl describe pod authentication-rs-ab1c2d
. The Events sections will list all the POD details, one of which will be Controlled By: ReplicaSet/authentication-rs
. Also notice that the metadata section of the POD contains ownerReferences
with the replicaSet name authentication-rs
.
Definition Files
Following below are sample Definition files to create a ReplicationController and a ReplicaSet
ReplicationController Definition File
# replicationController-definition.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: myapp-rc
labels:
app: myapp
type: frontend
spec:
template:
# Pod definition
metadata:
name: sample-app
labels:
app: api
type: frontend
spec:
containers:
- name: nginx-container
image: nginx
replicas: 5
ReplicaSet Definition File
# replicaSet-definition.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: myapp-rc
labels:
app: myapp
type: frontend
spec:
template:
# Pod definition
metadata:
name: sample-app
labels:
app: api
type: frontend
spec:
containers:
- name: nginx-container
image: nginx
replicas: 5
selector:
matchLabels:
type: frontend
Commands
- Create without yaml
kubectl create replicaSet authentication-rs --image nginx --replicas=3
- Get details of ReplicaSet
kubectl describe replicaSet authentication-rs
- To fix existing erroneous ReplicaSet
kubectl edit replicaset <name-of-replicaset>
- Scale up or down pods in the replica sets:
## Option 1
kubectl scale --replicas <number> rs/<name-of-replicaset>
## Option 2
kubectl scale replicaset --replicas=<number> <name-of-replicaset>
Debugging issue
- A erroneous ReplicaSet can still create PODs but in error state. To fix the issue there are two options:
- Option 1: Delete the ReplicaSet → Fix the YAML → Apply updated file to create new ReplicaSet
- Option 2: Using
kubectl edit
fix the YAML → Delete all the PODs which were created erroneously