Kubernetes- Replication Controller

Nidhi Ashtikar
3 min readApr 18, 2024

--

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.”

  1. Desired State Specification: You specify how many pod replicas your application should have.
  2. Controller Loop: The Replication Controller continually checks the cluster’s state.
  3. Comparison with Desired State: It compares the actual pod count with the desired count.
  4. Pod Creation/Deletion: It creates or deletes pods to match the desired count.
  5. Monitoring and Reconciliation: Constantly checks and adjusts pod count to match the desired state.
  6. Event-Driven Model: It reacts to changes in the cluster’s state to maintain the desired pod count.

Replication Controllers are Equality-Based Selectors:

  1. Labels are used to identify Kubernetes resources.
  2. Resources are identified based on matching labels and their values.
  3. Labels consist of key-value pairs attached to resources.
  4. Example: An example of a selector is app: frontend, which targets pods labeled with app: frontend.
  5. 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!

Thanks for spending your valuable time learning to enhance your knowledge!

--

--

Nidhi Ashtikar
Nidhi Ashtikar

Written by Nidhi Ashtikar

Experienced AWS DevOps professional with a passion for writing insightful articles.

No responses yet