Error in Create Cronjobs in Kubernetes

Today, I am doing a lab named “Create Cronjobs in Kubernetes” and got an error… and I tried a lot to debug that error but can’t figure out where i am wrong

commands I used are simple:
1. vi /tmp/cron.yml
and in Editor i added a script:-

apiVersion:apps/v1

kind: CronJob

metadata:

name: xfusion

spec:

schedule: “*/4 * * * *”

jobTemplate:

spec:

  template:

    spec:

      containers:

        - name: cron-xfusion

          image: nginx:latest

          command:

            - /bin/sh

            - -c

            - echo Welcome to xfusioncorp!

      restartPolicy: OnFailure

and after running the command kubectl create -f /tmp/cron.yml… i am getting the error
“error: error parsing /tmp/cron.yml: error converting YAML to JSON: yaml: line 3: mapping values are not allowed in this context”

Please check the script and tell me if i had done some mistake.
Thank you in Advance :blush:

@Sauron
@carlos140
@mmkmou

@Yash-Varma ,

i am getting the error
“error: error parsing /tmp/cron.yml: error converting YAML to JSON: yaml: line 3: mapping values are not allowed in this context”

Does that not give you enough clue already?

When asking for help please use proper Markdown (you know what is Markdown right?) to paste code so formatting is preserve. If you paste as text and formatting is lost, I have no idea if your code is incorrect in the first place or pasting as text cause your formatting to be lost. You need to help us so we can then help you.

As I can see in your code, you use wrong api for a cronjob.
Code from my notes for this task:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: datacenter
  labels:
    nginx: latest
spec:
  schedule: "*/8 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: cron-datacenter
            image: nginx:latest
            imagePullPolicy: IfNotPresent
            command:
            - /bin/sh
            - -c
            - echo Welcome to xfusioncorp!
          restartPolicy: OnFailure

In my code I use batch/v1 api. Try to do the same.

Ok i will keep that in mind from next time…!!

Thanks a lot bro…but already done i had figured out later that i did just a silly mistake