Replication Controller | Replica Set |
---|---|
apiVersion: v1
kind: ReplicationController
metadata:
name: myapp-rc
type: frontend
spec:
template:
metadata:
name: myapp-pod
labels:
app:myapp
spec:
containers:
- name: nginx-container
image: nginx
replicas: 3
spec
, template is used to specify the template of the pod to be use for the replica: move everything from the pod.yaml
except for the 2 first fields (i.e: apiVersion, kind)now we have 2 spec, 2 metadata
kubectl create -f rc-definition.yaml
kubectl get replicationcontroller
kubectl get pods
–> name of pod would start with name of the replicaapiVersion: apps/v1
kind: ReplicaSet
metadata:
name: myapp- replicaset
labels:
app:myapp
type: front-end
spec:
template:
metadata:
name: myapp-pod
labels:
app:myapp
type: front-end
spec:
containers:
- name: nginx-container
image: nginx
replicas: 3
selector:
matchLabels:
type: front-end
Note: matchLabels
under selector
have to match with labels
of the pod template
But why would you have to specify what PODs fall under it, if you have provided the contents of the pod-definition file itself in the template? It’s BECAUSE, replica set can ALSO manage pods that were not created as part of the replicaset creation.
Replicaset ensures that there’s always sufficient amount of replica of pods are present on the cluster
To get the replica:
kubectl get replicaset
or kubectl get rs
replica
is specified to 3, trying to create another pod which falls under the template section of the replica definition will get that pod terminated right away.matchLabels
filter and provide the same label that we used while creating the pods. This way the replicaset knows which pods to monitor.kubectl replace -f replicaset-definition.yaml
kubectl scale --replicas=6 -f replicaset-definition.yaml
kubectl scale --replicas=6 replicaset myapp-replicaset
kubectl edit rs myapp-replicaset
https://kubernetes.io/docs/concepts/workloads/controllers/replicaset/#replicaset-as-an- horizontal-pod-autoscaler-target