Skip to content

ReplicationController

A ReplicationController ensures that a specified number of pod replicas are running at any one time. If there are too many pods, the ReplicationController terminates the extra pods. If there are too few, the ReplicationController starts more pods. Unlike manually created pods, the pods maintained by a ReplicationController are automatically replaced if they fail, are deleted, or are terminated.

A ReplicationController is similar to a process supervisor, but instead of supervising individual processes on a single node, the ReplicationController supervises multiple pods across multiple nodes.

ReplicationController is often abbreviated to “rc” or “rcs” in discussion, and as a shortcut in kubectl commands.

Replication Controllers also provide other benefits, such as the ability to scale the number of pods, and to update or delete multiple pods with a single command

The following example ReplicationController config runs three copies of the nginx web server

$ cat replicatoin.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx
spec:
  replicas: 3
  selector:
    app: nginx
  template:
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80

$ kubectl create -f ./replication.yaml
replicationcontroller "nginx" created


$ kubectl get rc
NAME      DESIRED   CURRENT   READY     AGE
nginx     3         3         0         1m

$  kubectl describe rc/nginx
Name:         nginx
Namespace:    default
Selector:     app=nginx
Labels:       app=nginx
Annotations:  <none>
Replicas:     3 current / 3 desired
Pods Status:  0 Running / 3 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx
    Port:         80/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From                    Message
  ----    ------            ----  ----                    -------
  Normal  SuccessfulCreate  44s   replication-controller  Created pod: nginx-ck7bg
  Normal  SuccessfulCreate  44s   replication-controller  Created pod: nginx-zn5w2
  Normal  SuccessfulCreate  44s   replication-controller  Created pod: nginx-928wb

$ kubectl get pods
NAME                                   READY     STATUS              RESTARTS   AGE
nginx-928wb                            0/1       ContainerCreating   0          2m
nginx-ck7bg                            0/1       ContainerCreating   0          2m
nginx-zn5w2                            0/1       ContainerCreating   0          2m

Note

1
The .spec.selector field is a label selector. A ReplicationController manages all the pods with labels that match the selector. It does not distinguish between pods that it created or deleted and pods that another person or process created or deleted. This allows the ReplicationController to be replaced without affecting the running pods.

Deleting Replicatoin Controllers

To delete a ReplicationController and all its pods, use kubectl delete. Kubectl will scale the ReplicationController to zero and wait for it to delete each pod before deleting the ReplicationController itself

You can delete a ReplicationController without affecting any of its pods. Using kubectl, specify the --cascade=false option to kubectl delete.

To update pods to a new spec in a controlled way, use a rolling update.

Pods may be removed from a ReplicationController’s target set by changing their labels. This technique may be used to remove pods from service for debugging, data recovery, etc. Pods that are removed in this way will be replaced automatically (assuming that the number of replicas is not also changed).

$ kubectl delete rc nginx
replicationcontroller "nginx" deleted

$ kubectl get pods
NAME                                   READY     STATUS        RESTARTS   AGE
nginx-928wb                            0/1       Terminating   0          38m
nginx-ck7bg                            0/1       Terminating   0          38m
nginx-zn5w2                            0/1       Terminating   0          38m