简体   繁体   中英

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?

Your container to run a kubectl command cant use the default service account. You need to create a specific one.

Here is a nice way to do that: https://itnext.io/running-kubectl-commands-from-within-a-pod-b303e8176088

But redeploying every 30 minutes sounds to me well known. If you get in memory or cpu issues, consider to add a resources section in sour deployment.

Something like so:

spec:
  containers:
  - name: app
    image: images.my-company.example/app:v4
    resources:
      requests:
        memory: "64Mi"
        cpu: "250m"
      limits:
        memory: "128Mi"
        cpu: "1500m"

According to your needs also a livnessProbe, if you havent that already, can be a solution.

There is a workaround available for this issue, you can try using kube.netes jobs along with kube.netes scheduler for executing your commands inside the pods at your desired time intervals or you can even trigger these jobs when a certain event happens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM