Search code examples
kuberneteskubectlkubernetes-pod

How to run a pod with multiple commands and keeping an interactive shell after that with kubectl?


I run this command go get an interactive shell inside a pod in kubernetes:

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

That works just fine, but often I do run one more command in the interactive shell after it's up:

apk add curl

I cannot figure out how to combine this commands so that I get an interactive shell after the curl is installed. Is it possible to do? I tried some other approaches like -c argument to the shell, but it finishes execution after the install of curl.


Solution

  • Using sh -c "apk add curl && sh" works for me:

    $ 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
    

    My 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"}