Remotely execute commands in containers(kubectl exec command) - Execute Curl command in kubernetes pod and Running a shell in a pod’s container

The kubectl exec command allows you to remotely run arbitrary commands inside an existing container of a pod.

Execute curl command in a running container pod

1. Connect to Kubernetes cluster
n0r0082@m-c02z31rnlvdt ~ % connect sc-stage-a2
Connected to sc-stage-a2

2. Get all running pods and choose a pod where you want to execute command
n0r0082@m-c02z31rnlvdt ~ % kubectl get po -n my-namespace
NAME                                                         READY   STATUS      RESTARTS   AGE
prometheus-prometheus-0                                      3/3     Running     1          8d
prometheus-prometheus-1                                      3/3     Running     1          8d
app-configuration-stage-67696bb6c4-qr67q                     1/1     Running     0          8d
app-configuration-stage-5697c54f4b-z9mpb                    1/1     Running     0          3d15h
...
....
scale-down-app-configuration-stage-1645218000-ldnf7         0/1     Completed   0          5h27m
  • "-n my-namespace" -n switch is for namespace selection and my-namespace is name of namespace.
3. Select one pod where we want to run command(For example hitting an REST end point to get orderDetails)
n0r0082@m-c02z31rnlvdt ~ % kubectl -n my-namespace exec app-configuration-stage-67696bb6c4-qr67q 
-- curl --location --request GET 'https://app.platform.sync.stage.walmart.com/platform-app/services/v3/order/3092258313193/orderDetails' \ --header 'Content-Type: application/json' \ --header 'Cookie: vtc=Z1nBR_x0887jRQPE5IMO-E' Defaulted container "platform-stage-app" out of: platform-stage-app, vault-agent-init (init) % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 12963 0 12963 0 0 7532 0 --:--:-- 0:00:01 --:--:-- 7532 {"status":"OK","payload": ..... ..... ........ }
  • "kubectl exec" command allows you to remotely run arbitrary commands inside an existing container of a pod.
  • The double dash (--) in the command signals the end of command options for kubectl. Everything after the double dash is the command that should be executed inside the pod.

Sequence of events:  

  1. Above command instructed Kubernetes to execute the curl command inside pod(app-configuration-stage-67696bb6c4-qr67q) specified 
  2. App running inside that pod then handled the request and returned an HTTP response. 
  3. Curl then printed the response to the standard output, which was intercepted and printed to its standard output on your local machine by kubectl.


Running a shell in a pod’s container 

  • We can use the kubectl exec command to run bash (or any other shell) inside a pod’s container. Once inside shell, we are free to explore the container as long as we want, without having to perform a kubectl exec for every command
  • The shell’s binary executable must be available in the container image for this to work.
n0r0082@m-c02z31rnlvdt ~ % kubectl exec -it app-configuration-stage-67696bb6c4-qr67q bash -n my-namespace
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. 
Use kubectl exec [POD] -- [COMMAND] instead. Defaulted container "platform-stage-app" out of: platform-stage-app, vault-agent-init (init)
bash-4.2$

Now same url command(executed above) can be run from bash shell too.

bash-4.2$ curl --location --request GET 'https://app.platform.sync.stage.walmart.com/platform-app/services/v3/order/3092258313193/orderDetails' \ --header 'Content-Type: application/json' \ --header 'Cookie: vtc=Z1nBR_x0887jRQPE5IMO-E' Defaulted container "platform-stage-app" out of: platform-stage-app, vault-agent-init (init) % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 12963 0 12963 0 0 7532 0 --:--:-- 0:00:01 --:--:-- 7532 {"status":"OK","payload": ..... ..... ........ }


-------//----//-----//-------------//----//-----//------

Post a Comment

Previous Post Next Post