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

前端 未结 2 990
感情败类
感情败类 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: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)

提交回复
热议问题