将 YAML 转换为 JSON 时出错:yaml:第 21 行:找到违反缩进的制表符

error converting YAML to JSON: yaml: line 21: found a tab character that violate indentation

获取以下 deployment.yaml 的错误:

admin@ip-172-20-58-79:~/kubernetes-prometheus/kube-state-metrics-configs$ kubectl apply -f deployment.yaml

error: error converting YAML to JSON: yaml: line 21: found a tab character that violate indentation

admin@ip-172-20-58-79:~/kubernetes-prometheus/kube-state-metrics-configs$猫deployment.yaml

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/name: kube-state-metrics
    app.kubernetes.io/version: v1.8.0
  name: kube-state-metrics
  namespace: kube-system
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: kube-state-metrics
  template:
    metadata:
      labels:
        app.kubernetes.io/name: kube-state-metrics
        app.kubernetes.io/version: v1.8.0
    spec:
      containers:
      - image: quay.io/coreos/kube-state-metrics:v1.8.0
        imagePullPolicy: Always
        name: kube-state-metrics
        args:
        - --kubelet-insecure-tls
        - --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
        livenessProbe:
          httpGet:
            path: /healthz
            port: 8080
          initialDelaySeconds: 5
          timeoutSeconds: 5
        name: kube-state-metrics
        ports:
        - containerPort: 8080
          name: http-metrics
        - containerPort: 8081
          name: telemetry
        readinessProbe:
          httpGet:
            path: /
            port: 8081
          initialDelaySeconds: 5
          timeoutSeconds: 5
      nodeSelector:
        kubernetes.io/os: linux
      serviceAccountName: kube-state-metrics

更新 1: admin@ip-172-20-58-79:~$ kubectl 描述节点

https://pastebin.com/bectdNes

更新 2:根据 Arghya Sadhu 的建议添加了 nodeSelector

admin@ip-172-20-58-79:~/kubernetes-prometheus/kube-state-metrics-configs$ kubectl edit deploy kube-state-metrics -n kube-system

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "6"
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app.kubernetes.io/name":"kube-state-metrics","app.kubernetes.io/version":"v1.8.0"},"name":"kube-state-metrics","namespace":"kube-system"},"spec":{"replicas":1,"selector":{"matchLabels":{"app.kubernetes.io/name":"kube-state-metrics"}},"template":{"metadata":{"labels":{"app.kubernetes.io/name":"kube-state-metrics","app.kubernetes.io/version":"v1.8.0"}},"spec":{"containers":[{"args":["--kubelet-insecure-tls","--kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname"],"image":"quay.io/coreos/kube-state-metrics:v1.8.0","imagePullPolicy":"Always","livenessProbe":{"httpGet":{"path":"/healthz","port":8080},"initialDelaySeconds":5,"timeoutSeconds":5},"name":"kube-state-metrics","ports":[{"containerPort":8080,"name":"http-metrics"},{"containerPort":8081,"name":"telemetry"}],"readinessProbe":{"httpGet":{"path":"/","port":8081},"initialDelaySeconds":5,"timeoutSeconds":5}}],"nodeSelector":{"kubernetes.io/os":"linux"},"hostNetwork":true,"nodeSelector":{"node-role.kubernetes.io/master":""},"tolerations":[{"effect":"NoSchedule","key":"node-role.kubernetes.io/master","serviceAccountName":"kube-state-metrics"}}}}
  creationTimestamp: 2020-01-10T05:33:13Z
  generation: 12
  labels:
    app.kubernetes.io/name: kube-state-metrics
    app.kubernetes.io/version: v1.9.2
  name: kube-state-metrics
  namespace: kube-system
  resourceVersion: "178997153"
  selfLink: /apis/extensions/v1beta1/namespaces/kube-system/deployments/kube-state-metrics
  uid: b20aa645-336a-11ea-9618-0607d7cb72ed
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 2
  selector:
    matchLabels:
      app.kubernetes.io/name: kube-state-metrics
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app.kubernetes.io/name: kube-state-metrics
        app.kubernetes.io/version: v1.8.0
    spec:
      containers:
      - args:
        - --kubelet-insecure-tls
        - --kubelet-preferred-address-types=InternalIP
        image: quay.io/coreos/kube-state-metrics:v1.8.0
        imagePullPolicy: Always
        livenessProbe:
          failureThreshold: 3
          httpGet:
            path: /healthz
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 5
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 5
        name: kube-state-metrics
        ports:
        - containerPort: 8080
          hostPort: 8080
          name: http-metrics
          protocol: TCP
        - containerPort: 8081
          hostPort: 8081
          name: telemetry
          protocol: TCP
        readinessProbe:
          failureThreshold: 3
          httpGet:
            path: /
            port: 8081
            scheme: HTTP
          initialDelaySeconds: 5
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 5
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      hostNetwork: true
      nodeName: ip-172-20-58-72.us-west-1.compute.internal
      nodeSelector:
        node-role.kubernetes.io/master: ""
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      serviceAccount: kube-state-metrics
      serviceAccountName: kube-state-metrics
      terminationGracePeriodSeconds: 30
