【问题标题】:Kubernetes Error With Deployment YAML File部署 YAML 文件时出现 Kubernetes 错误
【发布时间】:2020-03-17 19:25:46
【问题描述】:

我有以下文件用于在我的 Kubernetes 集群上设置 Prometheus:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus-deployment
  namespace: plant-simulator-monitoring
spec:
  replicas: 1
  selector:
    matchLabels:
      name: prometheus-server
  template:
    metadata:
      labels:
        app: prometheus-server
    spec:
      containers:
        - name: prometheus
          image: prom/prometheus:latest
          args:
            - "--config.file=/etc/prometheus/prometheus.yml"
            - "--storage.tsdb.path=/prometheus/"
          ports:
            - containerPort: 9090
          volumeMounts:
            - name: prometheus-config-volume
              mountPath: /etc/prometheus/
            - name: prometheus-storage-volume
              mountPath: /prometheus/
          resources:
            requests:
              memory: "512Mi"
              cpu: "500m"
            limits:
              memory: "1Gi"
              cpu: "1000m"
      volumes:
        - name: prometheus-config-volume
          configMap:
            defaultMode: 420
            name: prometheus-server-conf

        - name: prometheus-storage-volume
          emptyDir: {}

当我将它应用到我的 Kubernetes 集群时,我看到以下错误:

ts=2020-03-16T21:40:33.123641578Z caller=sync.go:165 component=daemon err="plant-simulator-monitoring:deployment/prometheus-deployment: running kubectl: The Deployment \"prometheus-deployment\" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{\"app\":\"prometheus-server\"}: `selector` does not match template `labels`"

我看不出我的 yaml 文件有什么问题。有什么我遗漏的吗?

【问题讨论】:

  • 乍一看,您似乎有错误的标签/selectros:name: prometheus-serverapp: prometheus-server。我会检查我的集群是否是根本原因。

标签: kubernetes


【解决方案1】:

正如我在 cmets 中提到的,您在匹配 labels 时遇到问题。

spec.selector.matchLabels 中有name: prometheus-server,在spec.template.medatada.labels 中有app: prometheus-server。那里的价值观必须相同。下面是我使用你的 yaml 时得到的结果:

$ kubectl apply -f deploymentoriginal.yaml
The Deployment "prometheus-deployment" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"app":"prometheus-server"}: `selector` does not match template `labels`

当我在下面使用相同标签的 yaml 时输出:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus-deployment
  namespace: plant-simulator-monitoring
spec:
  replicas: 1
  selector:
    matchLabels:
      name: prometheus-server
  template:
    metadata:
      labels:
        name: prometheus-server
    spec:
      containers:
        - name: prometheus
          image: prom/prometheus:latest
          args:
            - "--config.file=/etc/prometheus/prometheus.yml"
            - "--storage.tsdb.path=/prometheus/"
          ports:
            - containerPort: 9090
          volumeMounts:
            - name: prometheus-config-volume
              mountPath: /etc/prometheus/
            - name: prometheus-storage-volume
              mountPath: /prometheus/
          resources:
            requests:
              memory: "512Mi"
              cpu: "500m"
            limits:
              memory: "1Gi"
              cpu: "1000m"
      volumes:
        - name: prometheus-config-volume
          configMap:
            defaultMode: 420
            name: prometheus-server-conf
        - name: prometheus-storage-volume
          emptyDir: {}

$ kubectl apply -f deploymentselectors.yaml
deployment.apps/prometheus-deployment created

有关选择器/标签的更多详细信息,请参阅Official Kubernetes docs

【讨论】:

    【解决方案2】:

    选择器 (name: prometheus-server) 和元数据 (app: prometheus-server) 中的标签不匹配。下面应该可以工作。

    selector:
        matchLabels:
          app: prometheus-server
      template:
        metadata:
          labels:
            app: prometheus-server
    

    【讨论】:

      猜你喜欢
      • 2020-02-20
      • 2020-10-09
      • 1970-01-01
      • 2019-07-08
      • 2020-07-10
      • 2022-01-18
      • 2021-10-30
      • 1970-01-01
      • 2011-03-23
      相关资源
      最近更新 更多