【问题标题】:HorizontalPodAutoscaler: missing field "conditions"Horizo​​ntalPodAutoscaler:缺少字段“条件”
【发布时间】:2020-12-10 19:56:17
【问题描述】:

朋友们,我正在尝试按照 k8s 的hpa tutorial 实现 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 }}

【问题讨论】:

    标签: kubernetes horizontal-pod-autoscaling


    【解决方案1】:

    背景

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

    在文档Understanding Kubernetes Objects - Object Spec and Status 中,您可以找到信息:

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

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

    Appendix: Horizontal Pod Autoscaler Status Conditions中部分描述了您的情况

    当使用HorizontalPodAutoscalerautoscaling/v2beta2 形式时,您将能够看到 Kubernetes 在 Horizo​​ntalPodAutoscaler 上设置的状态条件。这些状态条件表明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 时,您已创建HPA 用于部署php-apache

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

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

    第一个命令将显示带有任何 apiVersionv2beta2v2beta1 等)的所有 HPA 对象,第二个命令将仅显示带有 apiVersion: hpa.v2beta2.autoscalingHPA。我的集群默认使用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 配置的新文件。该文件基于之前的kubectl autoscale 命令创建的HPA

    $ 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 系统及其组件提供和更新。

    如果要基于YAMLstatus 创建资源,则必须在status.conditions 中提供值,其中condition 需要array 值。

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

    快速解决方案

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 2017-09-11
      • 1970-01-01
      • 2013-01-02
      相关资源
      最近更新 更多