Famokunwa Toluwani
Famokunwa Toluwani

Reputation: 513

error when creating "deployment.yaml": Deployment in version "v1" cannot be handled as a Deployment

I am new to DevOps. I wrote a deployment.yaml file for a Kubernetes cluster I just created on Digital Oceans. Creating the deployment keeps bringing up errors that I can't decode for now. This is just a test deployment in preparation for the migration of my company's web apps to kubernetes.

I tried editing the content of the deployment to look like conventional examples I've found. I can't even get this simple example to work. You may find the deployment.yaml content below.

---
kind: Deployment
apiVersion: apps/v1
metadata:
  name: testit-01-deployment
spec:
  replicas: 4
  #number of replicas generated
  selector:
    #assigns labels to the pods for future selection
    matchLabels:
      app: testit
      version: v01
  template:
    metadata:
      Labels:
        app: testit
        version: v01
    spec:
      containers:
        -name: testit-container
        image: teejayfamo/testit
        ports:
          -containerPort: 80

I ran this line on cmd in the folder container:

kubectl apply -f deployment.yaml --validate=false

Error from server (BadRequest): error when creating "deployment.yaml": Deployment in version "v1" cannot be handled as a Deployment: v1.Deployment.Spec: v1.DeploymentSpec.Template: v1.PodTemplateSpec.Spec: v1.PodSpec.Containers: []v1.Container: decode slice: expect [ or n, but found {, error found in #10 byte of ...|tainers":{"-name":"t|..., bigger context ...|:"testit","version":"v01"}},"spec":{"containers":{"-name":"testit-container","image":"teejayfamo/tes|...

I couldn't even get any information on this from my search. I can't just get the deployment created. Pls, who understands and can put me through?

Upvotes: 51

Views: 132319

Answers (5)

M3RS
M3RS

Reputation: 7550

I recommend adding the verbose flag to get detailed debug information.

kubectl apply -f deployment.yaml -v=10

Upvotes: 0

Pankaj
Pankaj

Reputation: 2708

Since this is the top result of the search, I thought I should add another case when this can occur. In my case, it was coming because there was no double quote on numeric env. var. Log did provide a subtle hint, but it was not very helpful.

Log

..., bigger context ...|c-server-service"},{"name":"SERVER_PORT","value":80}]

Env variable - the value of SERVER_PORT needs to be in double quote.

env:
  - name: SERVER_HOST
    value: grpc-server-service
  - name: SERVER_PORT
    value: "80"

Kubernetes issue for reference.

Upvotes: 57

nucc1
nucc1

Reputation: 374

Just to add, in my case, I turned up this result, but ultimately, the problem I had was that I had defined the same environment variable twice

Example:

...
env:
 - name: FOO
   value: "bar"
 - name: FOO
   value: "bar"

Upvotes: 0

Garvit
Garvit

Reputation: 400

In short -> Syntax Error

The mistake I was doing:

ports:
  -containerPort: 8081

the above snippet causing the following error:

Error from server (BadRequest): error when creating ".\\deployment.yml": Deployment in version "v1" cannot be handled as a Deployment: json: cannot unmarshal object into Go struct field Container.spec.template.spec.containers.ports of type []v1.ContainerPort

And this is how I corrected:

ports:
  - containerPort: 8081

so the difference was -containerPort (wrong one) and - containerPort (correct one).

Upvotes: 0

mchawre
mchawre

Reputation: 12268

There are syntax errors in your yaml file.

This should work.

---
kind: Deployment
apiVersion: apps/v1
metadata:
  name: testit-01-deployment
spec:
  replicas: 4
  #number of replicas generated
  selector:
    #assigns labels to the pods for future selection
    matchLabels:
      app: testit
      version: v01
  template:
    metadata:
      labels:
        app: testit
        version: v01
    spec:
      containers:
      - name: testit-container
        image: teejayfamo/testit
        ports:
        - containerPort: 80

The problem was:

  • Labels should be labels
  • The syntax of - name: and - containerPort were not formatted properly in spec.containers section.

Hope this helps.

Upvotes: 31

Related Questions