Start Preparation Smartly

We have the collection to start prepartion smartly.

Start Assessment

Learn Kubernetes | K8s Tutorial for Beginners

  DevOps Tech Hub

116 Followers

Deploy Replication Controller in Kubernetes (K8s) cluster


 What is Replication Controller?

Replication Controller ensures that the specified number of pod replicas are running at any one time within the kubernetes cluster. Replication Controller controls the creating of PODs  automatically if the number of running POD replicas not matching the specified number.

When Deployed Replication Controller it  takes care of  creating specified number if POD replicas automatically as per the Manifest definitions.

Replication Controller

Replication Controller is also more convenient way to scale out and scale in number of POD replicas within the K8s cluster.

Create manifest file for Replication Controller

Create 'createrd.yaml' file with below manifest definitions

#Replication Controller
apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-rc  
spec:
  replicas: 3
  selector:
    app: nginx-app

  template:
    metadata:
      name: nginx-pod
      labels:
        app: nginx-app
        env: sandbox
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80

Deploy Replication Controller

[root@ ~]$kubectl apply -f createrc.yaml
replicationcontroller/nginx-rc created
[root@ ~]$

Get running PODs in the cluster

[root@ ~]$kubectl get pods
NAME             READY   STATUS    RESTARTS   AGE
nginx-rc-497w9   1/1     Running   0          26s
nginx-rc-6h7vp   1/1     Running   0          26s
nginx-rc-lbv5c   1/1     Running   0          26s
[root@ ~]$

Get running Replication Controllers in the cluster

root@ ~]$kubectl get rc
NAME       DESIRED   CURRENT   READY   AGE
nginx-rc   3         3         3       49s
root@ ~]$kubectl get rc nginx-rc
NAME       DESIRED   CURRENT   READY   AGE
nginx-rc   3         3         3       72s
[root@ ~]$

Describe Replication Controllers properties with events

[root@ ~]$kubectl describe rc nginx-rc
Name:         nginx-rc
Namespace:    default
Selector:     app=nginx-app
Labels:       app=nginx-app
              env=sandbox
Annotations:  Replicas:  3 current / 3 desired
Pods Status:  3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=nginx-app
           env=sandbox
  Containers:
   nginx:
    Image:        nginx
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From                    Message
  ----    ------            ----  ----                    -------
  Normal  SuccessfulCreate  2m5s  replication-controller  Created pod: nginx-rc-lbv5c
  Normal  SuccessfulCreate  2m5s  replication-controller  Created pod: nginx-rc-497w9
  Normal  SuccessfulCreate  2m5s  replication-controller  Created pod: nginx-rc-6h7vp
[root@ ~]$

Scale Out number of PODs managing by Replication Controller

[root@ ~]$kubectl scale rc nginx-rc --replicas=5
replicationcontroller/nginx-rc scaled
[root@ ~]$kubectl get pods
NAME             READY   STATUS    RESTARTS   AGE
nginx-rc-497w9   1/1     Running   0          7m9s
nginx-rc-6fp9q   1/1     Running   0          14s
nginx-rc-6h7vp   1/1     Running   0          7m9s
nginx-rc-b4p6w   1/1     Running   0          14s
nginx-rc-lbv5c   1/1     Running   0          7m9s
[root@ ~]$

Scale In number of PODs managing by Replication Controller

[root@ ~]$kubectl scale rc nginx-rc --replicas=3
replicationcontroller/nginx-rc scaled
[root@ ~]$kubectl get pods
NAME             READY   STATUS        RESTARTS   AGE
nginx-rc-497w9   1/1     Running       0          7m40s
nginx-rc-6h7vp   1/1     Running       0          7m40s
nginx-rc-b4p6w   0/1     Terminating   0          45s
nginx-rc-lbv5c   1/1     Running       0          7m40s
[root@ ~]$
[root@ ~]$kubectl get pods
NAME             READY   STATUS    RESTARTS   AGE
nginx-rc-497w9   1/1     Running   0          7m54s
nginx-rc-6h7vp   1/1     Running   0          7m54s
nginx-rc-lbv5c   1/1     Running   0          7m54s
[root@ ~]$

Delete Replication Controller

[root@ ~]$kubectl delete rc nginx-rc
replicationcontroller "nginx-rc" deleted
[root@ ~]$kubectl get rc
No resources found in default namespace.
[root@ ~]$kubectl get pods
No resources found in default namespace.
[root@ ~]$



Previous Next