How can I login to any pod within any namespace in kubernetes and run any command?

6/4/2020

My requirement is to login into pod without selecting the namespace and the pod name, then providing a command to be run inside it. There was no thread or question that answered the query. Below is a solution that I came up with. If there can be improvements in the same, please provide those.

What I was looking for to build automation or a krew plugin for logging into any pod in any namespace and run any command inside it. It is purely automation based, there are plenty of plugins that do the same work like k9s and other krew plugins but my requirement was more of a generic based and lightweight which could be implemented in a Pipeline without any third-party tools.

-- redzack
awk
kubectl
kubernetes

1 Answer

6/4/2020

Create a Shell Script

login.sh

#$1 is namespace $2 is the row number where the pod is located $3 is the command to run inside the pod
#$3 is command can be bash,sh or some command like echo "test"
#Example ./login.sh test-ns 1 bash or ./login.sh test-ns 1 'echo "test"'

pod=$(kubectl get pods -n $1 | grep -v NAME | awk -v i=1 -v j=$2 'FNR == j {print $i}')
kubectl exec -it $pod -n $1 -- $3

How to use it?

login.sh $namespace $pod_number $command

1. Create the above script, make it executable and copy it in /usr/local/bin directory or in the $PATH variable

chmod +x login.sh
#Check if /usr/local/bin exists in PATH variable, if not then run the export command
echo $PATH
export PATH=$PATH:/usr/local/bin
cp -p login.sh /usr/local/bin/login.sh

2. Run kubectl get pods to get the pod number in which you want to the command to run

helloworld-v1-5dfcf5d5cd-v7xtw   1/1     Running   0          8d
httpbin-56db79f4f5-j2cxz         1/1     Running   1          8d
test-cc5b6bfd-2hhmr              1/1     Running   0          39h

And I want to exec into the second pod with the shell being bash

3. Run the command to use the script

login.sh default 2 bash

Demo Output

The output would look something like:

shubham.yadav@my-MAC:~/k8s $ ./login.sh default 2 bash
root@httpbin-56db79f4f5-j2cxz:/# 

Another example just to make implementation more clear

shubham.yadav@my-MAC:~/k8s $ login.sh qa-test 2 'echo "From the pod Login Succeeded"'
"From the pod Login Succeeded"

Note: In case you have a large number of pods in the namespace you can use NR utility of awk to get the column numbers of the pods.

kp is an alias for kubectl get pods

kp | grep -v NAME | awk '{print NR, $1}'

1 details-v1-78d78fbddf-zksf7
2 helloworld-v1-5dfcf5d5cd-wl2nx
3 httpbin-56db79f4f5-wj4ql
4 load-generator-5cdbd66865-fxpbm
5 productpage-v1-85b9bf9cd7-qp79l
6 ratings-v1-6c9dbf6b45-xm6ww
7 reviews-v1-564b97f875-cx86c
8 reviews-v2-568c7c9d8f-xxc86
9 reviews-v3-67b4988599-p98ft
10 test-cc5b6bfd-68tqg
11 test-cc5b6bfd-8q694
12 unset-deployment-7896c75bf6-5w27b
13 web-v1-fc4d58bdc-pcv9p
14 web-v2-7bf5dd654d-684t9
15 web-v3-7567d5d6b9-sqrpg

Creating a Binary for Shell Script

In case if you are interested in creating the binary for the above script, you can follow the following link which lets you create a binary for your above shell script.

You can also change the binary name to a more friendly name from login.sh to just execute

shubham.yadav@my-MAC:/usr/local/bin $ mv login.sh execute
shubham.yadav@my-MAC:/usr/local/bin $ execute qa-test 2 'echo "Binary Name Changed"'
"Binary Name Changed"
-- redzack
Source: StackOverflow