尝试限制内存时未找到预期的密钥错误

did not find expected key error when trying to limit memory

我正在尝试通过 Kubernetes 运行 测试容器,但出现错误。下面是我的 deploy.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.22.0 (955b78124)
  creationTimestamp: null
  labels:
    io.kompose.service: nginx
  name: nginx
spec:
  containers:
      requests:
          storage: 2Gi
          cpu: 0.5
          memory: "128M"
      limits:
          cpu: 0.5
          memory: "128M" # This is the line throws error
          - type: PersistentVolumeClaim
          max:
            storage: 2Gi
          min:
            storage: 1Gi
.
.
.

当我 运行 kubectl apply -k . 时,我得到这个错误:

error: accumulating resources: accumulation err='accumulating resources from 'deploy.yaml': yaml: line 19: did not find expected key': got file 'deploy.yaml', but '/home/kus/deploy.yaml' must be a directory to be a root

我试图阅读 Kubernetes 但我不明白为什么我会收到错误消息。

编辑 1

我按照@whites11 的说法更改了我的 deploy.yaml 文件,现在它是这样的:

.
.
.
spec:
  limits:
    memory: "128Mi"
    cpu: 0.5
.
.
.

现在我收到这个错误:

resourcequota/storagequota unchanged
service/nginx configured
error: error validating ".": error validating data: ValidationError(Deployment.spec): unknown field "limits" in io.k8s.api.apps.v1.DeploymentSpec; if you choose to ignore these errors, turn validation off with --validate=false

您共享的文件不是有效的 YAML。

limits:
     cpu: 0.5
     memory: "128M" # This is the line throws error
     - type: PersistentVolumeClaim
     max:
       storage: 2Gi
     min:
       storage: 1Gi

limits字段是混合类型(hash和collection),显然是错误的。

您似乎采用了指定 pods resource limits and mixed it with the one used to limit storage consuption.

的语法

在您的部署规范中,您可能希望像这样简单地设置限制:

apiVersion: apps/v1
kind: Deployment
...
spec:
  ...
  template:
  ...
    spec:
      containers:
      - ...
        resources:
          limits:
            cpu: 0.5
            memory: "128M"

并按照 the documentation 中所述处理命名空间级别的存储限制。

您应该将 yaml 文件更改为:

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    name: nginx
spec:
  containers:
  - name: nginx
    image: nginx:alpine
    resources:
      limits:
        memory: "128Mi"
        cpu: "500m"