joesan
joesan

Reputation: 15345

Kubernetes Error With Deployment YAML File

I have the following file using which I'm setting up Prometheus on my Kubernetes cluster:

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: {}

When I apply this to my Kubernetes cluster, I see the following error:

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`"

I could not see anything wrong with my yaml file. Is there something that I'm missing?

Upvotes: 1

Views: 1238

Answers (2)

PjoterS
PjoterS

Reputation: 14084

As I mentioned in comments, You have issue with matching labels.

In spec.selector.matchLabels you have name: prometheus-server and in spec.template.medatada.labels you have app: prometheus-server. Values there need to be the same. Below what I get when used your 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`

And output when I used below yaml with the same labels:

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

More detailed info about selectors/labels can be found in Official Kubernetes docs.

Upvotes: 2

Arghya Sadhu
Arghya Sadhu

Reputation: 44549

There is a mismatch between the label in selector(name: prometheus-server) and metadata (app: prometheus-server). Below should work.

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

Upvotes: 1

Related Questions