kubectl run - How to pass all the ENV variables of the hosting shell to the pod? - kubernetes

It is possible to pass some ENV variables to the pod via the --env option of kubectl run, for example:
kubectl run -ti --rm test --image=busybox --env location=city --env time=morning --namespace default -- sh
I would like to send all the present ENV variables to the pod without specifying all of them the via --env, especially for those that I might not know the variable name. Is there an way to do so? Thanks

You could try something like this:
kubectl run -ti --rm test --image=busybox $(printenv | xargs -I % echo '--env' "%" | tr '\n' ' ') --namespace default -- sh

Related

kubectl exec into a container without using the random guid at the end of the pod name

To exec into a container in a pod, I use the following two commands (note the template flag in the first command trims the output to print just the name of the pods):
$ kubectl get pods --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'
app-api-6421cdf4fd-x9tbk
app-worker-432f86f54-fknxw
app-frontend-87dd65d49c-6b4mn
app-rabbit-413632c874-s2ptw
$ kubectl exec -it app-api-6421cdf4fd-x9tbk -- bash
It would be nice to exec into the container without having to discover the random guid at the end of the pod name every single time. How can I do this?
You can exec into a pod using the deployment
You can use the following command:
kubectl exec -it deploy/<deployment-name> -- bash
Suppose you have a Deployment with the name myadmin and namespace demo. After deploy, you can exec into the deployment's pod by running the below command:
$ export id=( kubectl get pod -n demo | grep 'myadmin' | awk 'END {print $1}' | xargs echo) && kubectl -n demo exec -it $id -- bash
I believe you expect this.
(sed 's/pod\///' is used below because kubectl get pod -o name gives you names like pod/rabbitmq-0 and you should cut first part)
kubectl exec -it $(kubectl get pod -o name | sed 's/pod\///' | grep api) -- bash
Similar to previous one you can use any known commands to find and sort needed objects. Providing version from your initial question
kubectl exec -it $(kubectl get pod --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'| grep api) -- bash

Understanding kubectl run command

I am tryng to create a pod using kubectl run by creating an yaml file, where first command is creating container but showing state as error and second one is creating with out any issue. what is the difference between these commands?
master $ kubectl run --restart=Never --image=busybox static-busybox --command -- sleep 1000 --dry-run -o yaml //Error container
master $ kubectl run --restart=Never --image=busybox static-busybox --dry-run -o yaml --command -- sleep 1000 //working command
In the first one parameters --dry-run -o yaml are applied to the command you run in the container (sleep), in the second one, they are applied to your kubectl execution
As per the syntax of the kubectl run command needs to be at the end. That's precisely the reason why first command is not working but second one is working.
Usage:
kubectl run NAME --image=image [--env="key=value"] [--port=port] [--dry-run=server|client] [--overrides=inline-json]
[--command] -- [COMMAND] [args...] [options]

Do we have command to execute multiple arguments in kubernetes

I have a pod running in kubernetes and i need to run two commands in one line.
Say,
kubectl exec -it <pod name> -n <namespace > -- bash -c redis-cli
above command will open redis-cli
i want to run one more command after exec in one line ie info, i am trying below which is not working:
kubectl exec -it <pod name> -n <namespace > -- bash -c redis-cli -- info
You have to put your command and all the parameters between apostrophes.
in your example it would be:
kubectl exec -it <pod_name> -n <namespace> -- bash -c 'redis-cli info'
From Bash manual: bash -c: If the -c option is present, then commands are read from the first non-option argument commaqnd_string.
Other option (which in my opinion is a better approach) is to get the output from the command with an instant pod, which creates, runs and deletes the pod right after that, like this:
kubectl run --namespace <YOUR_NAMESPACE> <TEMP_RANDOM_POD_NAME> --rm --tty -i --restart='Never' --env REDIS_PASSWORD=$REDIS_PASSWORD --image docker.io/bitnami/redis:5.0.7-debian-10-r0 -- bash -c 'redis-cli -h redis-master -a $REDIS_PASSWORD info'
in my case the password was stored in a envvar called $REDIS_PASSWORD and I'm connecting to a server in a pod called redis-master.
I let it as I runned it to show that you can use as much parameters as needed.
POC:
user#minikube:~$ kubectl run --namespace default redis-1580466120-client --rm --tty -i --restart='Never' --env REDIS_PASSWORD=$REDIS_PASSWORD --image docker.io/bitnami/redis:5.0.7-debian-10-r0 -- bash -c 'redis-cli -h redis-master -a $REDIS_PASSWORD info'
10:41:10.65
10:41:10.66 Welcome to the Bitnami redis container
10:41:10.66 Subscribe to project updates by watching https://github.com/bitnami/bitnami-docker-redis
10:41:10.66 Submit issues and feature requests at https://github.com/bitnami/bitnami-docker-redis/issues
10:41:10.67 Send us your feedback at containers#bitnami.com
10:41:10.67
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Server
redis_version:5.0.7
redis_git_sha1:00000000
redis_git_dirty:0
...
{{{suppressed output}}}
...
# CPU
used_cpu_sys:1.622434
used_cpu_user:1.313600
used_cpu_sys_children:0.013942
used_cpu_user_children:0.008014
# Cluster
cluster_enabled:0
# Keyspace
pod "redis-1580466120-client" deleted
Not get your question, do you want to get the information from redis-cli?
kubernetes exec -it <pod name> -n <namespace > -- bash -c 'redis-cli info'
Did you try to link your commands using && ?
kubernetes exec -it <pod name> -n <namespace > -- bash -c redis-cli && info

Get environment variable from kubernetes pod?

What's the best way to list out the environment variables in a kubernetes pod?
(Similar to this, but for Kube, not Docker.)
kubectl exec -it <pod_name> -- env
Execute in bash:
kubectl exec -it <pod-name> -- printenv | grep -i env
You will get all environment variables that consists env keyword.
Both answers have the following issues:
They assume you have the permissions to start pod, which is not the case in a locked-down environment
They start a new pod, which is invasive and may give different environment variables than "a[n already running] kubernetes pod"
To inspect a running pod and get its environment variables, one can run:
kubectl describe pod <podname>
This is from Alexey Usharovski's comment.
I am hoping this gives more visibility to your great answer. If you would like to post it as an answer yourself, please let me know and I will delete mine.
kubectl exec <POD_NAME> -- sh -c 'echo $VAR_NAME'
I normally use:
kubectl exec -it <POD_NAME> -- env | grep "<VARIABLE_NAME>"

Need to run more than one commands

I have to run 2 commands at a time:
bash
service nginx start
How can I pass those by using the following command?
kubectl run nginx --image=nginx --command -- <cmd> <arg1> ... <argN>
kubectl run -it testnew --image=imagename --command -- "/bin/bash","-c","service nginx start && while true; do echo bye; sleep 10;done" --requests=cpu=200m
Not sure how the --command flag works or is supposed to work.
This works for me, in that I get a running nginx with bash looping forever and printing 'bye'.
kubectl run -it testnew --image=nginx -- /bin/bash -c "service nginx start && while true; do echo bye; sleep 10;done"
Instead of this special command, you probably want to create a tweaked image that runs a script on start. Easier to manage what is running and harder to lose the customizations.