Tutorials  /  Container & Cloud

Optimizing Kubernetes Resource Utilization: Focus on Horizontal Pod Autoscaler and Metrics Server

Ccentron Redaktion · November 2024 ·4 min read ·Container & Cloud, Tutorial

Effectively managing resource usage and ensuring scalability is a central challenge when operating applications in Kubernetes clusters. This is where the Horizontal Pod Autoscaler (HPA) and Metrics Server come into play. In this post, we will show you how to use these two components to automatically and dynamically scale your applications based on current usage levels.

Step 1: Setting up the Metrics Server with Helm

The Metrics Server is an essential component for monitoring resources in Kubernetes clusters. We begin by setting up the Metrics Server using Helm. This involves adding the appropriate Helm repository, updating available packages, and installing the Metrics Server in our cluster. This enables us to effectively monitor and analyze resources such as CPU and memory.

# Add Helm repository for Metrics Server

helm repo add metrics-server https://kubernetes-sigs.github.io/metrics-server

# Update available packages

helm repo update metrics-server

# Install Metrics Server

helm install metrics-server metrics-server/metrics-server --namespace metrics-server

K8

Matching infrastructure at centron

From container to cluster: managed Kubernetes with cStack starts at €29.99 per month – control plane, autoscaler and traffic included. Explore managed Kubernetes →

Step 2: Configuring the Horizontal Pod Autoscaler (HPA)

The HPA is a Kubernetes feature that allows automatic scaling of the number of pods in an application based on various metrics. We’ll show you how to configure an HPA to scale your applications based on CPU or memory usage. This includes both applying a YAML manifest directly and using the kubectl command.

# Example YAML manifest for a Horizontal Pod Autoscaler (HPA)

apiVersion: autoscaling/v2beta2

kind: HorizontalPodAutoscaler

metadata:

name: myapp-hpa

spec:

scaleTargetRef:

apiVersion: apps/v1

kind: Deployment

name: myapp-deployment

minReplicas: 1

maxReplicas: 10

metrics:

- type: Resource

resource:

name: cpu

target:

type: Utilization

averageUtilization: 50

Step 3: Automatically Scaling Applications with the Metrics Server

In this step, we demonstrate the practical application of the HPA and the Metrics Server with two scenarios: constant and variable load. In the first scenario, we use a CPU-intensive application, while in the second scenario, external load is simulated through rapid HTTP requests. You’ll learn how to configure the HPA for both scenarios and observe how it automatically responds to the load, adjusting the number of pods accordingly.

# Example YAML manifest for an application with constant load and HPA

apiVersion: apps/v1

kind: Deployment

metadata:

name: constant-load-app

spec:

replicas: 1

selector:

matchLabels:

app: constant-load

template:

metadata:

labels:

app: constant-load

spec:

containers:

- name: constant-load

image: busybox

resources:

limits:

cpu: 200m

command: ["sh", "-c"]

args: ["while true; do echo 'Constant Load'; sleep 1; done"]

Conclusion

Efficiently scaling Kubernetes applications is a crucial part of managing container-based workloads. By combining the Horizontal Pod Autoscaler with the Metrics Server, you can optimize the performance of your applications, use resources efficiently, and reduce the costs of your Kubernetes infrastructure. With these tools, you are well-equipped to keep your Kubernetes cluster scalable and stable. Optimizing Kubernetes Resource Utilization: Focus on Horizontal Pod Autoscaler and Metrics Server.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Container & Cloud
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren