21

I am trying to attach to a running container in Kubernetes, however I get the error message below.

>kubectl attach -it es-client-2756725635-4rk43 -c es-node
Unable to use a TTY - container es-node did not allocate one
If you don't see a command prompt, try pressing enter.

How do I enable a TTY in my container yaml?

speedplane
  • 15,673
  • 16
  • 86
  • 138
  • Tried the following command: kubectl attach wildfly-rc-uc79a -it And got the error: Unable to use a TTY - container wildfly-rc-pod did not allocate one If you don't see a command prompt, try pressing enter. – Arun Gupta Dec 05 '16 at 05:12
  • Try without the `-t` flag. See https://stackoverflow.com/a/61733197/658497 – Noam Manos May 11 '20 at 15:23

5 Answers5

30

In order to have proper TTY and stdin when doing attach:

kubectl attach -it POD -c CONTAINER

The container must be configured with tty: true and stdin: true. By default both of those values are false: https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/pod-v1/#debugging

Example Pod:

spec:
      containers:
      - name: web
        image: web:latest
        tty: true
        stdin: true
E.Z.
  • 139
  • 2
  • 14
Alex Plugaru
  • 2,209
  • 19
  • 26
16

The reason why it's failiing is because you're not passing the bash argument. This causes a failure when trying to create a tty connection.

Please try:

kubectl exec -it [POD-NAME] -c [CONTAINER-NAME] bash
Alex Luis Arias
  • 1,313
  • 1
  • 14
  • 27
  • 7
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – kayess Dec 15 '16 at 21:45
  • 2
    This successfully launches an interactive shell inside the running container which is VERY useful (thank you) but it doesn't answer the OP question of using attach. Also please note some containers only have sh (not bash) – chrisinmtown Jun 14 '19 at 15:11
7

For Windows, MINGW64 (git bash) does not seem to work, but PowerShell does!

kubectl exec -it abc-deployment-5d64659ff8-8tnnb -- /bin/bash
root@abc-deployment-5d64659ff8-8tnnb:/#
stefanbschneider
  • 5,460
  • 8
  • 50
  • 88
0

Whatever I did, it didn't work, tty command would always return not a tty response and exit non-0, i.e. nothing that require tty would work on my terminal.

I'm making an ephemeral workstation with persistent disk as my $HOME with Ubuntu Bionic Beaver on GKE.

Since I had brew installed in my PD mounted $HOME and brew was in my $PATH, following worked for me:

brew install tmux
tmux new -d -s <some arbitrary session name here> ### i.e.> tmux new -d -s tty
tmux ls # Lists your sessions
tmux a  # Attach to first available session

Then inside tmux:
$ tty
Voila
/dev/pts/0

My agnoster is b0rkz but now I can do stuff that prompts for interactive password entry, i.e. gcloud auth.
tmux with pts on kubectl auth prompt

PS: For those curious, I mounted /etc/shadow, /etc/group, /etc/passwd into /etc with configmap

0

you can add a shell container ,like this. then you can use

kubectl attach -it nginx -c shell
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  shareProcessNamespace: true
  containers:
  - name: nginx
    image: nginx
  - name: shell
    image: busybox
    stdin: true
    tty: true
J.Doe
  • 1
  • 1