Kubernetes Cron job invocation from pod

7/11/2019

Is it possible to invoke a kubernetes Cron job inside a pod . Like I have to run this job from the application running in pod .

Do I have to use kubectl inside the pod to execute the job .

Appreciate your help

-- Balakumar Ezhilmaran
kubernetes

1 Answer

7/11/2019

Use the Default Service Account to access the API server. When you create a pod, if you do not specify a service account, it is automatically assigned the default service account in the same namespace. If you get the raw json or yaml for a pod you have created (for example, kubectl get pods/ -o yaml), you can see the spec.serviceAccountName field has been automatically set.

You can access the API from inside a pod using automatically mounted service account credentials, as described in Accessing the Cluster. The API permissions of the service account depend on the authorization plugin and policy in use.

In version 1.6+, you can opt out of automounting API credentials for a service account by setting automountServiceAccountToken: false on the service account

https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/

  1. So the First task is to either grant the permission of doing what you need to create to the default service account of the pod OR create a custom service account and use it inside the pod

  2. Programatically access the API server using that service account to create the job you need

  3. It could be just a simple curl POST to the API server from inside the pod with the json for the job creation

How do I access the Kubernetes api from within a pod container?

you can also use the application specific SDK , for example if you have a python application , you can import kubernetes and run the job.

-- Ijaz Ahmad Khan
Source: StackOverflow