首页 > 解决方案 > 将 YAML 转换为 JSON 时出错:yaml:第 13 行:未找到预期的密钥

问题描述

我正在尝试部署此 yaml 以为 6 个不同的 grok-exporters 部署创建服务,每当我尝试部署此清单时,都会出现以下错误:

error converting YAML to JSON: yaml: line 13: did not find expected key and i can't figure out the issue 

yaml 清单:

apiVersion: v1
kind: Service
metadata:
  name: grok-exporter-centralized-service
spec:
    type: ClusterIP
    ports:
    - name: cluster-autoscaler-grok-exporter-metrics
      protocol: TCP
      targetPort: 9144
      port: 9148
    selector:
      app: cluster-autoscaler-grok-exporter
    - name: evicted-pods-grok-exporter
      protocol: TCP
      targetPort: 9144
      port: 9149
    selector:
      app: evicted-pods-grok-exporter
    - name: flux-hr-grok-exporter
      protocol: TCP
      targetPort: 9144
      port: 9144
    selector:
      app: flux-hr-grok-exporter
    - name: flux-system-grok-exporter
      protocol: TCP
      targetPort: 9144
      port: 9145
    selector:
      app: flux-system-grok-exporter
    - name: ha-proxy-grok-exporter
      protocol: TCP
      targetPort: 9144
      port: 9146
    selector:
      app: ha-proxy-grok-exporter
    - name: spot-requests-grok-exporter
      protocol: TCP
      targetPort: 9144
      port: 9147
    selector:
      app: spot-requests-grok-exporter

我在这里检查了类似的问题,但不幸的是,我无法让这个脚本工作,所以我非常感谢你的帮助。

谢谢大家!

标签: kubernetesyamlkubernetes-service

解决方案


您的问题是spec.selector.app每个spec.ports. 应该只有一个选择器,ports 数组应该不间断地列出所有端口

apiVersion: v1
kind: Service
metadata:
  name: grok-exporter-centralized-service
spec:
    type: ClusterIP
    ports:
    - name: cluster-autoscaler-grok-exporter-metrics
      protocol: TCP
      targetPort: 9144
      port: 9148
    - name: evicted-pods-grok-exporter
      protocol: TCP
      targetPort: 9144
      port: 9149
    - name: flux-hr-grok-exporter
      protocol: TCP
      targetPort: 9144
      port: 9144
    - name: flux-system-grok-exporter
      protocol: TCP
      targetPort: 9144
      port: 9145
    - name: ha-proxy-grok-exporter
      protocol: TCP
      targetPort: 9144
      port: 9146
    - name: spot-requests-grok-exporter
      protocol: TCP
      targetPort: 9144
      port: 9147
    selector:
      app: spot-requests-grok-exporter

第二步:假设我们想要一个指向多个出口商的单一服务,由不同的部署驱动。并且所有这些出口商都会在同一个端口上侦听,9144.

除了在您的部署中配置的当前标签之外,我们还将添加一个新标签,在所有这些导出器/部署之间共享。说foo=bar

现在,我可以创建一个“导出器”服务,其端点将针对我的所有导出器端口:

apiVersion: v1
kind: Service
metadata:
  name: generic-exporter
spec:
    type: ClusterIP
    ports:
    - name: generic-exporter
      protocol: TCP
      targetPort: 9144
      port: 9144
    selector:
      foo: bar

推荐阅读