Vertical application scaling in a cluster
Managed Service for Kubernetes supports several types of autoscaling. In this article you will learn how to configure the automatic management of pod resources with Vertical Pod Autoscaler:
Before you begin
-
If you don't have the Nebius AI command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder using the
--folder-name
or--folder-id
parameter. -
Create a Managed Service for Kubernetes cluster and a node group in any suitable configuration. When creating a cluster and a group of nodes:
- Select automatic as your public address assignment method.
-
Install kubectl
and configure it to work with the created cluster.
Create Vertical Pod Autoscaler and a test application
-
Create a file called
app.yaml
with thenginx
test application and load balancer settings:app.yaml--- ### Deployment apiVersion: apps/v1 kind: Deployment metadata: name: nginx labels: app: nginx spec: replicas: 1 selector: matchLabels: app: nginx template: metadata: name: nginx labels: app: nginx spec: containers: - name: nginx image: k8s.gcr.io/hpa-example resources: requests: memory: "256Mi" cpu: "500m" limits: memory: "500Mi" cpu: "1" --- ### Service apiVersion: v1 kind: Service metadata: name: nginx spec: selector: app: nginx ports: - protocol: TCP port: 80 targetPort: 80 type: LoadBalancer
-
Create a file called
vpa.yaml
with the Vertical Pod Autoscaler configuration:vpa.yaml--- apiVersion: autoscaling.k8s.io/v1 kind: VerticalPodAutoscaler metadata: name: nginx spec: targetRef: apiVersion: "apps/v1" kind: Deployment name: nginx updatePolicy: updateMode: "Auto"
-
Create objects:
kubectl apply -f app.yaml && \ kubectl apply -f vpa.yaml
-
Make sure that the Vertical Pod Autoscaler and
nginx
pods have changed their status toRunning
:kubectl get pods -n kube-system | grep vpa && \ kubectl get pods | grep nginx
Result:
vpa-admission-controller-58cf99779c-qmxtv 1/1 Running 0 44h vpa-recommender-678f4f6d4b-jqvgt 1/1 Running 0 44h vpa-updater-64ddd67787-xqsts 1/1 Running 0 44h nginx-6c5cbfc6d9-62j7w 1/1 Running 0 42h nginx-6c5cbfc6d9-6t4nz 1/1 Running 0 42h
Note
For Vertical Pod Autoscaler to run properly, a minimum of two
nginx
pods are required.
Test Vertical Pod Autoscaler
To test Vertical Pod Autoscaler, nginx
application workload will be simulated.
-
Review the recommendations provided by Vertical Pod Autoscaler prior to creating the workload:
kubectl describe vpa nginx
Note the low
Cpu
values in theStatus.Recommendation.Container Recommendations
metrics:Name: nginx Namespace: default Labels: <none> Annotations: <none> API Version: autoscaling.k8s.io/v1 Kind: VerticalPodAutoscaler ... Status: Conditions: Last Transition Time: 2022-03-18T08:02:04Z Status: True Type: RecommendationProvided Recommendation: Container Recommendations: Container Name: nginx Lower Bound: Cpu: 25m Memory: 262144k Target: Cpu: 25m Memory: 262144k Uncapped Target: Cpu: 25m Memory: 262144k Upper Bound: Cpu: 25m Memory: 262144k
-
Make sure that Vertical Pod Autoscaler is managing the
nginx
application pod resources:kubectl get pod <name of an nginx pod> --output yaml
Result:
apiVersion: v1 kind: Pod metadata: annotations: vpaObservedContainers: nginx vpaUpdates: 'Pod resources updated by nginx: container 0: cpu request, memory request, cpu limit, memory limit' ... spec: containers: ... name: nginx resources: limits: cpu: 50m memory: 500000Ki requests: cpu: 25m memory: 262144k
-
Run the workload simulation process in a separate window:
URL=$(kubectl get service nginx -o json \ | jq -r '.status.loadBalancer.ingress[0].ip') && \ while true; do wget -q -O- http://$URL; done
Tip
To increase load and accelerate scenario progress, run several processes in separate windows.
-
After several minutes, review the recommendation provided by Vertical Pod Autoscaler after the workload is created:
kubectl describe vpa nginx
Vertical Pod Autoscaler allocated additional resources to the pods as the workload increased. Note the increased
Cpu
values in theStatus.Recommendation.Container Recommendations
metrics:Name: nginx Namespace: default Labels: <none> Annotations: <none> API Version: autoscaling.k8s.io/v1 Kind: VerticalPodAutoscaler ... Status: Conditions: Last Transition Time: 2022-03-18T08:02:04Z Status: True Type: RecommendationProvided Recommendation: Container Recommendations: Container Name: nginx Lower Bound: Cpu: 25m Memory: 262144k Target: Cpu: 410m Memory: 262144k Uncapped Target: Cpu: 410m Memory: 262144k Upper Bound: Cpu: 28897m Memory: 1431232100
-
Stop simulating the workload. Within a few minutes, the
Status.Recommendation.Container Recommendations
metric values will return to their original values.
Delete the resources you created
Delete the resources you no longer need to avoid paying for them: