Home > Software design >  Error when trying to create a deployment using YAML: Deployment in version "v1" cannot be
Error when trying to create a deployment using YAML: Deployment in version "v1" cannot be

Time:11-27

I am new to K8S and trying to create a deployment using the below YAML file.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: building-apps-deploy
  labels:
    app: kubeacademy
spec:
  replicas: 2
  selector:
    matchlabels:
      app: kubeacademy
  template:
    metadata:
      labels:
        app: kubeacademy
    spec:
      containers:
        - name: building-apps-containers
          image: 'lander2k2/building-apps:0.1'

I had the file validated by 3 online validator websites and all is coming back ok but when I run the command - kubectl apply -f deployment_yam27112022.yml, it is throwing the below error: Error from server (BadRequest): error when creating "deployment_yam27112022.yml": Deployment in version "v1" cannot be handled as a Deployment: strict decoding error: unknown field "spec.selector.matchlabels"

I understand that the spec.selector.matchlables has some issue but cannot pinpoint the same.

Request the community help to resolve this situation and let me know if there are any online resources for validating the YAML files or generating ones which are recommended?

Additional info:

the pod was created successfully using below YAML file:

apiVersion: v1              
kind: Pod
metadata:
    name: building-apps-pod
    labels:
        app: kubeacademy
spec:
    containers:
    - name: building-apps-container
      image: lander2k2/building-apps:0.1

command used for pod creation: kubectl apply -f CreatePod22112022.yml

and the cluster was created using the command kind create cluster --name demo271122

To conclude my question: was trying to create a deployment post creation of cluster and pod. the deployment command is failing again and again.

I have also gone through the questions with same / similar error messages on stack overflow but have not been able to resolve the issue - some suggestions to earlier question was to format the YAML file properly but have already tried the same using Notepad and online yaml validator websites.

Thank you in advance for your help.

Best Regards

CodePudding user response:

Your error message says:

unknown field "spec.selector.matchlabels"

In your deployment, we can see the following:

selector:
  matchlabels:
    app: kubeacademy

You meant to do this instead:

selector:
  matchLabels:
    app: kubeacademy
  • Related