Skip to main content Link Menu Expand (external link) Document Search Copy Copied

16. Controller - Replication Controller

Table of contents
  1. 16. Controller - Replication Controller
    1. Summary
    2. Basic Configuration
    3. Principle of Replication Controller operation
    4. Replication Controller definition
    5. Example
      1. Basic
      2. Change replicas number
      3. Modify configuration by CLI command
      4. Delete pods made by replication controller

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

  1. Enforce replication controller yaml

     $ kubectl create -f re-nginx.yaml  
    

    1

  2. Check pods status

     $ kubectl get pods -o wide  
    

    2

     $ kubectl get pods --show-labes
    

    5 8

  3. Check the replicationcontrollers status

     $ kubectl get replicationcontrollers
    

    3 7

     $ kubectl get rc -o wide
    

    4

  4. Check the describe information

     $ kubectl describe rc rc-nginx
    

    6

Change replicas number

  1. open rc-nginx template for modifying

     $ kubectl edit re re-nginx
    

    10 9

  2. Change replicas number as 5

     ...
     spec:
       replicas: 5  # Change 3 to 5
     ...
    

    11

  3. Check the pods status

     $ kubectl get pods -o wide  
    

    12

     $ kubectl get pods --show-labels
    

    13

     $ kubectl describe rc rc-nginx
    

    14

Modify configuration by CLI command

  1. Change replicas number to 2 by CLI command

     $ kubectl scale re re-nginx --replicas=2
    

    15

  2. Chech the pods status

     $ kubectl get pods -o wide
    

    16

  3. Change replicas number to 4 by CLI command again

     $ kubectl scale re re-nginx --replicas=4
    

    17

  4. Chech the pods status again

     $ kubectl get pods -o wide
    

    18

Delete pods made by replication controller

  1. Delete pods
     $ kubectl delete re rc-nginx
    

    19

  2. Check pods

     $ kubectl get pods
    

    19


Table of contents