ReplicaSet

A ReplicaSet ensures that a specified number of pod replicas are running at any given time.

Module

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.

Overview

At the end of this module, you will :

  • Learn what is a Replicaset

  • Learn how to scale a Pod

  • Understand why replication is part of High Availability

Prerequisites

Create the directory data/replicaset in your home folder to manage the YAML file needed in this module.

mkdir ~/data/replicaset

Create

A ReplicaSet can be easily scaled up or down by simply updating the .spec.replicas field in a YAML file definition. The ReplicaSet controller ensures that a desired number of pods with a matching label selector are available and operational

The create command create a ReplicaSet object based on a yaml file definition to manage the number of a pods identified by his labels.

Exercise n°1

Create a ReplicaSet object to scale an Nginx Pod with 3 replicas.

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: mynginxreplicaset
  labels:
    app: mynginxreplicaset
    env: formation
spec:
  replicas: 3
  selector:
    matchLabels:
      app: mynginxreplicaset
    matchExpressions:
      - {key: app, operator: In, values: [mynginxreplicaset]}
  template:
    metadata:
      labels:
        app: mynginxreplicaset
        env: formation
    spec:
      containers:
      - name: nginx
        image: nginx
kubectl apply  -f data/replicaset/01_replicaset.yaml

Get

The get command list the object asked. It could be a single object or a list of multiple objects comma separated. This command is useful to get the status of each object. The output can be formatted to only display some information based on some json search or external tools like tr, sort, uniq.

The default output of a get ReplicaSet command display three replicas status :

  • Name : the name of the newly created resource

  • Desired : the number of replicas defined in the YAML file definition

  • Current : the current number of replicas deployed in the cluster

  • Ready : the number of production ready replicas in the cluster

  • Age : the age since his creation

Exercise n°1

List the ReplicaSet created in the default namespace.

kubectl get replicaset

Describe

Once an object is running, it is inevitably a need to debug problems or check the configuration deployed.

The describe command display a lot of configuration information about the ReplicaSet(s) (labels, resource requirements, etc.) or any other Kubernetes objects, as well as status information about the ReplicaSet(s) and Pod (state, readiness, restart count, events, etc.).

This command is really useful to introspect and debug an object deployed in a cluster.

Exercise n°1

Describe the ReplicaSet created in the default namespace.

kubectl describe replicaset mynginxreplicaset

Explain

Kubernetes come with a lot of documentation about his objects and the available options in each one. Those information can be fin easily in command line or in the official Kubernetes documentation.

The explain command allows to directly ask the API resource via the command line tools to display information about each Kubernetes objects and their architecture.

Exercise n°1

Get the documentation of a specific field of a resource.

kubectl explain replicaset.spec

Add the --recursive flag to display all of the fields at once without descriptions.

Scale

Some Kubernetes objects like Deployment, ReplicaSet, Replication Controller, or Job can be easily scaled in command line.

The command line management has to be used only for debug purpose. In production environment, it's recommended to update the YAML file definition to ensure a consistent deployment.

Exercise n°1

Scale the ReplicaSet mynginxreplicaset to 5 in command line.

kubectl scale replicaset mynginxreplicaset --replicas=5

List the ReplicaSet created in the default namespace.

kubectl get replicaset

Delete

The delete command delete resources by filenames, stdin, resources and names, or by resources and label selector.

Deleting a ReplicaSet will automatically delete each Pods associated with.

Note that the delete command does NOT do resource version checks, so if someone submits an update to a resource right when you submit a delete, their update will be lost along with the rest of the resource.

Exercise n°1

Delete the ReplicaSet mynginxreplicaset in command line.

kubectl delete replicaset mynginxreplicaset

Module exercise

The purpose of this section is to manage each steps of the lifecycle of an application to better understand each concepts of the Kubernetes course.

The main objective in this module is to extract each part of the Voting App in a distinct Pod and manage it with a ReplicaSet.

For more information about the application used all along the course, please refer to the Exercise App > Voting App link in the left panel.

Based on the principles explain in this module, try by your own to handle this steps. The development of a yaml file is recommended.

The file developed has to be stored in this directory : ~/data/votingapp/02_replicas

  1. Delete the Pods created in the previous module exercise

  2. Create a ReplicaSet to replicate the worker Pod to 1

  3. Ensure the Pod is replicate to 1 and it is up and running

  4. Scale the worker Pods to 3 in command line

  5. Ensure the worker Pod is the only Pods replicate to 3 and it is up and running

External documentation

Those documentations can help you to go further in this topic :

  • Kubernetes official documentation on ReplicaSet

  • Kubernetes official documentation on label selector to understand difference between ReplicationController and ReplicaSet

Last updated