首页 > 解决方案 > Horizo​​ntalPodAutoscaler:缺少字段“条件”

问题描述

朋友们,我正在尝试按照k8s的hpa 教程实现 HPA,但出现以下错误:

ValidationError(Horizo​​ntalPodAutoscaler.status):在 io.k8s.api.autoscaling.v2beta2.Horizo​​ntalPodAutoscalerStatus 中缺少必填字段“条件”

我找不到有关此字段“条件”的任何信息。有人知道我可能做错了什么吗?这是我的 HPA 的 YAML:

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: {{ .Values.name }}
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: {{ .Values.name}}
  minReplicas: {{ .Values.deployment.minReplicas }}
  maxReplicas: {{ .Values.deployment.maxReplicas }}
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50
status:
  observedGeneration: 1
  lastScaleTime: <some-time>
  currentReplicas: 2
  desiredReplicas: 2
  currentMetrics:
  - type: Resource
    resource:
      name: cpu
      current:
        averageValue: 0

这里是我的部署清单:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.name }}
spec:
  replicas: {{ .Values.deployment.replicaCount }}
  selector:
    matchLabels:
      app: {{ .Values.labels}}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
      labels:
        app: {{ .Values.labels }}
    spec:
      initContainers:
      - name: check-rabbitmq
        image: {{ .Values.initContainers.image }}
        command: ['sh', '-c',
        'until wget http://$(RABBITMQ_DEFAULT_USER):$(RABBITMQ_DEFAULT_PASS)@rabbitmq:15672/api/aliveness-test/%2F; 
        do echo waiting; sleep 2; done;']
        envFrom:
        - configMapRef:
            name: {{ .Values.name }}
      - name: check-mysql
        image: {{ .Values.initContainers.image }}
        command: ['sh', '-c', 'until nslookup mysql-primary.default.svc.cluster.local; do echo waiting for mysql; sleep 2; done;']
      containers:
      - name: {{ .Values.name }}
        image: {{ .Values.deployment.image }}
        ports:
        - containerPort: {{ .Values.ports.containerPort }} 
        resources:
          limits:
            cpu: 500m
          requests:
            cpu: 200m
        envFrom:
        - configMapRef:
            name: {{ .Values.name }}

标签: kuberneteshorizontal-pod-autoscaling

解决方案


背景

不确定,为什么要HPA使用status部分创建。如果您删除此部分,它将HPA毫无问题地创建。

在文档了解 Kubernetes 对象 - 对象规范和状态中,您可以找到以下信息:

几乎每个 Kubernetes 对象都包含两个管理对象配置的嵌套对象字段: objectspec和 object status。对于具有 的对象spec,您必须在创建对象时进行设置,提供您希望资源具有的特征的描述:其所需的状态。

描述对象的status当前状态,由 Kubernetes 系统及其组件提供和更新。Kubernetes 控制平面持续主动地管理每个对象的实际状态,以匹配您提供的所需状态。

附录中部分描述了您的情况:Horizo​​ntal Pod Autoscaler 状态条件

使用 的autoscaling/v2beta2形式时HorizontalPodAutoscaler,您将能够在 Horizo​​ntalPodAutoscaler 上看到 Kubernetes 设置的状态条件。这些状态条件表明是否HorizontalPodAutoscaler能够scale,以及当前是否以任何方式受到限制。

我的 GKE 测试集群中的示例

正如我之前提到的,如果您删除status部分,您将能够创建HPA.

$ kubectl apply -f - <<EOF
> apiVersion: autoscaling/v2beta2
> kind: HorizontalPodAutoscaler
> metadata:
>   name: hpa-apache
> spec:
>   scaleTargetRef:
>     apiVersion: apps/v1
>     kind: Deployment
>     name: php-apache
>   minReplicas: 1
>   maxReplicas: 3
>   metrics:
>   - type: Resource
>     resource:
>       name: cpu
>       target:
>         type: Utilization
>         averageUtilization: 50
> EOF
horizontalpodautoscaler.autoscaling/hpa-apache created

根据HPA文档,我创建了PHP Deployment.

$ kubectl apply -f https://k8s.io/examples/application/php-apache.yaml
deployment.apps/php-apache created
service/php-apache created

当您执行为部署kubectl autoscale创建的命令时HPAphp-apache

$ kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10
horizontalpodautoscaler.autoscaling/php-apache autoscaled

现在您可以使用or查看hpa资源。输出是一样的。kubectl get hpakubectl get hpa.v2beta2.autoscaling

第一个命令将显示HPA带有任何apiVersion( v2beta2,v2beta1等) 的所有对象,第二个命令将HPA仅显示带有apiVersion: hpa.v2beta2.autoscaling. 我的集群默认使用v2beta2,所以两个命令的输出是相同的。

$ kubectl get hpa
NAME         REFERENCE               TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
php-apache   Deployment/php-apache   0%/50%    1         10        1          76s
$ kubectl get hpa.v2beta2.autoscaling
NAME         REFERENCE               TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
php-apache   Deployment/php-apache   0%/50%    1         10        1          84s

执行以下命令时,hpa将创建带有配置的新文件。此文件基于HPA已从先前kubectl autoscale命令创建的。

$ kubectl get hpa.v2beta2.autoscaling -o yaml > hpa-v2.yaml
# If I would use command `kubectl get hpa hpa-apache > hpa-v2.yaml` file would look the same
$ cat hpa-v2.yaml
apiVersion: v1
items:
- apiVersion: autoscaling/v2beta2
  kind: HorizontalPodAutoscaler
  metadata:
...
status:
    conditions:
    - lastTransitionTime: "2020-12-11T10:44:43Z"
      message: recent recommendations were higher than current one, applying the highest
        recent recommendation
      reason: ScaleDownStabilized
      status: "True"
      type: AbleToScale
      ...
    currentMetrics:
    - resource:
        current:
          averageUtilization: 0
          averageValue: 1m

结论

描述对象的status当前状态,由 Kubernetes 系统及其组件提供和更新。

如果要基于YAMLwith创建资源,则必须在需要value 的地方status提供value。status.conditionsconditionarray

status:
  conditions:
  - lastTransitionTime: "2020-12-11T10:44:43Z"

快速解决方案

只需status从您的 YAML 中删除部分。

status如果您在从 YAML 清单中删除部分后仍然遇到任何问题,请告诉我。


推荐阅读