I am getting this specific error of error when creating ".\\deployment_test.yaml": Deployment in version "v1" cannot be handled as a Deployment

Viewed 76

I am trying create deployment using yaml file in kubernetes but facing this specific error:

Error from server (BadRequest): error when creating ".\deployment_test.yaml": Deployment in version "v1" cannot be handled as a Deployment: v1.Deployment.Spec: v1.DeploymentSpec.Template: v1.PodTemplateSpec.ObjectMeta: v1.ObjectMeta.Labels: ReadMapCB: expect { or n, but found ", error found in #10 byte of ...|"labels":"test"},"sp|...

My yaml file is as follows:

kind: Deployment
metadata:
    labels:
      environment: test
    name: agentstubpod-deployment
spec:
  replicas: 3
  selector: 
    matchLabels: 
      enviroment: test
  minReadySeconds: 10
  strategy: 
    rollingUpdate: 
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template: 
    metadata:
      labels: test
    spec: 
      containers:
      - name: agentstub
        image: some-repo:latest 
---
apiVersion: apps/v1
kind: Deployment
metadata:
    labels:
      environment: test
    name: proxystubpod-deployment
spec:
  replicas: 3
  selector: 
    matchLabels: 
      enviroment: test
  minReadySeconds: 10
  strategy: 
    rollingUpdate: 
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template: 
    metadata:
      labels: test
    spec: 
      containers:
      - name: procyservice
        image: some-repo:latest

What is wrong with this syntax? I am having a really hard time making a deployment

1 Answers

There are some misconfigurations.

  1. apiVersion is missing in the first deployment
  2. indentation below metadata is incorrect
  3. You must include the metadata.name field
  4. spec.selector.matchLabels and spec.template.metadata.labels should be matched.

Here is the corrected example:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    environment: test
    name: agentstubpod-deployment
  name: dep1
spec:
  replicas: 3
  selector:
    matchLabels:
      environment: test
  minReadySeconds: 10
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      labels:
        environment: test
    spec:
      containers:
      - name: agentstub
        image: some-repo:latest

---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    environment: test
    name: proxystubpod-deployment
  name: dep2
spec:
  replicas: 3
  selector:
    matchLabels:
      environment: test
  minReadySeconds: 10
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      labels:
        environment: test
    spec:
      containers:
      - name: procyservice
        image: some-repo:latest
Related