28

I have created a Kubernetes autoscaler, but I need to change its parameters. How do I update it?

I've tried the following, but it fails:

✗ kubectl autoscale -f docker/production/web-controller.yaml --min=2 --max=6
Error from server: horizontalpodautoscalers.extensions "web" already exists
aknuds1
  • 65,625
  • 67
  • 195
  • 317

4 Answers4

52

You can always interactively edit the resources in your cluster. For your autoscale controller called web, you can edit it via:

kubectl edit hpa web

If you're looking for a more programmatic way to update your horizontal pod autoscaler, you would have better luck describing your autoscaler entity in a yaml file, as well. For example, here's a simple Replication Controller, paired with a Horizontal Pod Autoscale entity:

 apiVersion: v1
 kind: ReplicationController
 metadata:
   name: nginx
 spec:
   replicas: 2
   template:
     metadata:
       labels:
         run: nginx
     spec:
       containers:
       - name: nginx
         image: nginx
         ports:
         - containerPort: 80
 ---
 apiVersion: autoscaling/v1
 kind: HorizontalPodAutoscaler
 metadata:
   name: nginx
   namespace: default
 spec:
   maxReplicas: 3
   minReplicas: 2
   scaleTargetRef:
     apiVersion: v1
     kind: ReplicationController
     name: nginx

With those contents in a file called nginx.yaml, updating the autoscaler could be done via kubectl apply -f nginx.yaml.

amcelwee
  • 1,175
  • 1
  • 9
  • 10
  • To delete just one of the deployments in a larger `hpa` config, run `kubectl delete hpa `. – avocade May 04 '17 at 10:46
  • does the autoscaler work with deployments as it does for replicationController ? like `spec.scaleTargetRef.kind` with `Deployment` value ? – Ben Jun 22 '17 at 20:34
  • If you'd like to get your autoscale command as a yaml, do `kubectl autoscale deploy/your-deployment --cpu-percent=90 --min=2 --max=10 --dry-run -o yaml` – FooBar Apr 19 '22 at 23:49
37

You can use the kubectl patch command as well, to see its current status

kubectl get hpa <autoscaler-name-here> -o json

An example output:

{
"apiVersion": "autoscaling/v1",
"kind": "HorizontalPodAutoscaler",
"metadata": {
    ...
    "name": "your-auto-scaler",
    "namespace": "your-namespace",
    ...
},
"spec": {
    "maxReplicas": 50,
    "minReplicas": 2,
    "scaleTargetRef": {
        "apiVersion": "extensions/v1beta1",
        "kind": "Deployment",
        "name": "your-deployment"
    },
    "targetCPUUtilizationPercentage": 40
},
"status": {
    "currentReplicas": 1,
    "desiredReplicas": 2,
    "lastScaleTime": "2017-12-13T16:23:41Z"
}
}

If you want to update the minimum number of replicas:

kubectl -n your-namespace patch hpa your-auto-scaler --patch '{"spec":{"minReplicas":1}}'

The same logic applies to other parameters found in the autoscaler configuration, change minReplicas to maxReplicas if you want to update the maximum number of allowed replicas.

Andre Hahn
  • 471
  • 4
  • 4
22

First delete the autoscaler and then re-create it:

✗ kubectl delete hpa web
✗ kubectl autoscale -f docker/production/web-controller.yaml --min=2 --max=6
aknuds1
  • 65,625
  • 67
  • 195
  • 317
  • If you'd like to get your autoscale command as a yaml, do `k autoscale deploy/your-deployment --cpu-percent=90 --min=2 --max=10 --dry-run -o yaml` – FooBar Apr 19 '22 at 23:49
0

I tried multiple options but the one which works the best is First delete the existing hpa

kubectl delete hpa web

then recreate another one

kubectl autoscale -f docker/production/web-controller.yaml --min=2 --max=6

Shithanshu
  • 21
  • 1