如何使用kubectl运行具有多个命令的pod并在此之后保持交互式外壳?

时间:2019-08-01 12:57:41

标签: kubernetes kubectl kubernetes-pod

我运行此命令去在kubernetes的Pod中获得一个交互式shell:

kubectl run my-shell --generator=run-pod/v1 --rm -it --image alpine -- sh

这很好用,但是在启动后,我经常在交互式外壳中再运行一个命令:

apk add curl

我无法弄清楚如何组合此命令,以便在安装curl之后获得交互式shell。有可能吗?我尝试了其他一些方法,例如对外壳程序使用-c参数,但是在安装curl之后,它完成了执行。

1 个答案:

答案 0 :(得分:3)

使用sh -c "apk add curl && sh"对我有用:

$ kubectl run my-shell --generator=run-pod/v1 --rm -it --image alpine -- sh -c "apk add curl && sh"
If you don't see a command prompt, try pressing enter.

/ # curl --version
curl 7.65.1 (x86_64-alpine-linux-musl) libcurl/7.65.1 OpenSSL/1.1.1c zlib/1.2.11 nghttp2/1.38.0
Release-Date: 2019-06-05
Protocols: dict file ftp ftps gopher http https imap imaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp 
Features: AsynchDNS HTTP2 HTTPS-proxy IPv6 Largefile libz NTLM NTLM_WB SSL TLS-SRP UnixSockets

/ # exit
Session ended, resume using 'kubectl attach my-shell -c my-shell -i -t' command when the pod is running
pod "my-shell" deleted

我的kubectl version

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.1", GitCommit:"b7394102d6ef778017f2ca4046abbaa23b88c290", GitTreeState:"clean", BuildDate:"2019-04-08T17:11:31Z", GoVersion:"go1.12.1", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.1", GitCommit:"b7394102d6ef778017f2ca4046abbaa23b88c290", GitTreeState:"clean", BuildDate:"2019-04-08T17:02:58Z", GoVersion:"go1.12.1", Compiler:"gc", Platform:"linux/amd64"}