How do I run a container from the command line in Kubernetes (like docker run)?

前端 未结 2 897
感情败类
感情败类 2021-01-04 05:54

I would like to run a one-off container Pod from the command line in my Kubernetes cluster. I am looking for the equivalent of:

docker run --rm -it centos /b         


        
相关标签:
2条回答
  • 2021-01-04 06:28

    In order to have a Pod created instead of a Deployment and to have it removed by itself when you exit it, try this:

    kubectl run curl-debug --rm -i --tty --restart=Never --image=radial/busyboxplus:curl -- /bin/sh
    

    The --restart=Never flag is what it says to create a Pod instead of a Deployment object

    Also - This image is lightweight, downloads fast and is good for network debugging.

    Hope that helps

    0 讨论(0)
  • 2021-01-04 06:37

    It looks like the simplest way is:

    kubectl run tmp-shell --rm -i --tty --image centos -- /bin/bash
    

    Notes:

    • This will bring up a whole Deployment named tmp-shell. This happens any time you use kubectl run.
    • --rm ensures this Deployment and all of its components are deleted when you exit the shell. If you omitted --rm, you can delete it manually with kubectl delete deploy/tmp-shell.
    • If you want to detach from the shell and leave it running with the ability to re-attach, omit the --rm. You will then be able to reattach with: kubectl attach $pod-name -c $pod-container -i -t after you exit the shell.
    • If your shell does not start, check whether your cluster is out of resources (kubectl describe nodes). You can control the resources this deployment is requesting with --requests:

      --requests='': The resource requirement requests for this container.  For example, 'cpu=100m,memory=256Mi'.  Note that server side components may assign requests depending on the server configuration, such as limit ranges.
      

    (Inspired by https://gc-taylor.com/blog/2016/10/31/fire-up-an-interactive-bash-pod-within-a-kubernetes-cluster)

    0 讨论(0)
提交回复
热议问题