Kubernetes – ReplicaSets – YAML file to create replicasets

One awesome feature of Kubernetes is the replicasets. Simply put, say you want to run multiple pods – x number of pods all the time for fault tolerance. It is often used to guarantee the availability of a specified number of identical pods identified by labels.

Replicasets will enable you do this. I would strongly encourage you to check out kubernetes.io to read more about it plus the site has tons of useful documentation for everything related to Kubernetes

Here is a complete yaml file that you can readily use to create replica sets. Note: I will try to keep this file updated as some of the specs may change with future versions. I am using Bitnami’s Ghost container for demo purposes

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: mywebapp
  labels:
    app: mywebapp
spec:
  # modify replicas according to your case
  replicas: 7
  selector:
    matchLabels:
      app: mywebapp
  template:
    metadata:
      labels:
        app: mywebapp
    spec:
      containers:
      - name: ghost
        image: ghost

If you see the yaml file above, the definition lets you create 7 pods and will ensure at least 7 pods remain active all the time.

Some of this is straightforward yaml definition but important concept is understand that all pods are going to have labels and the replicaset is going to use those labels to ensure at least 7 pods with that label exists.

The line selector: matchLabels: will look at the right pod label and spin up the required number of instances defined.

You can readily use this yaml file and run this command to create the pods

kubectl create -f mywebapp-replicasets.yaml