Home > Back-end >  How to create a pod and attach with a terminal later on
How to create a pod and attach with a terminal later on

Time:12-28

I want to run a multi-container pod, and interactuate with it via a terminal. With kubectl run -ti I can run a monocontainer pod and attach easily to it.

I think I have to kubectl apply to create a complex pod, and later attach to it.

I've tried this and it doesn't work:

  kubectl run alpine --image alpine                                                                       pod/alpine created

  kubectl attach -i alpine                                                                                      If you don't see a command prompt, try pressing enter.
error: unable to upgrade connection: container alpine not found in pod alpine

I'm using latest k3s v1.25.4 k3s1 under a Linux host (Ubuntu 22.04).

CodePudding user response:

This does work:

  kubectl run alpine --image alpine --command sleep -- 999d                                               
pod/alpine created

  kubectl exec -ti alpine -- ash                                                                                
/ #

I need an auxiliary sleep.

CodePudding user response:

You need attach interactively first:

kubectl run -it alpine --image alpine

Then detach by using CTRL-P, then CTRL-Q

To reattach, you then use:

kubectl attach alpine -c alpine -i -t

Note that if you close the shell at any point, you terminate the pod, and it will restart

  • Related