且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Kubernetes中从命令行运行容器(如docker run)?

更新时间:2023-01-15 22:32:06

最简单的方法是:

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

注释:

  • 这将显示一个名为tmp-shell的整体Deployment.只要您使用kubectl run,就会发生这种情况.
  • --rm确保退出外壳程序时删除此Deployment及其所有组件.如果省略了--rm,则可以使用kubectl delete deploy/tmp-shell手动将其删除.
  • 如果要脱离外壳并使其具有重新连接的能力,请省略--rm.退出外壳后,您将可以重新连接:kubectl attach $pod-name -c $pod-container -i -t.
  • 如果您的外壳未启动,请检查您的集群是否资源不足(kubectl describe nodes).您可以通过--requests

  • 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.

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