I'm trying to execute some command in a pod but it's not working. What could be your suggessing?
import subprocess
p = subprocess.Popen('kubectl get pods -o=custom-columns=NAME:.metadata.name | grep -i nottepod' , shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
print line,
#Here the line = exact name of the pod. After here not working. it's not login to the pods
p2 = subprocess.Popen("kubectl exec -it' +$(line)+ '-- bash'" , shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
p3 = subprocess.Popen("ls -lrth" , shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line2 in p3.stdout.readlines():
print line2,
retval = p3.wait()
I expect to run a couple of command after get in the pod with p2 step.
What I really try is, first, get the pods name(p1), login to the pod with exec command(p2) that's why I need p1 output to use in it. After I'm login to the pod, I also need to run some commands (p3..etc) That's my main purpose. Below you can see what's happening when I run them manually.
[abc@master ~]$ kubectl get pods -o=custom-columns=NAME:.metadata.name | grep -i nottepod
nottepod-xyz
[abc@master ~]$
[abc@master ~]$ kubectl exec -it nottepod-xyz -- bash
Defaulted container "notte" out of: notte, key
[root@master -notte ]# ls -lrth
[root@master -notte ]# ls -lrth
total 216M
drwxr-xr-x 2 root root 6 Jul 6 2022 Cache
-rwxr-xr-x 3 1037 users 111 Dec 26 16:51 start.sh
-rwxr-xr-x 3 1037 users 291 Dec 26 16:51 rataten.sh