Bite-Sized Kubernetes Part 3 - ReplicaSets
Tags: kubernetes, devops, containers
Published: 2023-03-07 21:41:04

## Aye Aye!☸️

In Part 2 we looked at pods in kubernetes.
Let us now venture further with **Controllers and ReplicaSets**!🌊

<table>
<tr><th>OBJECT</th><th>DESCRIPTION</th></tr>
<tr><td>Pods</td><td>The lowest level, single instance of an application</td></tr>
<tr><td>👉 ReplicaSets</td><td>The next level, manages a set of pods</td></tr>
<tr><td>Deployments</td><td>The next level, manages a set of ReplicaSets</td></tr>
<tr><td>Services</td><td>The next level, manages a set of deployments</td></tr>
<tr><td>Ingress</td><td>The next level, manages a set of services</td></tr>
<tr><td>Volumes</td><td>The next level, manages a set of ingress</td></tr>
<tr><td>Namespaces</td><td>The next level, manages a set of volumes</td></tr>
<tr><td>Cluster</td><td>The highest level, manages a set of namespaces</td></tr>
</table>

## Controllers and ReplicaSets:
- A controller is a k8s object that is responsible for managing a set of pods.
- Replication controller: 
  - A controller that is responsible for managing a set of pods.
  - It ensures that the specified number of pods are running at any given time.
  - It is responsible for creating new pods if the number of pods falls below the specified number.
  - It is responsible for deleting pods if the number of pods exceeds the specified number.
  - It is responsible for updating the pods if the pod template is changed.
  - It helps to ensure that multiple pods are running at all times, thus guaranteeing "high availability" of the application.
  - It is also responsible for load balancing and scaling the number of pods up and down.
  - Replication controller can span across multiple nodes.
- **Replication controller is a legacy object, and is replaced by ReplicaSet**
- Creating replication controllers:
  - Example `rc-definition.yml`:
        ```yaml
        apiVersion: v1
        kind: ReplicationController
        metadata:
          name: my-app-rc
          labels:
            app: my-app
            type: my-type
        spec:
          # the template is the pod template that will be used to create the pods
          # all the contents of the pod-definition.yml file can be copied here, except the apiVersion and kind
          template:
            metadata:
              name: my-pod
              labels:
                app: my-app
                type: my-type
            spec:
              containers:
              - name: my-container
                image: my-image-name:my-image-tag
          # the number of replicas that should be running at any given time
          replicas: 5
        ```
  - Once the .yml file is created, it can be used to create a replication controller:
        ```bash
        $ kubectl create -f <file name>.yml
        ```
  - See list of replication controllers:
        ```bash
        $ kubectl get replicationcontroller
        ``` 
  - See list of pods (those created by the replication contrller will have the name of the replication controller, here `my-app-rc`, followed by a hash suffix):
        ```bash
        $ kubectl get pods  
        ``` 
- Creating ReplicaSets:
  - Example `rs-definition.yml`:
        ```yaml
        apiVersion: apps/v1 # not v1
        kind: ReplicaSet
        metadata:
          name: my-app-rs
          labels:
            app: my-app
            type: my-type
        spec:
          # the template is the pod template that will be used to create the pods
          # all the contents of the pod-definition.yml file can be copied here, except the apiVersion and kind
          template:
            metadata:
              name: my-pod
              labels:
                app: my-app
                type: my-type
            spec:
              containers:
              - name: my-container
                image: my-image-name:my-image-tag
          # the number of replicas that should be running at any given time
          replicas: 5
          # selector is needed for the ReplicaSet, not needed for replication controller
          # this identifies the pods that it is managing
          # this is because ReplicaSet can also manage pods that were created manually or already created pods 
          # i.e. those that were not a part of the ReplicaSet pod creation process
          # this ReplicaSet will only manage the pods that have the labels app=my-app and type=my-type
          selector:
            matchLabels:
              app: my-app
              type: my-type
        ```
  - Once the .yml file is created, it can be used to create a ReplicaSet:
        ```bash
        $ kubectl create -f <file name>.yml
        ``` 
  - See list of ReplicaSets:
        ```bash
        $ kubectl get replicaset
        ``` 
  - See list of pods (those created by the ReplicaSet will have the name of the ReplicaSet, here `my-app-rs`, followed by a hash suffix):
        ```bash
        $ kubectl get pods  
        ``` 
  - Edit a ReplicaSet:
        ```bash
        $ kubectl edit replicaset <replicaset name>

        # opens up vi editor
        # press i to enter insert mode, make necessary changes
        # press 'esc' to exit insert mode
        # type ':wq!' to save and exit

        # then delete all the pods managed by the ReplicaSet and then the ReplicaSet will create new pods with the updated pod template
        ```
- Scaling up or down:
  - We can scale up the number of pods by updating the `replicas` property in the .yml file and then run the `kubectl replace` command:
        ```bash
        $ kubectl replace -f <file name>.yml
        ```
  - Or we can use the `kubectl scale` command:
        ```bash
        $ kubectl scale --replicas=<number of replicas> -f <file name>.yml
        ```
  - Delete a ReplicaSet and all pods managed by it:
        ```bash
        $ kubectl delete replicaset <replicaset name>
        ```

Great going sailor, Posiedon watches over ya!🔱
Now that you understand ReplicaSets, let us move on to "Deployments" in Part 4.

**Heave Ho!!!**🚣🏻