16. Controller - Replication Controller
Table of contents
Summary
A replication controller in Kubernetes is a resource object that helps ensure a specified number of pod replicas are running at all times. It monitors the state of the pods and automatically replaces any that have failed or been terminated, thus maintaining the desired number of replicas. It also allows for scaling up or down the number of replicas as needed to handle changes in application demand.
Basic Configuration
selector: It defines pods in the replication controller range
replicas: It defines the desired number of pods that would run.
Principle of Replication Controller operation
Replication Controller definition
apiVersion: v1
kind: ReplicationController
metadata:
name: rc-nginx
spec:
replicas: 3 # Desired number of pods
selector:
app: webui #
template:
metadata:
name: nginx-pod
labels:
app: webui #
spec:
containers:
- name: nginx-container
image: nginx:1.14
Example
Basic
Enforce replication controller yaml
$ kubectl create -f re-nginx.yaml
Check pods status
$ kubectl get pods -o wide
$ kubectl get pods --show-labes
Check the replicationcontrollers status
$ kubectl get replicationcontrollers
$ kubectl get rc -o wide
Check the describe information
$ kubectl describe rc rc-nginx
Change replicas number
open rc-nginx template for modifying
$ kubectl edit re re-nginx
Change replicas number as 5
... spec: replicas: 5 # Change 3 to 5 ...
Check the pods status
$ kubectl get pods -o wide
$ kubectl get pods --show-labels
$ kubectl describe rc rc-nginx
Modify configuration by CLI command
Change replicas number to 2 by CLI command
$ kubectl scale re re-nginx --replicas=2
Chech the pods status
$ kubectl get pods -o wide
Change replicas number to 4 by CLI command again
$ kubectl scale re re-nginx --replicas=4
Chech the pods status again
$ kubectl get pods -o wide
Delete pods made by replication controller
- Delete pods
$ kubectl delete re rc-nginx
Check pods
$ kubectl get pods