# Replication Controllers

### What are ReplicationControllers? <a href="#replicationcontrollers-la-gi-2" id="replicationcontrollers-la-gi-2"></a>

ReplicationControllers is a resource that will create and manage pods, and make sure the number of pods it manages remains unchanged and kept running. ReplicationControllers will create the number of pods equal to the number we specify in the replicas attribute and manage pods through pod labels.

<figure><img src="https://images.viblo.asia/18bf20c6-7160-4987-9fae-66ec6edc6d33.png" alt=""><figcaption></figcaption></figure>

#### Why should we use ReplicationControllers to run pods? <a href="#tai-sao-ta-nen-dung-replicationcontrollers-de-chay-pod-3" id="tai-sao-ta-nen-dung-replicationcontrollers-de-chay-pod-3"></a>

We already know that pod will monitor the container and automatically restart the container when it fails

<figure><img src="https://images.viblo.asia/5c1b6cdb-626b-41d3-ad56-3d0a0df59651.png" alt=""><figcaption></figcaption></figure>

So what happens in case our entire worker node fails? pod it will no longer be able to run, and our application will downtime for users

<figure><img src="https://images.viblo.asia/cfb88987-43ef-49d7-9835-050482618b2b.png" alt=""><figcaption></figcaption></figure>

If we run cluster with more than 1 worker node, RC will help us solve this problem. Because RC will make sure that the number of pods it creates does not change, for example when we create an RC with the number of replicas = 1, RC will create 1 pod and monitor it, when a worker node dies, If the RC manager's pod is in that worker node, then the RC will discover that the number of its pods is 0, and it will create a pod in another worker node to regain the number. first

<figure><img src="https://images.viblo.asia/e47d1df7-93cb-48fd-abd1-775f200665c1.png" alt=""><figcaption></figcaption></figure>

Below is an illustration of how RC works

<figure><img src="https://images.viblo.asia/365e0eb8-8cad-430d-aeaa-efd87db31c61.png" alt=""><figcaption></figcaption></figure>

Using ReplicationControllers to run pods will help keep our application as available as possible. In addition, we can increase the performance of the application by specifying the number of replicas in RC so that RC creates multiple pods running the same version of the application.

For example, if we have a webservice, if we only deploy one pod to run the application, we will only have 1 container to handle user requests, but if we use RC and specify replicas = 3, we will have 3 pods running 3. application container, and the user's request will be sent to 1 of these 3 pods, helping our processing process increase 3 times.

<figure><img src="https://images.viblo.asia/a6db7abf-1452-4463-8345-43f9b640b307.png" alt=""><figcaption></figcaption></figure>

### Create a ReplicationControllers <a href="#tao-mot-replicationcontrollers-4" id="tao-mot-replicationcontrollers-4"></a>

So now that the theory part is over, let's move on to the practical part. Create a file named hello-rc.yaml and copy the following config into it:

```yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: hello-rc
spec:
  replicas: 2 # number of the pod
  selector: # The pod selector determining what pods the RC is operating on
    app: hello-kube # label value
  template: # pod template
    metadata:
      labels:
        app: hello-kube # label value
    spec:
      containers:
      - image: 080196/hello-kube # image used to run container
        name: hello-kube # name of the container
        ports:
          - containerPort: 3000 # pod of the container
```

The structure of an RC config file will include 3 main parts as follows:

* label selector: will specify which pods will be monitored by RC
* replica count: number of pods to be created
* pod template: config of the pod to be created

<figure><img src="https://images.viblo.asia/3604e4cd-1a95-4b60-8266-1b592272533e.png" alt=""><figcaption></figcaption></figure>

Now let's create RC

`kubectl apply -f hello-rc.yaml`

<figure><img src="https://images.viblo.asia/a61bd091-6bd2-4260-8fef-7dbe7d8772f5.png" alt=""><figcaption></figcaption></figure>

Check if our rc has run successfully or not

`kubectl get rc`

<figure><img src="https://images.viblo.asia/e84ae2b2-dff6-4f59-8349-a59ada5257ff.png" alt=""><figcaption></figcaption></figure>

If the number in the READY column is equal to the DESIRED number, then we have run RC successfully. Now we check whether the number of pods created by RC matches the number specified in the replicas as in theory.

`kubectl get pod`

<figure><img src="https://images.viblo.asia/4ff9929a-2d9b-4013-ab65-ad15a4c5e102.png" alt=""><figcaption></figcaption></figure>

Here, the name of your pod displayed will be different from the picture. If you see there are 2 pods, the number of pods created by RC is correct, the name of the pod created by RC will be in the style **`<replicationcontroller name>-<random>`**. Now we will try to delete one pod to see if RC will recreate another pod for us as in theory. Remember to specify the correct name of your pod

`kubectl delete pod hello-rc-c6l8k`

<figure><img src="https://images.viblo.asia/bc32dbc1-d757-4f26-85f3-b20c0e854551.png" alt=""><figcaption></figcaption></figure>

Open another terminal window and type the command

`kubectl get pod`

<figure><img src="https://images.viblo.asia/e9f36658-5dc9-4a15-9cae-d397fab7b33d.png" alt=""><figcaption></figcaption></figure>

