【问题标题】:ValidationError: missing required field "selector" in io.k8s.api.v1.DeploymentSpecValidationError:在 io.k8s.api.v1.DeploymentSpec 中缺少必填字段“选择器”
【发布时间】:2024-01-02 16:15:01
【问题描述】:

我已经创建了 Hyper-V 机器并尝试使用 Sawtooth YAML 文件在 Minikube 上部署 Sawtooth: https://sawtooth.hyperledger.org/docs/core/nightly/master/app_developers_guide/sawtooth-kubernetes-default.yaml

我将 apiVersion 即 apiVersion: extensions/v1beta1 更改为 apiVersion: apps/v1,尽管我已经使用此命令在 Kubernetes v1.17.0 中启动了 Minikube

minikube 启动 --kubernetes-version v1.17.0

之后我无法部署服务器。命令是

kubectl apply -f sawtooth-kubernetes-default.yaml --validate=false

它显示错误“sawtooth-0”无效。

---
apiVersion: v1
kind: List

items:

- apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: sawtooth-0
  spec:
    replicas: 1
    selector:
      matchLabels:
        name: sawtooth-0
    template:
      metadata:
        labels:
          name: sawtooth-0
      spec:
        containers:
          - name: sawtooth-devmode-engine
            image: hyperledger/sawtooth-devmode-engine-rust:chime
            command:
              - bash
            args:
              - -c
              - "devmode-engine-rust -C tcp://$HOSTNAME:5050"

          - name: sawtooth-settings-tp
            image: hyperledger/sawtooth-settings-tp:chime
            command:
              - bash
            args:
              - -c
              - "settings-tp -vv -C tcp://$HOSTNAME:4004"

          - name: sawtooth-intkey-tp-python
            image: hyperledger/sawtooth-intkey-tp-python:chime
            command:
              - bash
            args:
              - -c
              - "intkey-tp-python -vv -C tcp://$HOSTNAME:4004"

          - name: sawtooth-xo-tp-python
            image: hyperledger/sawtooth-xo-tp-python:chime
            command:
              - bash
            args:
              - -c
              - "xo-tp-python -vv -C tcp://$HOSTNAME:4004"

          - name: sawtooth-validator
            image: hyperledger/sawtooth-validator:chime
            ports:
              - name: tp
                containerPort: 4004
              - name: consensus
                containerPort: 5050
              - name: validators
                containerPort: 8800
            command:
              - bash
            args:
              - -c
              - "sawadm keygen \
              && sawtooth keygen my_key \
              && sawset genesis -k /root/.sawtooth/keys/my_key.priv \
              && sawset proposal create \
                -k /root/.sawtooth/keys/my_key.priv \
                sawtooth.consensus.algorithm.name=Devmode \
                sawtooth.consensus.algorithm.version=0.1 \
                -o config.batch \
              && sawadm genesis config-genesis.batch config.batch \
              && sawtooth-validator -vv \
                  --endpoint tcp://$SAWTOOTH_0_SERVICE_HOST:8800 \
                  --bind component:tcp://eth0:4004 \
                  --bind consensus:tcp://eth0:5050 \
                  --bind network:tcp://eth0:8800"

          - name: sawtooth-rest-api
            image: hyperledger/sawtooth-rest-api:chime
            ports:
              - name: api
                containerPort: 8008
            command:
              - bash
            args:
              - -c
              - "sawtooth-rest-api -C tcp://$HOSTNAME:4004"

          - name: sawtooth-shell
            image: hyperledger/sawtooth-shell:chime
            command:
              - bash
            args:
              - -c
              - "sawtooth keygen && tail -f /dev/null"

- apiVersion: apps/v1
  kind: Service
  metadata:
    name: sawtooth-0
  spec:
    type: ClusterIP
    selector:
      name: sawtooth-0
    ports:
      - name: "4004"
        protocol: TCP
        port: 4004
        targetPort: 4004
      - name: "5050"
        protocol: TCP
        port: 5050
        targetPort: 5050
      - name: "8008"
        protocol: TCP
        port: 8008
        targetPort: 8008
      - name: "8800"
        protocol: TCP
        port: 8800
        targetPort: 8800

【问题讨论】:

  • “在此处输入图片描述”链接背后的内容是什么?通常我希望看到某种文本错误消息;你能用你得到的错误的实际文本(不是图像)替换链接吗?你能包含足够的实际 YAML 文件(不是链接)来演示错误吗?

标签: kubernetes hyperledger-sawtooth


【解决方案1】:

您需要修复您的部署yaml 文件。从您的错误消息中可以看出,Deployment.spec.selector 字段不能为空。

更新yaml(即添加spec.selector)如下所示:

  spec:
    replicas: 1
    selector:
      matchLabels:
        app.kubernetes.io/name: sawtooth-0
    template:
      metadata:
        labels:
          app.kubernetes.io/name: sawtooth-0
  • 为什么selector 字段很重要?

selector 字段定义了 Deployment 如何查找要管理的 Pod。在这种情况下,您只需选择在 Pod 模板中定义的标签 (app.kubernetes.io/name: sawtooth-0)。然而,更复杂的选择规则是可能的,只要 Pod 模板本身满足规则。

更新:

k8s 服务的apiVersionv1:

- apiVersion: v1 # Update here
  kind: Service
  metadata:
    app.kubernetes.io/name: sawtooth-0
  spec:
    type: ClusterIP
    selector:
      app.kubernetes.io/name: sawtooth-0
    ... ... ...

【讨论】:

  • 我确实在部署中添加了选择器并启动了 minikube 机器,现在错误正在运行。这是我收到的消息...........``` C:\Users\Debo>minikube status host: Running kubelet: Running apiserver: Running kubeconfig:已配置 C:\Users\Debo>kubectl apply -f sawtooth-kubernetes-default.yaml --validate=false deployment.apps/sawtooth-0 created 无法识别版本“apps/v1”中类型“服务”的不匹配项" ```
  • @debokarmakar 能否将更新后的 yaml 添加到问题正文中,以及启动 minikube 后 kubectl version --short 命令的结果?
  • C:\Users\Debo>kubectl 版本 --short 客户端版本:v1.17.0 服务器版本:v1.17.0 和我在问题中更新的 yaml 文件
  • @debokarmakar,我已经更新了我的答案,看看吧。
  • @ZufarMuhamadeev 查看recommended 标签,但它可以是任何东西。
【解决方案2】:

@Kamol 已经给出了答案


如果您仍然收到错误的一些一般可能原因:

missing required field “XXX” in YYY
  1. 检查文件顶部的apiVersion(对于部署,版本为:apps/v1 & 对于服务,它是 v1
  2. 检查“XXX”的spelling(未知字段)并检查syntax是否不正确。
  3. 再次检查kind: ...

如果您发现其他原因,请发表评论并让其他人知道:)

【讨论】:

    【解决方案3】:

    对于 api 版本 v1(以及应用程序/v1),您需要使用 app: <your lable>

    apiVersion: v1
    kind: Service
    metadata:
      name: sawtooth-0
    spec:
      selector:
        app: sawtooth-0
    

    见:https://kubernetes.io/docs/concepts/services-networking/service/

    【讨论】:

    • 不起作用。错误:io.k8s.apimachinery.pkg.apis.meta.v1.LabelSelector 中的未知字段“app”;
    最近更新 更多