0

How do I pass a environment variable into a kubectl exec command, which is calling a script?

kubectl exec client -n namespace -- /mnt/script.sh

In my script.sh, I need the value of the passed variable.

I tried:

kubectl exec client -n namespace -- PASSWORD=pswd /mnt/script.sh

which errors with:

OCI runtime exec failed: exec failed: unable to start container process: exec: "PASSWORD=pswd": executable file not found in $PATH: unknown

1 Answer 1

1

You can use env(1)

kubectl exec client -n namespace -- \
  env PASSWORD=pswd /mnt/script.sh

or explicitly wrap the command in an sh(1) invocation so a shell processes it

kubectl exec client -n namespace -- \
  sh -c 'PASSWORD=pswd /mnt/script.sh'

This comes with the usual caveats around kubectl exec: a "distroless" image may not have these standard tools; you're only modifying one replica of a Deployment; your changes will be lost as soon as the Pod is deleted, which can sometimes happen outside of your control.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.