简体   繁体   中英

How to get output from an interactive shell inside of a pod

I am trying to write a python script using one of the sdk by my org, and extract some useful information from kubernetes pod.

The sdk I have has a podexec() function which can be used to execute a command inside the pod.

I have a specific usecase, where I've to execute a command inside of the pod, which inturn will spin up an interactive shell, and then in that interactive shell, I want to execute a command and print the output.

For example, Let's say there's a mysql pod, and I want to first exec into the mysql pod, and then run mysql command which will bring up an interactive mysql shell , where I want to enter some commands like " Show tables; ", and then get the output of that command in my script. Is it possible?

After getting into the pod, I am able to run a single command like below

kubectl exec -it mysql-pod -- bash

echo "show tables;" |mysql

Now how to run this without entering the pod with just the kubectl?

NOTE: My usecase is not w.r.t mysql actually. My org has a custom tool which lets us execute commands in it's interactive shell. Mysql here is just an example.

Ok. Figured it out.

kubectl exec -it mysql-pod -- bash -c "echo \"show tables\" |mysql"

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