1

使用 except 命令解决无法在无 tty 的环境里执行 kubectl exec -it 命令的问题

 1 month ago
source link: https://mozillazg.com/2024/03/fix-can-not-run-kubectl-exec-it-in-no-tty-env.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

问题

当我们在无 tty 环境下执行 kubectl exec -it 命令的时候, kubectl 会输出如下警告然后自动禁用 -t 选项:

Unable to use a TTY - input is not a terminal or the right kind of file

无 tty 的场景包括但不限于:

  • 在未启用 tty 的容器中执行 kubectl 命令
  • 使用 nohup 执行包含 kubectl 命令的脚本(可以使用该方法在不修改原脚本的情况下复现上面的报错)
  • 使用管道或 < 作为 kubectl 命令的输入

解决方法

对于无需进行交互式操作的场景,通常可以通过主动去除 -t 选项来去掉该告警。 但是有时我们的场景确实需要进行交互式操作, 或者我们的场景是即便不需要进行交互式操作也会需要依赖 -t 选项(比如,为了测试 -t 选项的功能)。

此时,我们可以通过 except 这个工具来启用一个 tty, 满足 -t 选项所依赖的环境:

cat <<EOF > test.sh

# 执行 kubectl exec -it 命令
spawn kubectl exec -it xxxx yyy

# 可以通过 send 和 expect 进行交互式操作
# send "<CMD>\r"
# expect "<STR>"

# 发送 exit 命令,退出交互式终端
# send "exit\r"
# expect eof

EOF

except -f test.sh

关于 except 命令的详细使用方式大家可以在网络上自行搜索。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK