0

What I'd like to ask is if is it possible to create a Kubernetes Job that runs a bash command within another Pod.

apiVersion: batch/v1
kind: Job
metadata:
  namespace: dev
  name: run-cmd
spec:
  ttlSecondsAfterFinished: 180
  template:
    spec:
      containers:
      - name: run-cmd
        image: <IMG>
        command: ["/bin/bash",  "-c"]
        args:
        - <CMD> $POD_NAME
      restartPolicy: Never
  backoffLimit: 4

I considered using :

  • Environment variable to define the pod name
  • Using Kubernetes SDK to automate

But if you have better ideas I am open to them, please!

rafidini
  • 216
  • 1
  • 8

1 Answers1

0

The Job manifest you shared seems the valid idea.

Yet, you need to take into consideration below points:

  • As running command inside other pod (pod's some container) requires interacting with Kubernetes API server, you'd need to interact with it using Kubernetes client (e.g. kubectl). This, in turn, requires the client to be installed inside the job's container's image.
  • job's pod's service account has to have the permissions to pods/exec resource. See docs and this answer.
rok
  • 9,403
  • 17
  • 70
  • 126