kubernetes – How to run kubectl commands inside docker container cron job

1.kubectl commands giving response in container terminal and getting cluster-info.

[root@cronjob-cj-m1dr3tsda-aw5rv /]# kubectl cluster-info
Kubernetes control plane is running at https://172.20.0.1:443
CoreDNS is running at https://172.20.0.1:443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

2.We have crontab running inside container and scheduled some job in crontab as below

*/30 * * * * root /usr/local/bin/scheduler_job.sh >> /var/log/scheduler_job.log

cat scheduler_job.sh
#!/bin/bash
whereis kubectl
kubectl version --client
kubectl cluster-info
kubectl get pods -n kube-system
kubectl rollout restart ds/scheduler-ds -n kube-system

Above kubectl commands output is empty

kubectl: /usr/local/bin/kubectl
Tue Dec 20 11:05:04 UTC 2022
WARNING: This version information is deprecated and will be replaced with the output from kubectl version --short.  Use --output=yaml|json to get the full version.
Client Version: version.Info{Major:"1", Minor:"26", GitVersion:"v1.26.0", GitCommit:"b46a3f887ca979b1a5d14fd39cb1af43e7e5d12d", GitTreeState:"clean", BuildDate:"2022-12-08T19:58:30Z", GoVersion:"go1.19.4", Compiler:"gc", Platform:"linux/amd64"}
Kustomize Version: v4.5.7

Crontab scheduled job script have kubectl commands, Its giving output as blank, please help how can we configure to get cluster access to crontab scheduled job that running inside container?

Read more here: Source link