Kubernetes- Replication Controller
Equality-Based Selectors
The Name says all, the Replication Controller is a Controller that Controls the Replication part in Kubernetes.
“Resource type is used to ensure that a specified number of pod replicas are running at any given time. It helps in maintaining the desired state of pods by creating or deleting pod replicas as necessary.”
- Desired State Specification: You specify how many pod replicas your application should have.
- Controller Loop: The Replication Controller continually checks the cluster’s state.
- Comparison with Desired State: It compares the actual pod count with the desired count.
- Pod Creation/Deletion: It creates or deletes pods to match the desired count.
- Monitoring and Reconciliation: Constantly checks and adjusts pod count to match the desired state.
- Event-Driven Model: It reacts to changes in the cluster’s state to maintain the desired pod count.
Replication Controllers are Equality-Based Selectors:
- Labels are used to identify Kubernetes resources.
- Resources are identified based on matching labels and their values.
- Labels consist of key-value pairs attached to resources.
- Example: An example of a selector is
app: frontend
, which targets pods labeled withapp: frontend
. - Use Cases: Labels are used for grouping and targeting related resources.
#rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: myapp
spec:
replicas: 3 #Specifies the desired number of pod replicas (in this case, 3).
selector: #Defines how the Replication Controller identifies which pods it manages.
app: myapp
template: # Defines the pod template used for creating new pod replicas.
metadata:
labels: #Labels applied to the pods
app: myapp
spec:
containers: #Container specification for the pods, including image and ports.
- name: myapp-container
image: nginx:latest
ports:
- containerPort: 80
# Pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: my-pod
labels:
app: my-app
spec:
containers:
- name: my-container
image: nginx:latest
ports:
- containerPort: 80
In the above YAML file, you can see the Lable = app: myapp for rc.yaml and pod.yaml. Both Lable should exactly match.
COMMANDS:
Create a Replication Controller: kubectl create -f replication-controller.yaml
List Replication Controllers:kubectl get replicationcontrollers
Describe a Replication Controller:kubectl describe replicationcontroller <controller-name>
Update a Replication Controller:kubectl replace -f replication-controller.yaml
Scale a Replication Controller:kubectl scale --replicas=5 replicationcontroller <controller-name>
Delete a Replication Controller:kubectl delete replicationcontroller <controller-name>
Get Replication Controller Pods:kubectl get pods --selector=<selector>
Expose Replication Controller as a Service:kubectl expose rc <controller-name> --port=80 --target-port=8080 --type=LoadBalancer
Rollout Status of Replication Controller:kubectl rollout status replicationcontroller <controller-name>
Rolling Update of Replication Controller:kubectl set image replicationcontroller <controller-name> <container-name>=<new-image>
Get detailed information about Replication Controller Pods:kubectl get pods --selector=<selector> -o wide
Pause and Resume a Replication Controller:kubectl rollout pause replicationcontroller <controller-name>
kubectl rollout resume replicationcontroller <controller-name>
Rollback to a Previous Revision:kubectl rollout undo replicationcontroller <controller-name>
Rollback to a Specific Revision:kubectl rollout undo replicationcontroller <controller-name> --to-revision=<revision-number
Edit a Replication Controller:kubectl edit replicationcontroller <controller-name>
Dry Run for Updates:kubectl apply -f replication-controller.yaml --dry-run=client
Export Replication Controller YAML:kubectl get replicationcontroller <controller-name> -o yaml > replication-controller-export.yaml
Delete Pods Controlled by Replication Controller:kubectl delete pods --selector=<selector>
Set Annotations on Replication Controller:kubectl annotate replicationcontroller <controller-name> key1=value1 key2=value2
Get Replication Controller Events:kubectl get events --field-selector involvedObject.kind=ReplicationController --watch
If there’s a specific topic you’re curious about, feel free to drop a personal note or comment. I’m here to help you explore whatever interests you!