The `kubectl hpa` command in Kubernetes is used to manage Horizontal Pod Autoscalers (HPA), which automatically scale the number of pods in a deployment based on observed CPU utilization or other metrics. This helps ensure that your application can handle varying loads efficiently. To create or update an HPA, you can use the following command:
kubectl autoscale deployment <deployment-name> --cpu-percent=<target-cpu-percentage> --min=<min-pods> --max=<max-pods>
Example YAML configuration for an HPA:
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: <hpa-name>
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: <deployment-name>
minReplicas: <min-pods>
maxReplicas: <max-pods>
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: <target-cpu-percentage>