Replication Controllers
A replication controller (RC) is a supervisor for long-running pods. An RC will launch a specified number of pods called replicas and makes sure that they keep running, for example when a node fails or something inside of a pod, that is, in one of its containers goes wrong.
A ReplicationController ensures that a specified number of pod replicas are running at any one time. In other words, a ReplicationController makes sure that a pod or a homogeneous set of pods is always up and available.
How a ReplicationController Works
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. For example, your pods are re-created on a node after disruptive maintenance such as a kernel upgrade. For this reason, you should use a ReplicationController even if your application requires only a single pod. 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.
Running an example ReplicationController
Let’s create an RC that supervises a single replica of a pod:
cat << EOF > replication.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: rcex
spec:
replicas: 1
selector:
app: sise
template:
metadata:
name: somename
labels:
app: sise
spec:
containers:
- name: sise
image: mhausenblas/simpleservice:0.5.0
ports:
- containerPort: 9876
EOF
kubectl create -f replication.yaml
replicationcontroller "rcex" created
You can see the RC and the pod it looks after like so:
kubectl get rc
NAME DESIRED CURRENT READY AGE
rcex 1 1 1 22s
kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
rcex-pv695 1/1 Running 0 40s app=sise
- the supervised pod got a random name assigned (
rcex-pv695) - the way the RC keeps track of its pods is via the label, here
app=sise
To scale up, that is, to increase the number of replicas, do:
kubectl scale --replicas=3 rc/rcex
replicationcontroller "rcex" scaled
kubectl get pods -l app=sise
NAME READY STATUS RESTARTS AGE
rcex-4dq6w 1/1 Running 0 26s
rcex-lp6sc 1/1 Running 0 26s
rcex-pv695 1/1 Running 0 1m
Delete one of the pods
kubectl delete pod rcex-4dq6w
pod "rcex-4dq6w" deleted
kubectl get pods -l app=sise
NAME READY STATUS RESTARTS AGE
rcex-4dq6w 1/1 Terminating 0 1m
rcex-lp6sc 1/1 Running 0 1m
rcex-pv695 1/1 Running 0 3m
rcex-z9mgx 1/1 Running 0 8s
A new pod is created to maintain 3 replicas
Finally, to get rid of the RC and the pods it is supervising, use:
kubectl delete rc rcex
replicationcontroller "rcex" deleted
Note that, going forward, the RCs are called replica sets (RS), supporting set-based selectors. The RS are already in use in the context of deployments.