kubernetes - HorizontalPodAutoscaler:缺少字段“条件”
问题描述
朋友们,我正在尝试按照k8s的hpa 教程实现 HPA,但出现以下错误:
ValidationError(HorizontalPodAutoscaler.status):在 io.k8s.api.autoscaling.v2beta2.HorizontalPodAutoscalerStatus 中缺少必填字段“条件”。
我找不到有关此字段“条件”的任何信息。有人知道我可能做错了什么吗?这是我的 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 }}
解决方案
背景
不确定,为什么要HPA
使用status
部分创建。如果您删除此部分,它将HPA
毫无问题地创建。
在文档了解 Kubernetes 对象 - 对象规范和状态中,您可以找到以下信息:
几乎每个 Kubernetes 对象都包含两个管理对象配置的嵌套对象字段: object
spec
和 objectstatus
。对于具有 的对象spec
,您必须在创建对象时进行设置,提供您希望资源具有的特征的描述:其所需的状态。
描述对象的
status
当前状态,由 Kubernetes 系统及其组件提供和更新。Kubernetes 控制平面持续主动地管理每个对象的实际状态,以匹配您提供的所需状态。
附录中部分描述了您的情况:Horizontal Pod Autoscaler 状态条件
使用 的
autoscaling/v2beta2
形式时HorizontalPodAutoscaler
,您将能够在 HorizontalPodAutoscaler 上看到 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
创建的命令时HPA
php-apache
$ kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10
horizontalpodautoscaler.autoscaling/php-apache autoscaled
现在您可以使用or查看hpa
资源。输出是一样的。kubectl get hpa
kubectl 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 系统及其组件提供和更新。
如果要基于YAML
with创建资源,则必须在需要value 的地方status
提供value。status.conditions
condition
array
status:
conditions:
- lastTransitionTime: "2020-12-11T10:44:43Z"
快速解决方案
只需status
从您的 YAML 中删除部分。
status
如果您在从 YAML 清单中删除部分后仍然遇到任何问题,请告诉我。
推荐阅读
- php - CardDAV 从 iCloud 获取所有联系人
- c# - 从字符串中删除点
- firebase - firebase部署功能上的触发功能
- reactjs - 如何模拟输入 onChange 事件,然后模拟点击结果
- javascript - 从 ftp 获取文件并在控制台中显示文件目录路径 - Node.js
- javascript - 如何将 JavaScript [对象字符串] 转换为 HTMLElement?
- reactjs - 反应:无法以 HTTP 格式从 webAPI 访问信息(WebApi 返回类型 IHttpActionResult)
- android-studio - 无法在 android studio 中使用图像资产添加图像
- android - 我是否必须始终从片段/活动中调用视图模型中的方法?
- php - 获得经纬度