部署YAML文件时出现Kubernetes错误

时间:2020-03-17 10:50:46

标签: kubernetes

我有以下文件,用于在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文件看不到任何错误。有什么我想念的吗?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

正如我在评论中提到的,您遇到匹配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