Help Needed with Kubernetes CronJob Question

Hey everyone,

I recently encountered a question related to Kubernetes CronJobs and would appreciate some insights from the community.

Question:

For this question, please set the context to cluster1 by running:

kubectl config use-context cluster1

In the ckad-job namespace, create a cronjob named simple-python-job to run every 30 minutes to list all the running processes inside a container that used python image (the command needs to be run in a shell).

In Unix-based operating systems, ps -eaf can be use to list all the running processes.

My Answer:
apiVersion: v1
items:

  • apiVersion: batch/v1
    kind: CronJob
    metadata:
    annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
    {“apiVersion”:“batch/v1”,“kind”:“CronJob”,“metadata”:{“annotations”:{},“creationTimestamp”:“2024-05-06T02:39:40Z”,“generation”:2,“name”:“simple-python-job”,“namespace”:“ckad-job”,“resourceVersion”:“2011”,“uid”:“df42fdca-65bb-4d6b-85df-35ecb1d69ef7”},“spec”:{“concurrencyPolicy”:“Allow”,“failedJobsHistoryLimit”:1,“jobTemplate”:{“metadata”:{“creationTimestamp”:null,“name”:“simple-python-job”},“spec”:{“template”:{“metadata”:{“creationTimestamp”:null},“spec”:{“containers”:[{“args”:[“-c”,“ps -eaf”],“command”:[“/bin/sh”],“image”:“python”,“imagePullPolicy”:“Always”,“name”:“simple-python-job”,“resources”:{},“terminationMessagePath”:“/dev/termination-log”,“terminationMessagePolicy”:“File”}],“dnsPolicy”:“ClusterFirst”,“restartPolicy”:“OnFailure”,“schedulerName”:“default-scheduler”,“securityContext”:{},“terminationGracePeriodSeconds”:30}}}},“schedule”:"*/30 * * * ",“successfulJobsHistoryLimit”:3,“suspend”:false},“status”:{“active”:[{“apiVersion”:“batch/v1”,“kind”:“Job”,“name”:“simple-python-job-28582720”,“namespace”:“ckad-job”,“resourceVersion”:“1609”,“uid”:“04c8389b-f080-47d9-b833-ba5391ee6dfd”},{“apiVersion”:“batch/v1”,“kind”:“Job”,“name”:“simple-python-job-28582721”,“namespace”:“ckad-job”,“resourceVersion”:“1669”,“uid”:“a039e141-2bf7-4f9a-a7eb-10656c22f77d”},{“apiVersion”:“batch/v1”,“kind”:“Job”,“name”:“simple-python-job-28582724”,“namespace”:“ckad-job”,“resourceVersion”:“2009”,“uid”:“0242d4d8-21e1-4bd3-9aba-c250a1f89188”}],“lastScheduleTime”:“2024-05-06T02:44:00Z”,“lastSuccessfulTime”:“2024-05-06T02:43:05Z”}}
    creationTimestamp: “2024-05-06T02:44:46Z”
    generation: 1
    name: simple-python-job
    namespace: ckad-job
    resourceVersion: “5423”
    uid: 3dccc20c-ff3f-4df4-8b20-8fb753b88844
    spec:
    concurrencyPolicy: Allow
    failedJobsHistoryLimit: 1
    jobTemplate:
    metadata:
    creationTimestamp: null
    name: simple-python-job
    spec:
    template:
    metadata:
    creationTimestamp: null
    spec:
    containers:
    - args:
    - -c
    - ps -eaf
    command:
    - /bin/sh
    image: python
    imagePullPolicy: Always
    name: simple-python-job
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    dnsPolicy: ClusterFirst
    restartPolicy: OnFailure
    schedulerName: default-scheduler
    securityContext: {}
    terminationGracePeriodSeconds: 30
    schedule: '
    /30 * * * *’
    successfulJobsHistoryLimit: 3
    suspend: false
    status:
    lastScheduleTime: “2024-05-06T04:00:00Z”
    lastSuccessfulTime: “2024-05-06T04:00:35Z”
    kind: List
    metadata:
    resourceVersion: “”
    selfLink: “”

I crossed checked

student-node ~ ➜ k get pods -n ckad-job
NAME READY STATUS RESTARTS AGE
simple-python-job-28582740-4h22z 0/1 Completed 0 84m
simple-python-job-28582770-8qb5x 0/1 Completed 0 54m
simple-python-job-28582800-p4p6b 0/1 Completed 0 24m

student-node ~ ➜ k logs -f simple-python-job-28582800-p4p6b -n ckad-job
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 04:00 ? 00:00:00 /bin/sh -c ps -eaf
root 57 1 0 04:00 ? 00:00:00 ps -eaf

but this is wrong in while check
this solution by kodekloud
Create a YAML file with the content as below:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: simple-python-job
  namespace: ckad-job
spec:
  schedule: "*/30 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: simple-python-job
            image: python
            imagePullPolicy: IfNotPresent
            command:
            - /bin/sh
            - -c
            - ps -eaf
          restartPolicy: OnFailure

Then use kubectl apply -f file_name.yaml to create the required object.

failed with this


I’m not sure what’s missing or if there’s something wrong with my approach.
or the checker is not checking properly

Your code is kind of a mess –

use code blocks:
  - so we
  - can read things

but I think the problem is that you are using both args and command. You don’t need to do this, and I think that this is why the grader is getting confused. Just do

            command:
            - /bin/sh
            - -c
            - ps -eaf

instead.

but this is same thing
also endgoal is same

Still, you don’t need to use both. I’m guessing that the grader makes a choice and is testing the content of the command block.

but if we use both or not
at the end command is executed
/bin/sh -c "ps -eaf"

@mmumshad can you please take a look to this
Thanks

I found your question in the CKAD Ultimate Mock Exams (Design #11/22). The key thing here is how the question is worded.

In the ckad-job namespace, create a cronjob named simple-python-job to run every 30 minutes to list all the running processes inside a container that used python image (the command needs to be run in a shell).

When you are working a problem like this one, you need to look for clues like this. Here they say you need to run a command – so this typically means that you need to use a command block.

The easiest way to get a valid cronjob is to use the imperative command k create cronjob. I tried this version of it, looking up the syntax with k create cj --help:

k -n ckad-job create cj simple-python-job --image python --schedule '*/30 * * * *'  --dry-run=client -o yaml -- sh -c "ps -eaf" > cj.yaml

This gives me something that looks pretty plausible:

student-node ~ ➜  cat cj.yaml 
apiVersion: batch/v1
kind: CronJob
metadata:
  creationTimestamp: null
  name: simple-python-job
  namespace: ckad-job
spec:
  jobTemplate:
    metadata:
      creationTimestamp: null
      name: simple-python-job
    spec:
      template:
        metadata:
          creationTimestamp: null
        spec:
          containers:
          - command:
            - sh
            - -c
            - ps -eaf
            image: python
            name: simple-python-job
            resources: {}
          restartPolicy: OnFailure
  schedule: '*/30 * * * *'
status: {}

When I press End Exam, this passes. It’s a little different than the version posted in the exam – I have sh, the standard solution has /bin/sh – but the grader still likes my answer. Notice that the imperative command automatically used a command block as well. This is standard; if you tried using both command and args in a real exam, the odds are good that you would not get full score.