status:
  conditions:
  - lastTransitionTime: 2020-01-16T03:07:35Z
    lastUpdateTime: 2020-01-16T03:07:35Z
    message: Deployment does not have minimum availability.
    reason: MinimumReplicasUnavailable
    status: "False"
    type: Available
  - lastTransitionTime: 2020-01-15T07:41:48Z
    lastUpdateTime: 2020-01-16T04:15:14Z
    message: ReplicaSet "kube-state-metrics-5fdf7fb4fc" is progressing.
    reason: ReplicaSetUpdated
    status: "True"
    type: Progressing
  observedGeneration: 12
  replicas: 2
  unavailableReplicas: 2
  updatedReplicas: 1

出现以下错误:

Events:
  Type     Reason            Age   From                                                 Message
  ----     ------            ----  ----                                                 -------
  Warning  PodFitsHostPorts  21s   kubelet, ip-172-20-58-72.us-west-1.compute.internal  Predicate PodFitsHostPorts failed

更改端口后现在得到:

Events:
  Type     Reason             Age   From                                                 Message
  ----     ------             ----  ----                                                 -------
  Warning  MatchNodeSelector  11s   kubelet, ip-172-20-58-72.us-west-1.compute.internal  Predicate MatchNodeSelector failed

我尝试了 yaml,它给了我这个错误

error: unable to recognize "kube-state-metrics.yaml": no matches for kind "Deployment" in version "apps/v1beta1"

我将 apps/v1beta1 更改为 apps/v1 来解决它,但我没有收到您报告的错误。

这是我使用的yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/name: kube-state-metrics
    app.kubernetes.io/version: v1.8.0
  name: kube-state-metrics
  namespace: kube-system
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: kube-state-metrics
  template:
    metadata:
      labels:
        app.kubernetes.io/name: kube-state-metrics
        app.kubernetes.io/version: v1.8.0
    spec:
      containers:
      - image: quay.io/coreos/kube-state-metrics:v1.8.0
        imagePullPolicy: Always
        name: kube-state-metrics
        args:
        - --kubelet-insecure-tls
        - --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
        livenessProbe:
          httpGet:
            path: /healthz
            port: 8080
          initialDelaySeconds: 5
          timeoutSeconds: 5
        name: kube-state-metrics
        ports:
        - containerPort: 8080
          name: http-metrics
        - containerPort: 8081
          name: telemetry
        readinessProbe:
          httpGet:
            path: /
            port: 8081
          initialDelaySeconds: 5
          timeoutSeconds: 5
      nodeSelector:
        kubernetes.io/os: linux
      serviceAccountName: kube-state-metrics

我看不出给定的 yaml 有问题。我检查并部署成功。但是,仔细检查第 21 行没有使用制表符进行缩进,然后重新部署相同的 yaml。

也尝试在 v1.9.2 中部署 yaml

 apiVersion: apps/v1
 kind: Deployment
 metadata:
   labels:
     app.kubernetes.io/name: kube-state-metrics
     app.kubernetes.io/version: v1.9.2
   name: kube-state-metrics
   namespace: kube-system
 spec:
   replicas: 1
   selector:
     matchLabels:
       app.kubernetes.io/name: kube-state-metrics
   template:
     metadata:
       labels:
         app.kubernetes.io/name: kube-state-metrics
         app.kubernetes.io/version: v1.9.2
     spec:
       containers:
       - image: quay.io/coreos/kube-state-metrics:v1.9.2
         livenessProbe:
           httpGet:
             path: /healthz
             port: 8080
           initialDelaySeconds: 5
           timeoutSeconds: 5
         name: kube-state-metrics
         ports:
         - containerPort: 8080
           name: http-metrics
         - containerPort: 8081
           name: telemetry
         readinessProbe:
           httpGet:
             path: /
             port: 8081
           initialDelaySeconds: 5
           timeoutSeconds: 5
       nodeSelector:
         kubernetes.io/os: linux
       serviceAccountName: kube-state-metrics