Programming On Mars Logo
  • Home
  • Articles
  • Labs
Programming On Mars Logo
  • Home
  • Articles
  • Labs

  • Andre Lucas
  • Fri Aug 16 2024

Managing Pods using ReplicaSet

ReplicaSet Header

To follow this article you can get the files on - https://github.com/ProgrammingOnMars/kubernetes-for-dev

When a pod is created, this pod isn't managing for anything. Thus, a pod can be stopped for any reason, perhaps error application, or network errors. So, we need a mechanism to keep the application available.

When a Pod is stopped for any reason your application will not be available, but you must not want it. You need to a mechanism to restart or create others pods to keep the application available again.

Another reason is when you want to create a loading balancing to distribute the workloads of your application.

How a ReplicaSet works

The ReplicaSet manages the pod's state, their quantity to ensure loading balancing between nodes, and the minimum pod's quantity.

Let's create a ReplicaSet

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: programmingonmars-replicaset
  labels:
    app: programmingonmars
spec:
  replicas: 3
  selector:
    matchLabels:
      app: programmingonmars
  template:
    metadata:
      labels:
        app: programmingonmars
    spec:
      containers:
      - name: programmingonmars-container
        image: andrelucastic/demo:latest
        ports:
        - containerPort: 8080

The number of replicas indicates the number of Pods that should be maintaining

The template is the data (characteristics) of the new Pods. When a ReplicaSet needs to create a new Pod it uses its template

Let's apply this configuration file in the cluster

kubectl apply -f 03-managing-pods-using-replica-set/replica-set.yml

Now let's see the ReplicaSet created using the command

kubectl get replicaset

ReplicaSet Output

Desire - The number of Pods that we declared in the file.

Current - The pods that are running in the cluster and that are being managed by the replica set.

Ready - The pods that are running.

Let's see if the application is working using for now the command:

kubectl port-forward replicaset/programmingonmars-replicaset 8000:8080

And we can see accessing localhost:8000 using the terminal or the browser.

Curl Output

Let's access the URL

Updated Curl Output

Suppose that we have 10, 100, 1000 pods? Yes, using ReplicaSet we needed to delete these all pods.

But we need to solve it using deployments

We will see in the next post. See you there!

Tags: Microservice, Kubernetes, replicaset, k8s

Tags:
KubernetesReplicaSetk8sDevOps
  • Privacy Policy
  • Terms And Condition
  • Contact
© Programming On Mars