How do I update a Kubernetes autoscaler?

aknuds1 picture aknuds1 · May 10, 2016 · Viewed 15.9k times · Source

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

Answer

amcelwee picture amcelwee · May 10, 2016

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.