【问题标题】:How do I update a Kubernetes autoscaler?如何更新 Kubernetes 自动扩缩器?
【发布时间】:2016-09-05 02:41:29
【问题描述】:

我创建了一个Kubernetes autoscaler,但我需要更改它的参数。如何更新?

我尝试了以下方法,但失败了:

✗ kubectl autoscale -f docker/production/web-controller.yaml --min=2 --max=6
Error from server: horizontalpodautoscalers.extensions "web" already exists

【问题讨论】:

    标签: kubernetes autoscaling


    【解决方案1】:

    您始终可以以交互方式编辑集群中的资源。对于名为 web 的自动缩放控制器,您可以通过以下方式对其进行编辑:

    kubectl edit hpa web
    

    如果您正在寻找一种更加程序化的方式来更新您的水平 pod 自动扩缩器,那么您最好在 yaml 文件中描述您的自动扩缩器实体。例如,这是一个简单的复制控制器,与 Horizo​​ntal Pod Autoscale 实体配对:

     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
    

    将这些内容保存在名为 nginx.yaml 的文件中,可以通过 kubectl apply -f nginx.yaml 更新自动缩放器。

    【讨论】:

    • 要删除较大的hpa 配置中的一个部署,请运行kubectl delete hpa <DEPLOYMENT NAME>
    • 自动扩缩器是否像对 replicationController 一样适用于部署?像 spec.scaleTargetRef.kindDeployment 值一样?
    【解决方案2】:

    您也可以使用 kubectl patch 命令查看其当前状态

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

    示例输出:

    {
    "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"
    }
    }
    

    如果要更新最小副本数:

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

    同样的逻辑适用于自动扩缩器配置中的其他参数,如果要更新允许的最大副本数,请将 minReplicas 更改为 maxReplicas。

    【讨论】:

      【解决方案3】:

      先删除自动缩放器,然后重新创建它:

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

      【讨论】:

        猜你喜欢
        • 2020-10-20
        • 2019-11-16
        • 2017-05-29
        • 2020-03-12
        • 2016-08-24
        • 1970-01-01
        • 2015-04-29
        • 2021-01-20
        • 2018-08-04
        相关资源
        最近更新 更多