Search code examples
kuberneteskubectlkubernetes-deployment

What is the best way to verify a deployment.yaml kubernetes file?


I have the following deployment.yaml file in Kuberentes:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: basic-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: basic
    spec:
      containers:
      - name: basic
        image: nginx
        volumeMounts:
        - name: config-volume
          mountPath: /etc/nginx/conf.d
      volumes:
      - name: config-volume
        configMap:
          name: basic-config

I am not sure how I can fix the following error when I run kubectl create -f basic-deployment.yaml:

The Deployment "basic-deployment" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"app":"basic"}: selector does not match template labels


Solution

  • apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: basic-deployment
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: basic
      template:
        metadata:
          labels:
            app: basic
        spec:
          containers:
          - name: basic
            image: nginx
            volumeMounts:
            - name: config-volume
              mountPath: /etc/nginx/conf.d
          volumes:
          - name: config-volume
            configMap:
              name: basic-config
    

    Basically, the selector match label in your deployment spec needs to match a label in your template. In your case, you have app: nginx as a matching label for the selector and you have app: basic in your template, so no match.

    You would have to have something either one app: nginx or app: basic on both so that there is a match.