Scaling Up with Kubernetes Replica
The Replica resource is used for system redundancy. Replica can automatically place and maintain the number of replicas defined in the spec, and it can automatically recover when a Pod stops.
Running Replica with a YAML File
Here as well, you need to write the settings in a YAML file.
replicaset.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: helloworld
labels:
app: helloworld
spec:
replicas: 5
selector:
matchLabels:
app: helloworld
template:
metadata:
labels:
app: helloworld
spec:
containers:
- name: helloworld
image: gcr.io/google-samples/hello-app:1.0
Pods are replicated according to the value specified in the replicas field of this YAML file. In this file, the value is 5, so five Pods are created.
To start Pods as a ReplicaSet using this YAML file, run the following command.
kubectl apply -f replicaset.yaml
Result:
% kubectl apply -f replicaset.yaml
replicaset.apps/helloworld created
Run the following command to check whether the ReplicaSet was created.
kubectl get replicaset
Result:
% kubectl get replicaset
NAME DESIRED CURRENT READY AGE
helloworld 5 5 5 27s
% kubectl get pod
NAME READY STATUS RESTARTS AGE
helloworld-22r5b 1/1 Running 0 45s
helloworld-9bkcr 1/1 Running 0 45s
helloworld-ft2b7 1/1 Running 0 45s
helloworld-ndsph 1/1 Running 0 45s
helloworld-zrsw4 1/1 Running 0 45s
You can confirm that the ReplicaSet was created. Since it shows that five Pods were created, checking the Pod list confirms that the Pods were actually created. You can also see that all of these Pods are named in the form helloworld-[hash].
Increasing the Number of Running Pods
You can also increase the number of Pods while they are running. This can be done by adding the --replicas option to the kubectl scale command.
kubectl scale --replicas=[number of Pods] replicaset/[replicaset name]
As an example, increase it to 10.
% kubectl scale --replicas=10 replicaset/helloworld
replicaset.apps/helloworld scaled
Check the ReplicaSet list again.
% kubectl get replicaset
NAME DESIRED CURRENT READY AGE
helloworld 10 10 10 5m11s
Now check the Pods as well.
% kubectl get pod
NAME READY STATUS RESTARTS AGE
helloworld-22r5b 1/1 Running 0 5m36s
helloworld-26x7g 1/1 Running 0 76s
helloworld-9bkcr 1/1 Running 0 5m36s
helloworld-ft2b7 1/1 Running 0 5m36s
helloworld-ldfpk 1/1 Running 0 76s
helloworld-ndsph 1/1 Running 0 5m36s
helloworld-sjg5c 1/1 Running 0 76s
helloworld-smdhb 1/1 Running 0 76s
helloworld-xgl6m 1/1 Running 0 76s
helloworld-zrsw4 1/1 Running 0 5m36s
The result shows that the number of Pods has increased to 10.
When a Pod Stops
Next, let’s forcibly stop one of the running Pods. In this case, the ReplicaSet determines that one of the 10 Pods has stopped and creates a Pod again. At this time, a different hash from the previous one is assigned, and one new Pod is created with a different hash.
For testing, stop helloworld-22r5b as follows.
% kubectl delete pod helloworld-22r5b
pod "helloworld-22r5b" deleted
Check the Pod status.
% kubectl get pod
NAME READY STATUS RESTARTS AGE
helloworld-26x7g 1/1 Running 0 6m9s
helloworld-9bkcr 1/1 Running 0 10m
helloworld-ft2b7 1/1 Running 0 10m
helloworld-ldfpk 1/1 Running 0 6m9s
helloworld-ndsph 1/1 Running 0 10m
helloworld-sjg5c 1/1 Running 0 6m9s
helloworld-smdhb 1/1 Running 0 6m9s
helloworld-xgl6m 1/1 Running 0 6m9s
helloworld-xmxbx 1/1 Running 0 16s <<<<< newly created
helloworld-zrsw4 1/1 Running 0 10m
In this case, you can confirm that helloworld-xmxbx was automatically created in place of the stopped helloworld-22r5b.