如何从 Kubernetes 的命令行运行容器(如 docker run)? [英] How do I run a container from the command line in Kubernetes (like docker run)?

查看:23
本文介绍了如何从 Kubernetes 的命令行运行容器(如 docker run)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 Kubernetes 集群中从命令行运行一次性容器.相当于:

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

docker run --rm -it centos /bin/bash

是否有 kubectl 等价物?

推荐答案

kubectl 相当于

docker run --rm -it centos /bin/bash

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

注意事项:

  • 这将创建一个名为 tmp-shell 的 Pod.如果您不指定 --restart=Never,则会创建一个部署(来源:Urosh T 的回答).

  • This will create a Pod named tmp-shell. If you don't specify --restart=Never, a Deploment will be created instead (credit: Urosh T's answer).

--rm 确保在 shell 退出时删除 Pod.

--rm ensures the Pod is deleted when the shell exits.

如果您想从外壳分离并使其运行并能够重新连接,请省略 --rm.然后,您可以在退出 shell 后重新附加:kubectl attach $pod-name -c $pod-container -i -t.

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.

如果您的 shell 没有启动,请检查您的集群是否资源不足(kubectl describe nodes).您可以使用 --requests 指定资源请求:

If your shell does not start, check whether your cluster is out of resources (kubectl describe nodes). You can specify resource requests 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)

这篇关于如何从 Kubernetes 的命令行运行容器(如 docker run)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