Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Kubernetes

Kubernetes: Use HPA for Automatic Scaling

- 10.07.26 - ErcanOPAK

📈 HPA = Automatic Scaling

Traffic changes constantly. Horizontal Pod Autoscaler scales automatically. More pods when needed, less when not.

📝 HPA Setup

# Install metrics-server
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

# Verify metrics-server
kubectl top pods
kubectl top nodes

# Create HPA
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
    name: web-hpa
spec:
    scaleTargetRef:
        apiVersion: apps/v1
        kind: Deployment
        name: web
    minReplicas: 2
    maxReplicas: 10
    metrics:
    - type: Resource
        resource:
            name: cpu
            target:
                type: Utilization
                averageUtilization: 70
    - type: Resource
        resource:
            name: memory
            target:
                type: Utilization
                averageUtilization: 80

# Create HPA via command
kubectl autoscale deployment web --cpu-percent=70 --min=2 --max=10

# Check HPA status
kubectl get hpa
kubectl describe hpa web-hpa

🎯 Advanced HPA

# Custom metrics (Prometheus)
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
    name: custom-hpa
spec:
    scaleTargetRef:
        apiVersion: apps/v1
        kind: Deployment
        name: app
    minReplicas: 2
    maxReplicas: 20
    metrics:
    - type: Pods
        pods:
            metric:
                name: http_requests_per_second
            target:
                type: AverageValue
                averageValue: 100

# Multiple metrics
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
    name: multi-metric-hpa
spec:
    metrics:
    - type: Resource
        resource:
            name: cpu
            target:
                type: Utilization
                averageUtilization: 60
    - type: Resource
        resource:
            name: memory
            target:
                type: Utilization
                averageUtilization: 75
    - type: Pods
        pods:
            metric:
                name: requests_per_second
            target:
                type: AverageValue
                averageValue: 500

# Behavior configuration
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
    name: behavior-hpa
spec:
    behavior:
        scaleDown:
            stabilizationWindowSeconds: 300
            policies:
            - type: Percent
                value: 10
                periodSeconds: 60
        scaleUp:
            stabilizationWindowSeconds: 0
            policies:
            - type: Percent
                value: 100
                periodSeconds: 30
            - type: Pods
                value: 4
                periodSeconds: 30
            selectPolicy: Max

💡 HPA Tips

  • Set appropriate min/max replicas
  • Monitor HPA behavior
  • Use custom metrics for business logic
  • Consider VPA for vertical scaling
  • Test scaling scenarios

“HPA scales automatically. More pods when busy, fewer when idle. Essential for cost optimization.”

— DevOps Engineer

Related posts:

Kubernetes: Liveness vs Readiness Probes - Don't Kill Your Traffic

Kubernetes: Use Pod Affinity to Schedule Related Pods Together

Why Liveness ≠ Readiness Probes

Post Views: 0

Post navigation

WordPress: Use REST API for Headless CMS
Docker: Optimize Images with Dockerfile Best Practices

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

July 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  
« Jun    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (960)
  • How to add default value for Entity Framework migrations for DateTime and Bool (898)
  • How to make theater mode the default for Youtube (857)
  • Get the First and Last Word from a String or Sentence in SQL (840)
  • How to select distinct rows in a datatable in C# (814)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (597)
  • Add Constraint to SQL Table to ensure email contains @ (582)
  • Average of all values in a column that are not zero in SQL (545)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (512)
  • Find numbers with more than two decimal places in SQL (460)

Recent Posts

  • C#: Use Using Statements for Resource Management
  • C#: Use Lambda Expressions for Concise Code
  • SQL: Use GROUP BY for Data Aggregation
  • .NET Core: Master Routing for Clean URLs
  • Git: Use Reset to Undo Local Changes
  • Ajax: Use Axios for HTTP Requests
  • JavaScript: Understand Hoisting
  • HTML: Use Web Storage for Client-Side Data
  • CSS: Use Filter Effects for Visual Magic
  • Windows 11: Unlock God Mode for All Settings

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (960)
  • How to add default value for Entity Framework migrations for DateTime and Bool (898)
  • How to make theater mode the default for Youtube (857)
  • Get the First and Last Word from a String or Sentence in SQL (840)
  • How to select distinct rows in a datatable in C# (814)

Recent Posts

  • C#: Use Using Statements for Resource Management
  • C#: Use Lambda Expressions for Concise Code
  • SQL: Use GROUP BY for Data Aggregation
  • .NET Core: Master Routing for Clean URLs
  • Git: Use Reset to Undo Local Changes

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com