skip to Main Content

I am following Cloud Guru K8S course and have issues with the template they provided. I can’t see what’s wrong.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: blue-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: bluegreen-test
      color: blue
  template:
    metadata:
      labels:
        app: bluegreen-test
        color: blue
    spec:
      containers:
      - name: nginx
        image: linuxacademycontent/ckad-nginx:blue
        ports:
        - containerPort: 80

When I run

kubectl apply -f my-deployment.yml

I get

error: error parsing blue-deployment.yml: error converting YAML to JSON: yaml: line 4: found character that cannot start any token

What’s wrong with this template? It’s nearly identical to the official example deployment definition https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#creating-a-deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

2

Answers


  1. Your template is correct, it’s just YAML that’s complaining. YAML doesn’t accept tabs, only 2 spaces. If you copied + pasted, it could have caused a discrepancy. If you want to stay on terminal, vim my-deployment.yml and make sure each "tab" is 2 spaces. This is quite time-consuming especially in vim so the alternative is to use a text editor like Sublime Text to update it.

    Login or Signup to reply.
  2. Nothing wrong with the yaml syntax. I just deployed it and it works. see below screenshot. Suggest you to use vi editor and add the manifest and then try. It should work

    enter image description here

    You can validate the syntax at the below link as well

    https://validkube.com/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search