CKA Exam Practice Series - 2
CKA Exam Practice Series - 2
Q2 Scale down
There are two Pods named db-0 and db-1 in Namespace project. management asked you to scale the Pods down to one replica to save resources.
Answer
You can scale down the replicas of the db Pods in the project Namespace by using the kubectl scale command with the --replicas flag set to the desired number of replicas (in this case, one).
Here's an example command to scale down the db Pods:
kubectl scale --replicas=1 deployment/db -n project
This command scales down the deployment named db
in the project
Namespace to one replica. The deployment resource is used here because it is the resource that manages the replica set for the db Pods.
Note that if the db Pods were created using a StatefulSet or a ReplicaSet instead of a Deployment, the command would be slightly different. For example, to scale down a StatefulSet named db to one replica, you would run:
kubectl scale --replicas=1 statefulset/db -n project
After running this command, the number of replicas for the db Pods will be reduced to one. If the Pods were previously running on different nodes, the one remaining Pod will be randomly assigned to one of those nodes.
The example yaml of statefulset will be like below:
apiVersion: apps/v1 kind: StatefulSet metadata: name: db namespace: project spec: replicas: 2 selector: matchLabels: app: db serviceName: db template: metadata: labels: app: db spec: containers: - name: db-container image: mysql:5.7 ports: - containerPort: 3306 name: db env: - name: MYSQL_ROOT_PASSWORD value: "password" volumeMounts: - name: db-storage mountPath: /var/lib/mysql volumeClaimTemplates: - metadata: name: db-storage spec: accessModes: [ "ReadWriteOnce" ] resources: requests: storage: 1Gi
To scale down the replicas of this StatefulSet to one, you can use the kubectl scale command as shown in my previous answer:
kubectl scale --replicas=1 statefulset/db -n project
Note that when scaling down a StatefulSet, Kubernetes will terminate the Pods in reverse order. In this case, the second Pod (db-1) will be terminated first, and then the first Pod (db-0) will be terminated. This ensures that the StatefulSet maintains its ordered identity.