...

/

Creating ReplicaSets

Creating ReplicaSets

Learn how to create and retrieve a ReplicaSet.

Definition

Let’s look at a ReplicaSet named go-demo-2.yml based on the Pod we created in the previous chapter:

Press + to interact
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: go-demo-2
spec:
replicas: 2
selector:
matchLabels:
type: backend
service: go-demo-2
template:
metadata:
labels:
type: backend
service: go-demo-2
db: mongo
language: go
spec:
containers:
- name: db
image: mongo:3.3
- name: api
image: vfarcic/go-demo-2
env:
- name: DB
value: localhost
livenessProbe:
httpGet:
path: /demo/hello
port: 8080

Note: The apiVersion, kind, and metadata fields are mandatory with all Kubernetes objects. ReplicaSet is no exception because it’s also a Kubernetes object.

  • Line 1: We specify that the apiVersion is apps/v1.

  • Lines 2–3: The kind is ReplicaSet and metadata has the name key set to go-demo-2. We could have extended ReplicaSet metadata with labels. However, we’ve skipped that part since the labels only provide extra information. They do not affect the behavior of the ReplicaSet.

Since we’ve already explored the above three fields, we won’t dwell on them a lot. However, we should note that there is a fourth section called spec and it is mandatory. Let’s look at it in more detail in the line-by-line explanation below:

  • Line 5–6: The first field we define in the spec section is replicas. It sets the desired number of replicas of the Pod. In this case, the ...