How to create a pod and attach with a terminal later on - kubernetes

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).

This does work:
 kubectl run alpine --image alpine --command sleep -- 999d
pod/alpine created
 kubectl exec -ti alpine -- ash
/ #
I need an auxiliary sleep.

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

App pod logs with linkerd | unable to view

I was able to view the app container logs using kubectl -f logs and was able to login to the container using "k exec --stdin --tty -- /bin/bash".
After injecting linkerd, I could not login to the container. However my goal is to check the app logs.
When I use this "k logs -f linkerd-proxy" I could not see the app-related logs.
I tried injecting debug-sidecar as well.
Tried this - "k logs deploy/ linkerd-debug - " and as well as this "k exec -it -c linkerd-debug -- tshark -i any -f "tcp" -V -Y "http.request"
still I couldn't see the exact logs for my app in the pod. Please suggest.
Linkerd works by injecting an additional container into your pods; this is known as the "sidecar" pattern. Your application (or better said container) logs are still accessible, however, as a result of having more than one container in the pod, kubectl requires you to explicitly specify the container name.
For example, assuming you have a pod with two containers (linkerd-proxy and app), you'd have to specify app as the name of the container:
$ kubectl logs -f <pod-name> -c app
# You can specify the container name without the -c flag
$ kubectl logs -f <pod-name> app
# This will work for 'exec' too
$ kubectl exec <pod-name> -c app -it -- sh

Configure command to use for shell on pod

In k9s: Is there a way to configure the command which is used when starting a shell inside the pod?
I have looked their docu and briefly browsed the source without hints how a shell command could be specified.
kubectl command-line tool must be configured to communicate with your cluster.
Create the Pod:
kubectl apply -f https://k8s.io/examples/application/shell-demo.yaml
Verify that the container is running:
kubectl get pod shell-demo
Get a shell to the running container:
kubectl exec --stdin --tty shell-demo -- /bin/bash
In your shell, list the root directory:
Run this command inside the container:
ls /
Please refer to this document Configure command to use for shell on pod

How can I enter a Kubernetes managed container faster?

Currently if I'm about to inspect my container, I have to do three steps:
kubectl get all -n {NameSpace}
kubectl describe {Podname from step 1} -n {NameSpace}
Find the Node Host and the container ID (My eyes are complaning!)
Switch to the host and execute "docker exec -ti -u root {Container ID} bash"
I am so mad about it right now. Wish somebody could offer some help to me and those who may share the same issue.
Pods are the smallest deployable units of computing that you can
create and manage in Kubernetes.
So, if you want to "enter" a container, you just need to "exec" into the pod in a particular namespace. Kubernetes will get you the shell/command for that pod.
kubectl -n somenamespace exec -it podname -- bash
There is no need to mention the node here as Kubernetes internally knows on which node the pod is scheduled.
If a Pod has more than one container, use --container or -c to specify
a container in the kubectl exec command. For example, suppose you have
a Pod named my-pod, and the Pod has two containers named main-app and
helper-app. The following command would open a shell to the main-app
container.
kubectl exec -it my-pod -c main-app -- /bin/bash
https://kubernetes.io/docs/tasks/debug-application-cluster/get-shell-running-container/

kubernetes : How to deattach from an attached pod

I have a running pod and I use kubectl attach -ti mypod to give him input.
But then, I would like to "deattach" from my pod and return back to my terminal without terminating my pod.
How can I do that?
From the kubernetes docs:
To detach from the container, you can type the escape sequence Ctrl+P
followed by Ctrl+Q.
Update:
Based on feedback from #Abdelghani:
For the record, this does not work if tty is set to false in the
container. I didnt find another way to detach other than killing the
pod/container.
You can kill the kubectl attach -it .. process without killing the pod:
host$ kubectl attach -it podname
pod$ do something in pod
Open the other terminal and find your kubectl attach command and then kill it, something like:
host$ ps x | grep "kubectl attach"
> 1234 pts/22 Sl+ 0:00 kubectl attach -it podname
host$ kill 1234

Do we have command to execute multiple arguments in kubernetes

I have a pod running in kubernetes and i need to run two commands in one line.
Say,
kubectl exec -it <pod name> -n <namespace > -- bash -c redis-cli
above command will open redis-cli
i want to run one more command after exec in one line ie info, i am trying below which is not working:
kubectl exec -it <pod name> -n <namespace > -- bash -c redis-cli -- info
You have to put your command and all the parameters between apostrophes.
in your example it would be:
kubectl exec -it <pod_name> -n <namespace> -- bash -c 'redis-cli info'
From Bash manual: bash -c: If the -c option is present, then commands are read from the first non-option argument commaqnd_string.
Other option (which in my opinion is a better approach) is to get the output from the command with an instant pod, which creates, runs and deletes the pod right after that, like this:
kubectl run --namespace <YOUR_NAMESPACE> <TEMP_RANDOM_POD_NAME> --rm --tty -i --restart='Never' --env REDIS_PASSWORD=$REDIS_PASSWORD --image docker.io/bitnami/redis:5.0.7-debian-10-r0 -- bash -c 'redis-cli -h redis-master -a $REDIS_PASSWORD info'
in my case the password was stored in a envvar called $REDIS_PASSWORD and I'm connecting to a server in a pod called redis-master.
I let it as I runned it to show that you can use as much parameters as needed.
POC:
user#minikube:~$ kubectl run --namespace default redis-1580466120-client --rm --tty -i --restart='Never' --env REDIS_PASSWORD=$REDIS_PASSWORD --image docker.io/bitnami/redis:5.0.7-debian-10-r0 -- bash -c 'redis-cli -h redis-master -a $REDIS_PASSWORD info'
10:41:10.65
10:41:10.66 Welcome to the Bitnami redis container
10:41:10.66 Subscribe to project updates by watching https://github.com/bitnami/bitnami-docker-redis
10:41:10.66 Submit issues and feature requests at https://github.com/bitnami/bitnami-docker-redis/issues
10:41:10.67 Send us your feedback at containers#bitnami.com
10:41:10.67
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Server
redis_version:5.0.7
redis_git_sha1:00000000
redis_git_dirty:0
...
{{{suppressed output}}}
...
# CPU
used_cpu_sys:1.622434
used_cpu_user:1.313600
used_cpu_sys_children:0.013942
used_cpu_user_children:0.008014
# Cluster
cluster_enabled:0
# Keyspace
pod "redis-1580466120-client" deleted
Not get your question, do you want to get the information from redis-cli?
kubernetes exec -it <pod name> -n <namespace > -- bash -c 'redis-cli info'
Did you try to link your commands using && ?
kubernetes exec -it <pod name> -n <namespace > -- bash -c redis-cli && info