You will see that an old pod is being deleted, and at the same time, a new pod will be created by RC, here my new pod is called **hello-rc-mftfd** . RC's activities are illustrated as follows

<figure><img src="https://images.viblo.asia/1a6dd461-ebf1-46de-a42c-b580531937d6.png" alt=""><figcaption></figcaption></figure>

#### Change pod template <a href="#thay-doi-template-cua-pod-5" id="thay-doi-template-cua-pod-5"></a>

You can change the pod template and update the RC, but it will not apply to existing pods. If you want your pod to update the new template, you must delete all pods for RC to create a new pod, or delete RC. and recreate

<figure><img src="https://images.viblo.asia/52e144c9-0c89-47f9-8a64-618556e02cb4.png" alt=""><figcaption></figcaption></figure>

So we have run RC, now let's delete it. To delete RC, you use the command

`kubectl delete rc hello-rc`

When you delete RC, the pods it manages will also be deleted

<figure><img src="https://images.viblo.asia/aa139690-dc3f-4357-8f8d-31cea4b59570.png" alt=""><figcaption></figcaption></figure>

As you can see, ReplicationController is a very useful resource for us to deploy pods

### Use ReplicaSets instead of RC <a href="#su-dung-replicasets-thay-the-rc-6" id="su-dung-replicasets-thay-the-rc-6"></a>

This is a similar resource to RC, but it is a newer version of RC and will be used to replace RC. We will use ReplicaSets (RS) to deploy pods instead of using RC. In this article, I will talk about RC first so that we can understand its origin, so that even if we are asked questions in the interview, we will still know the answer .

Now we will try to create an RS guy, its configuration is still the same as RC, just a few different parts. Create a file named hello-rs.yaml, copy the following config into it:

```yaml
apiVersion: apps/v1 # change version API
kind: ReplicaSet # change resource name
metadata:
  name: hello-rs
spec:
  replicas: 2
  selector:
    matchLabels: # change here 
      app: hello-kube
  template:
    metadata:
      labels:
        app: hello-kube
    spec:
      containers:
      - image: 080196/hello-kube
        name: hello-kube
        ports:
          - containerPort: 3000
```

`kubectl apply -f hello-rs.yaml`

Check if our RS runs successfully or not

`kubectl get rs`

<figure><img src="https://images.viblo.asia/00125b69-1c3b-40d4-b7f2-986bf65d12dd.png" alt=""><figcaption></figcaption></figure>

If there are 2 pods created, we have run RS successfully. To delete RS, we use the command

`kubectl delete rs hello-rs`

<figure><img src="https://images.viblo.asia/037587c2-0b14-4b68-8b83-1dc31468304e.png" alt=""><figcaption></figcaption></figure>

#### Compare ReplicaSets and ReplicationController <a href="#so-sanh-replicasets-va-replicationcontroller-7" id="so-sanh-replicasets-va-replicationcontroller-7"></a>

RS and RC will operate similarly. But RS is more flexible in the label selector part, while the RC label selector can only select pods that are completely similar to the label it specifies, the RS will allow the use of some expressions or matching to select the pod it manages.

For example, RC cannot match a pod that has env=production and env=testing at the same time, while RS can, by specifying a label selector like **env=** \* . Additionally, we can use **operators with the matchExpressions** attribute as follows:

```yaml
selector:
  matchExpressions:
    - key: app
      operator: In
      values:
        - kubia
```

There are 4 basic operators: In, NotIn, Exists, DoesNotExist

### Use DaemonSets to run exactly one pod on one worker node <a href="#su-dung-daemonsets-de-chay-chinh-xac-mot-pod-tren-mot-worker-node-8" id="su-dung-daemonsets-de-chay-chinh-xac-mot-pod-tren-mot-worker-node-8"></a>

This is another kube resource, like RS, it will also monitor and manage pods by lables. But for RS, pods can be deployed on any node, and in one node, several pods can be run. And this DaemonSets guy will deploy to each node a unique pod, and it will definitely have how many pods there are, it will not have the replicas attribute.

<figure><img src="https://images.viblo.asia/f3e48546-9b72-41bf-aa74-79cc354f06e6.png" alt=""><figcaption></figcaption></figure>

This DaemonSets application will be used for logging and monitoring. At this point, we will only want one monitoring pod at each node. And we can also type a label into a woker node using the command

`kubectl label nodes <your-node-name> disk=ssd`

Then we can specify the DaemonSets config in the nodeSelector column with disk=ssd. Only deploy the pod on the node with an SSD drive. Here is an example config

```yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: ssd-monitor
spec:
  selector:
    matchLabels:
      app: ssd-monitor
  template:
    metadata:
      labels:
        app: ssd-monitor
    spec:
      nodeSelector:
        disk: ssd
      containers:
        - name: main
          image: luksa/ssd-monitor
```

<figure><img src="https://images.viblo.asia/7fc7135d-8a28-4375-8741-5e789b4fcbd4.png" alt=""><figcaption></figcaption></figure>

Here we will not practice creating DaemonSets because we need an environment with many worker nodes to demo this resource. You can read more about DaemonSets [here](https://kubernetes.io/docs/concepts/workloads/controllers/daemonset/)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://huy312100.gitbook.io/software-development/devops/kubernetes/replication-controllers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
