Kubernetes - kubectl exec -it {into the pod I just created from kubectl apply -f file.yml} -- bash

6/20/2021

Small question regarding Kubernetes, and the kubectl command please.

Currently, I perform three separate actions:

action 1: kubectl -n=mynamespace apply -f /path/to/manifest.yml

Based on the successful creation, when I see the created I perform action 2, in order to retrieve the pod

kubectl -n=mynamespace get all

This step is purely a manual, time consuming and error prone step. I have to look at the terminal with eyes, selecting the correct pod name. Just to copy paste manually, the latest pod created from step 1.

I will be looking with my eye for something like this, and manually copy paste: pod/my-pod-6bd84ccb9f-6kjwj

Once I get the pod, I go with action 3, which is to exec inside the pod, with the copy paste from step 2:

kubectl -n=mynamespace exec -it pod/my-pod-6bd84ccb9f-6kjwj -- bash

I feel like this is not the correct solution, and because of my ignorance, I am doing unnecessary steps.

May I ask if there is a smarter way to simply just create the pod, then to be able to directly exec inside this pod?

Some kind of command which will allow this.

kubectl -n=mynamespace exec -it {into the pod I just created from kubectl apply -f file.yml} -- bash

Thank you.

-- PatPatPat
kubernetes

3 Answers

6/20/2021

Its tricky, you may try combine a few kubectl command with && conditions and put it as for command conditions.

something like this:

for pod in $(kubectl -n test apply -f /path/to/manifest.yaml && kubectl -n test get pod -o jsonpath={.items[*].metadata.name}); do echo $pod && kubectl -n test exec --stdin $pod -- /bin/sh; done

but I dont think this will work for multiple pods listed inside the namespace.

the best way that I can think of is put a fix name of the pods and exec command after created with && conditions.

-- DomuHarahap
Source: StackOverflow

6/20/2021

Not really. There is kubectl run -i but I don't think that's what you're looking for? Really the answer is "don't". kubectl exec is intended only for very rare debugging use and if you're doing it enough to be annoyed, something is probably very very wrong with your workflow. What do you think you need it?

-- coderanger
Source: StackOverflow

6/21/2021

TL;DR Your solution may be to use && between commands. && lets you do something based on whether the previous command completed successfully.


Explanation I have deployed simple pod, based on this documentation. I have created GKE cluster for it, and then: 1. I have created a namespace, called test-ns:

kubectl create namespace test-ns
  1. I make sure that it has been created:
kubectl get ns

gives me output:

NAME                   STATUS   AGE
default                Active   7m
kube-node-lease        Active   7m
kube-public            Active   7m
kube-system            Active   7m
test-ns                Active   1m
  1. In this step, I will create deploy and log in to pod at the same time:
<user>@cloudshell:~ (<user>-<project>)$ kubectl apply -n test-ns -f https://k8s.io/examples/pods/init-containers.yaml && kubectl -n test-ns exec -it init-demo -- /bin/bash

gives me output:

pod/init-demo configured
Defaulted container "nginx" out of: nginx, install (init)
root@init-demo:/#

I created deploy and logged in to pod at the same time. If I logout from this pod using exit or ctrl +D, and then execute the command kubectl get pods -n test-ns, I have an output:

NAME        READY   STATUS    RESTARTS   AGE
init-demo   1/1     Running   0          9m

That means, everything is fine. Pod is created successfully.

-- Mikołaj Głodziak
Source: StackOverflow