gpt4 book ai didi

kubernetes - Horizo​​ntalPodAutoscaler : missing field "conditions"

转载 作者:行者123 更新时间:2023-12-04 14:09:48 26 4
gpt4 key购买 nike

friend 们,我正在尝试按照 hpa tutorial 实现 HPA k8s 和我有以下错误:

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

这里是我的部署 list :

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

最佳答案

背景

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

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

Almost every Kubernetes object includes two nested object fields that govern the object's configuration: the object spec and the object status. For objects that have a spec, you have to set this when you create the object, providing a description of the characteristics you want the resource to have: its desired state.

The status describes the current state of the object, supplied and updated by the Kubernetes system and its components. The Kubernetes control plane continually and actively manages every object's actual state to match the desired state you supplied.

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

When using the autoscaling/v2beta2 form of the HorizontalPodAutoscaler, you will be able to see status conditions set by Kubernetes on the HorizontalPodAutoscaler. These status conditions indicate whether or not the HorizontalPodAutoscaler is able to scale, and whether or not it is currently restricted in any way.

来 self 的 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 部署

$ 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 资源。输出是一样的。

第一个命令将显示具有任何 apiVersion(v2beta2v2beta1 等)的所有 HPA 对象,第二个命令将仅在 apiVersion: hpa.v2beta2.autoscaling 时显示 HPA。我的集群默认使用 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 系统及其组件提供和更新。

如果你想创建基于 YAML 的带有 status 的资源,你必须在 status.conditions 中提供值,其中 条件 需要array 值。

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

快速解决方案

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

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

关于kubernetes - Horizo​​ntalPodAutoscaler : missing field "conditions",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65241439/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com