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