Skip to content

Commit 8e21e2f

Browse files
committed
Added introduction about Replication Controller
Signed-off-by: knrt10 <[email protected]>
1 parent f63e660 commit 8e21e2f

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

Diff for: kubia-rc.yaml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: v1
2+
kind: ReplicationController
3+
metadata:
4+
name: kubia
5+
spec:
6+
replicas: 3
7+
selector:
8+
app: kubia
9+
template:
10+
metadata:
11+
labels:
12+
app: kubia
13+
spec:
14+
containers:
15+
- name: kubia
16+
image: knrt10/kubia
17+
ports:
18+
- containerPort: 8080

Diff for: readme.md

+95
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ This is just a simple demonstration to get a basic understanding of how kubernet
8080
- [Seeing a liveness probe in action](#seeing-a-liveness-probe-in-action)
8181
- [Configuring additional properties of liveness probe](#configuring-additional-properties-of-liveness-probe)
8282
- [Creating effective liveness probe](#creating-effective-liveness-probe)
83+
- [Introducing ReplicationControllers](#introducing-replicationcontrollers)
84+
- [The operation of a ReplicationController](#the-operation-of-a-replicationcontroller)
85+
- [Introducing the controller reconciliation loop](#introducing-the-controller-reconciliation-loop)
86+
- [Creating a ReplicationController](#creating-a-replicationcontroller)
8387

8488
4. [Todo](#todo)
8589

@@ -1022,6 +1026,97 @@ But if the node itself crashes, it’s the Control Plane that must create replac
10221026

10231027
To make sure your app is restarted on another node, you need to have the pod managed by a ReplicationController or similar mechanism later on in this readme.
10241028

1029+
### Introducing ReplicationControllers
1030+
1031+
A Replication Controller(RC) is a Kubernetes resource that ensures its pods are always kept running. If a pod disappers for any reasons like in case of node disappearing from the cluster or because the pod was evicted from the node, the RC notes the pod missing and creates a replacement pod. Please refer to the image below
1032+
1033+
![RC](https://user-images.githubusercontent.com/24803604/70711851-6c2c2e00-1d08-11ea-90cd-1feba139645d.png)
1034+
1035+
> When a node fails, only pods backed by a ReplicationController are recreated.
1036+
1037+
The RC in the figure only manage only a single pod but in general they are meant to create and manage a mutiple copies (replicas) of a pod. That's where RC got their name from.
1038+
1039+
#### The operation of a ReplicationController
1040+
1041+
The RC contantly monitors the list of running pods and makes sure the actual number of pods of a "type" always matches the desires number. If too few such pods are running, it creates new replica from pod template. If too much pods are running, it remove the excess replicas.
1042+
1043+
You might be wondering how there can be more than the desired number of replicas. This can happen for a few reasons:
1044+
1045+
- Someone creates a pod of the same type manually.
1046+
- Someone changes an existing pod’s “type.”
1047+
- Someone decreases the desired number of pods, and so on.
1048+
1049+
I’ve used the term pod “type” a few times. But no such thing exists. Replication Controllers don’t operate on pod types, but on sets of pods that match a certain label selector, which I have told you previously.
1050+
1051+
#### Introducing the controller reconciliation loop
1052+
1053+
A ReplicationController’s job is to make sure that an exact number of pods always matches its label selector. If it doesn’t, the ReplicationController takes the appropriate action to reconcile the actual with the desired number.
1054+
1055+
![controller-reconcilition-loop](https://user-images.githubusercontent.com/24803604/70713322-c5e22780-1d0b-11ea-9334-3d9213e86d3c.png)
1056+
1057+
A ReplicationController has three essential parts:
1058+
1059+
- A label selector, which determines what pods are in the ReplicationController’s scope
1060+
- A replica count, which specifies the desired number of pods that should be running
1061+
- A pod template, which is used when creating new pod replicas
1062+
1063+
![RC three parts](https://user-images.githubusercontent.com/24803604/70715546-8964fa80-1d10-11ea-8e33-59f1a5a36698.png)
1064+
1065+
> The three key parts of a
1066+
ReplicationController (pod selector,
1067+
replica count, and pod template)
1068+
1069+
A ReplicationController’s replica count, the label selector, and even the pod template can all be modified at any time, but only changes to the replica count affect existing pods.
1070+
1071+
Changes to the label selector and the pod template have no effect on existing pods. Changing the label selector makes the existing pods fall out of the scope of the ReplicationController, so the controller stops caring about them. ReplicationControllers also don’t care about the actual “contents” of its pods (the container images, environment variables, and other things) after they create the pod. The template therefore only affects new pods created by this ReplicationController. You can think of it as a cookie cutter for cutting out new pods.
1072+
1073+
**BENEFITS**
1074+
1075+
- It makes sure a pod (or multiple pod replicas) is always running by starting a
1076+
new pod when an existing one goes missing.
1077+
- When a cluster node fails, it creates replacement replicas for all the pods that
1078+
were running on the failed node (those that were under the Replication-
1079+
Controller’s control).
1080+
- It enables easy horizontal scaling of pods—both manual and automatic
1081+
1082+
**Note**:- A pod instance is never relocated to another node. Instead, the ReplicationController creates a completely new pod instance that has no relation to the instance it’s replacing.
1083+
1084+
#### Creating a ReplicationController
1085+
1086+
You’re going to create a file called **kubia-rc.yaml** (you can create it in any directory you want), or copy from this repo, where you’ll find the file with filename [kubia-rc.yaml](https://github.com/knrt10/kubernetes-basicLearning/blob/master/kubia-rc.yaml). The following listing shows the entire contents of the file.
1087+
1088+
```yaml
1089+
apiVersion: v1
1090+
kind: ReplicationController
1091+
metadata:
1092+
name: kubia
1093+
spec:
1094+
replicas: 3
1095+
selector:
1096+
app: kubia
1097+
template:
1098+
metadata:
1099+
labels:
1100+
app: kubia
1101+
spec:
1102+
containers:
1103+
- name: kubia
1104+
image: knrt10/kubia
1105+
ports:
1106+
- containerPort: 8080
1107+
```
1108+
1109+
When you post the file to the API server, Kubernetes creates a new ReplicationController named `kubia`, which makes sure three pod instances always match the label selector `app=kubia`. When there aren’t enough pods, new pods will be created from the provided pod template. The contents of the template are almost identical to pod defination we created before.
1110+
1111+
The pod labels in the template must obviously match the label selector of the ReplicationController; otherwise the controller would create new pods indefinitely, because spinning up a new pod wouldn’t bring the actual replica count any closer to the desired number of replicas. To prevent such scenarios, the API server verifies the ReplicationController definition and will not accept it if it’s misconfigured.
1112+
1113+
Not specifying the selector at all is also an option. In that case, it will be configured automatically from the labels in the pod template.
1114+
1115+
To create the ReplicationController, use the `kubectl create` command, which you already know:
1116+
1117+
`kubectl create -f kubia-rc.yaml`
1118+
> replicationcontroller "kubia" created
1119+
10251120
## Todo
10261121

10271122
- [ ] Write more about pods

0 commit comments

Comments
 (0)