Bite-Sized Kubernetes Part 3 - ReplicaSets
Hrittik Bhattacharjee
Posted on March 7, 2023
Aye Aye!☸️
In Part 2 we looked at pods in kubernetes.
Let us now venture further with Controllers and ReplicaSets!🌊
OBJECT | DESCRIPTION |
---|---|
Pods | The lowest level, single instance of an application |
👉 ReplicaSets | The next level, manages a set of pods |
Deployments | The next level, manages a set of ReplicaSets |
Services | The next level, manages a set of deployments |
Ingress | The next level, manages a set of services |
Volumes | The next level, manages a set of ingress |
Namespaces | The next level, manages a set of volumes |
Cluster | The highest level, manages a set of namespaces |
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
:
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:
$ kubectl create -f <file name>.yml
-
See list of replication controllers:
$ 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):
$ kubectl get pods
-
-
Creating ReplicaSets:
-
Example
rs-definition.yml
:
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:
$ kubectl create -f <file name>.yml
-
See list of ReplicaSets:
$ 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):
$ kubectl get pods
-
Edit a ReplicaSet:
$ 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 thekubectl replace
command:
$ kubectl replace -f <file name>.yml
-
Or we can use the
kubectl scale
command:
$ kubectl scale --replicas=<number of replicas> -f <file name>.yml
-
Delete a ReplicaSet and all pods managed by it:
$ 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!!!🚣🏻
Posted on March 7, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.