Vertical scaling of an application in a cluster
Managed Service for Kubernetes supports several types of autoscaling. In this tutorial, you will learn how to set up automatic pod resource management with Vertical Pod Autoscaler:
If you no longer need the resources you created, delete them.
Required paid resources
The support cost for this solution includes:
- Fee for using the master and outgoing traffic in a Managed Service for Kubernetes cluster (see Managed Service for Kubernetes pricing).
- Fee for using computing resources, OS, and storage in cluster nodes (VMs) (see Compute Cloud pricing).
- Fee for a public IP address for cluster nodes (see Virtual Private Cloud pricing).
Getting started
-
If you do not have the Yandex Cloud CLI installed yet, install and initialize it.
By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the
yc config set folder-id <folder_ID>command. You can also set a different folder for any specific command using the--folder-nameor--folder-idparameter. -
Create security groups for the Managed Service for Kubernetes cluster and its node groups.
Warning
The configuration of security groups determines the performance and availability of the cluster and the services and applications running in it.
-
Create a Managed Service for Kubernetes cluster. Use these settings:
- Use the security groups you created earlier.
- For Yandex Cloud internal network usage, your cluster does not need a public IP address. To enable internet access to your cluster, assign it a public IP address.
-
Create a node group. Use these settings:
- Use the security groups you created earlier.
- To enable internet access for your node group (e.g., for Docker image pulls), assign it a public IP address.
-
Install kubect
and configure it to work with the new cluster.If a cluster has no public IP address assigned and
kubectlis configured via the cluster's private IP address, runkubectlcommands on a Yandex Cloud VM that is in the same network as the cluster. -
Install Vertical Pod Autoscaler from this repository
as follows:cd /tmp && \ git clone https://github.com/kubernetes/autoscaler.git && \ cd autoscaler/vertical-pod-autoscaler/hack && \ ./vpa-up.sh
Create Vertical Pod Autoscaler and a test application
-
Create a file named
app.yamlwith thenginxtest 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: registry.k8s.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 named
vpa.yamlwith 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" minReplicas: 1 -
Create the objects:
kubectl apply -f app.yaml && \ kubectl apply -f vpa.yaml -
Make sure the Vertical Pod Autoscaler and
nginxpods switched toRunning:kubectl get pods -n kube-system | grep vpa && \ kubectl get pods | grep nginxResult:
vpa-admission-controller-58********-qmxtv 1/1 Running 0 44h vpa-recommender-67********-jqvgt 1/1 Running 0 44h vpa-updater-64********-xqsts 1/1 Running 0 44h nginx-6c********-62j7w 1/1 Running 0 42h
Test Vertical Pod Autoscaler
To test Vertical Pod Autoscaler, you will simulate nginx workload.
-
Review the recommendations provided by Vertical Pod Autoscaler prior to simulating the workload:
kubectl describe vpa nginxNote the low
Cpuvalues in theStatus.Recommendation.Container Recommendationsmetrics: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 Vertical Pod Autoscaler is managing the
nginxpod resources:kubectl get pod <nginx_pod_name> --output yamlResult:
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 -
In a separate terminal window, run the following command to simulate a workload:
URL=$(kubectl get service nginx -o json \ | jq -r '.status.loadBalancer.ingress[0].ip') && \ while true; do wget -q -O- http://$URL; doneTip
To increase the load and speed up the scenario, run multiple simulations in separate windows.
-
Wait a few minutes and review the recommendation provided by Vertical Pod Autoscaler after simulating the workload:
kubectl describe vpa nginxVertical Pod Autoscaler allocated additional resources to the pods as the workload increased. Note the increased
Cpuvalues in theStatus.Recommendation.Container Recommendationsmetrics: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 Recommendationsmetrics will regain their initial values.
Delete the resources you created
Delete the resources you no longer need to avoid paying for them:
- Delete the Kubernetes cluster.
- If you used static public IP addresses to access your cluster or nodes, release and delete them.