Required some Help from community to complete a Task

Hey Folks, Reaching out everyone having some Expertise in K8s and related stuff, need some help in doing a task where i need to Configurea k8s Cron job which collects metrics from the cluster via Node-Exporter.

Let me know if some one is open to help.

TIA

Which part are you stuck on?

not able to create the right Cronjob which runs and fetches the metrics. the pods that the job cretes fails, hence stucked in the initial stage only.

the Problem statement is

Problem statement

Create a Kubernetes cron job that pulls node metrics like (CPU, Memory, Disk usage) and stores them in a file.

  • Every time the cron runs, it should create a new file. The filename should have the current timestamp.
  • By default, cron should be minute, but it should be configurable with the slightest changes to code.
  • Choose a tool of your choice to collect and expose metrics. Preferrable is node exporter.
  • The instances involved here are Kubernetes nodes themselves.

Expected Output:

  • The actual program code pulls the metrics and writes them to a file.
  • The Dockerfile to containerize the code.
  • Kubernetes YAML or HELM Chart.
  • A README file explaining the design, deployment and other details.
  • If possible, record a short video to demo the output with the code files.

Writing cron job is easy but writing the code to pull the metrics is some what difficult. Which metrics you are trying to pull, hitting /metrics endpoint and storing the output in a file or computation of cpu usage, disk usage and memory usage ?

Even i am also assigned with the same task

cronjob.yaml

apiVersion: batch/v1
kind: CronJob
metadata:
name: node-metrics-job
spec:
schedule: “*/1 * * * *” # Run every 5 minutes (adjust per your preference)
jobTemplate:
spec:
template:
spec:
containers:
- name: node-metrics-container
image: bitnami/metrics-server:latest
command:
- “/bin/sh”
- “-c”
args:
- |

            kubectl top nodes | jq '.items[] | {node: .metadata.name, cpu: .usage.cpu, memory: .usage.memory}' > /mnt/metrics/node_metrics_$(date +"%Y-%m-%d_%H-%M-%S").json

        volumeMounts:
        - name: metrics-volume
          mountPath: /mnt/metrics
      restartPolicy: OnFailure
      volumes:
      - name: metrics-volume
        hostPath:
          path: /mnt/metrics
          type: DirectoryOrCreate

pod.yaml

apiVersion: v1
kind: Pod
metadata:
name: busybox-pv-pod
spec:
containers:

  • name: busybox
    image: busybox
    command:
    • “/bin/sh”
    • “-c”
    • “while true; do echo ‘Hello from BusyBox’; sleep 3600; done”
      volumeMounts:
    • name: my-pv-storage
      mountPath: /mnt/metrics
      volumes:
  • name: my-pv-storage
    hostPath:
    path: /mnt/metrics
    type: DirectoryOrCreate

pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
name: metrics-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: manual
hostPath:
path: “/mnt/metrics”