由于部署清单问题,无法安装 helm

Har*_*kat 4 kubernetes kubernetes-helm

在努力表演的同时helm install

错误:无法从发布清单构建 kubernetes 对象:[无法识别“”:版本“extensions/v1beta1”中类型“服务”没有匹配项,验证“”时出错:验证数据时出错:ValidationError(Deployment.spec):缺失io.k8s.api.apps.v1.DeploymentSpec 中的必填字段“选择器”]

我的service.yaml看起来像下面

apiVersion: extensions/v1beta1
kind: Service
metadata:
  name: helm-xxx-helper-api
spec:
  type: NodePort
  ports:
    - nodePort: 31235
      port: 80
      targetPort: 8080
  selector:
     app: helm-xxx-helper
Run Code Online (Sandbox Code Playgroud)

我的deployment.yaml

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: helm-xxx-helper
spec:
  replicas: 2
  selector:
    matchLabels:
    name: helm-xxx-helper
  template:
    metadata:
      labels:
        app: helm-xxx-helper
    spec:
      containers:
      - name: helm-xxx-helper
        image: xxxxxxxxx:5001/devops/xxx-helper:latest
        imagePullPolicy: Always
        env:
          - name: XXX_STAGE
            value: "DEV"
        ports:
        - containerPort: 8080
Run Code Online (Sandbox Code Playgroud)

这里可能有什么问题?

Pjo*_*erS 7

当您收到此错误时,这意味着您正在使用 Kubernetes 1.16 或更高版本。

问题 1 - 与Service

在此版本中,许多内容apiVersion都已更改(部署、StatefulSet、服务)。更多详情可在这找到。

在 Kubernetes 1.16 中,您需要使用apiVersion: v1for service. 否则你会收到类似的错误

error: unable to recognize "STDIN": no matches for kind "Service" in version "extensions/v1beta1"
error: unable to recognize "STDIN": no matches for kind "Service" in version "extensions/v1"
error: unable to recognize "STDIN": no matches for kind "Service" in version "apps/v1"
Run Code Online (Sandbox Code Playgroud)

问题 2 - 与Deployment.

  • spec.selector.matchLabels不包含类似的值name。您需要使用来自 的值labels。因此,在这种情况下,name: helm-xxx-helper您不需要使用app: helm-xxx-helper,否则您将收到如下错误:
The Deployment "helm-xxx-helper" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"app":"helm-xxx-helper"}: `selector` does not match template `labels`
Run Code Online (Sandbox Code Playgroud)
  • YAML 格式错误。在你的代码中你有
...
selector:
  matchLabels:
  name: helm-xxx-helper
...
Run Code Online (Sandbox Code Playgroud)

的值matchLabels应低于第三个字母 (t)。另外,正如我在上一点中提到的,您需要更改nameapp

具有正确值的正确格式matchLables

...
selector:
  matchLabels:
    app: helm-xxx-helper
...
Run Code Online (Sandbox Code Playgroud)

您可以在此处Labels阅读有关和 的内容。Selectors

正如您所提到的HELM,您需要更改Kubernetes version为 1.16 之前的版本或更改目录apiVersion中的每个对象 YAML template。此前已有类似案例。请检查此线程以获取更多信息。

在两个 YAML 下方将创建ServiceDeployment. 在 Kubernetes 1.16.1 上测试。

apiVersion: v1
kind: Service
metadata:
  name: helm-xxx-helper-api
spec:
  type: NodePort
  ports:
    - nodePort: 31235
      port: 80
      targetPort: 8080
  selector:
    app: helm-xxx-helper
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: helm-xxx-helper
spec:
  replicas: 2
  selector:
    matchLabels:
      app: helm-xxx-helper
  template:
    metadata:
      labels:
        app: helm-xxx-helper
    spec:
      containers:
      - name: helm-xxx-helper
        image: nginx # As I dont have your image ive put nginx
        imagePullPolicy: Always
        env:
          - name: XXX_STAGE
            value: "DEV"
        ports:
        - containerPort: 8080
Run Code Online (Sandbox Code Playgroud)