This small article is of creating ReplicaSet using Terraform

Kubernetes (K8s) is an open-source system for automating deployment, scaling, and management of containerized applications.

What is ReplicaSet ?

A ReplicaSet’s purpose is to maintain a stable set of replica Pods running at any given time. As such, it is often used to guarantee the availability of a specified number of identical Pods.

To know in detail,please refer to this link :-

What is Terraform ?

Use Infrastructure as Code to provision and manage any cloud, infrastructure, or service. Terraform users define infrastructure in a simple, human-readable configuration language called HCL (HashiCorp Configuration Language). Users can write unique HCL configuration files or borrow existing templates from the public module registry.

Why Terraform? While you could use kubectl or similar CLI-based tools mapped to API calls to manage all Kubernetes resources described in YAML files, orchestration with Terraform presents a few benefits. Use the same configuration language to provision the Kubernetes infrastructure and to deploy applications into it.

I have created functioning/working of ReplicaSet using Terraform code below.

I have used resource “replication controller”, but according to the used case, we can also use resource “deployment” .

Autoscaling Replica set

First resource is provider, which is to be used for initialization for any terraform file and then use command <terraform init> to install plugins,which is sort of a necessary part.

The above resource is for auto-scaling and create replica set which consists of the following contents :-

  • ReplicaSets always work on either SELECTORS or OWNER-REFERENCE-FIELD of pods. If owner.reference.feild is absent or not set then replicaset comes into action with selector field.
  • → Selector field takes label as their reference to acquire pods within it, so that is why , I have written LABELS in the above code.
  • — “env, dev, dc, In” are environment, development, data_center, India respectively (like tags ).
  • Metadata is data of data ,we give information to metadata, when we launch something
  • → Name of our ReplicaSet is “replicaset”.
  • Spec block is Specification of what we are launching.
  • max_replicas are maximum replicas to be auto-scaled.
  • min_replicas are minimum replicas to be auto-scaled.
  • Lastly, we have to tell about the reference of scaling i.e “scale_target_ref” . In kind variable ,I have told about my resource which will be creating pods with number of replicas i.e “replication-controller”.
Resource — Replication controller part 2(a)
  • The above resource is “replication contoller” , which controls/manages the replicas. A Replication Controller ensures that a specified number of pod “replicas” are running at any one time. In other words, a Replication Controller makes sure that a pod or homogeneous set of pods are always up and available. If there are too many pods, it will kill some. If there are too few, the Replication Controller will start more replicas.

Description of objects, blocks and variables are as follows :-

  • → In metadata block, we tell about name of our replication controller and I have simply given name as “replication-controller”. In this block , we can also provide labels ,but it is optional.
  • → The spec block is again about specification of desired working of a pod. The SELECTOR discussed above,now comes into play i.e if all labels(tags) specified in this block are same as that of pod created, then the pods will be acquired by ReplicaSet and will match the no of replicas mentioned in the spec block and if the number exceeds, then ReplicaSet will terminate that pod. (In my case replicas is set to 3, so 4 or more replicas will not be allowed to run with same labels).

“This functioning of ReplicaSet only works when all labels match with the labels specified in the Selector Block, In other cases like no tags match, 1 or 2 match, ReplicaSet will allow pod to create and run.”

Below are the screenshots which will be proving the above statement :-

TEST-1 => This is the code for pod written in YAML File, I will be creating this pod by changing the label.
TEST-2 => As you can see ,my 3 replicas, Replication controller , ReplicaSet are running and observe Labels also

→ I will be now creating pod using code (as shown in TEST-1).

TEST-3
TEST-4
TEST-5
TEST-6
TEST-7

Now , I think readers can get the point, I want to explain.

Resource — Replication controller part 2(b)
  • Template block is used when a replica shuts down or terminates due to any reason such as excessive traffic at a time or any other technical/non-technical issue. Again metadata and spec block are used to launch desired replica. In the spec block, we are specifying container with image name which will be pulled either locally or from docker hub and a name is specified to the container.

→ For Guidelines of using Terraform code , visit the link mentioned below for docs :-

→ For viewing Terraform code and code for pod creation , goto Github repositories mentioned below :-

“I practiced and gained knowledge of this topic “ReplicaSet” under the mentorship of Mr. VIMAL DAGA Sir during the Hybrid Multi Cloud Training by Linux World India.”

I hope this article is Informative and Explanatory. Hope you like it !!!

For any suggestions or if any reader find any flaw in this article, please email me to “akhileshjain9221@gmail.com”

Thank You Readers!!!

I am a student and persuing under graduation in computer science and engineering.