问题描述
我现在正在尝试在Kubernetes集群上运行一个带有shell(/bin/bash)的简单容器.
I'm now trying to run a simple container with shell (/bin/bash) on a Kubernetes cluster.
我认为可以使用pseudo-tty
和detach选项(docker run
命令的-td
选项)来保持容器在Docker容器上运行.
I thought that there was a way to keep a container running on a Docker container by using pseudo-tty
and detach option (-td
option on docker run
command).
例如,
$ sudo docker run -td ubuntu:latest
Kubernetes中有这样的选择吗?
Is there an option like this in Kubernetes?
我尝试使用kubectl run-container
命令运行容器,例如:
I've tried running a container by using a kubectl run-container
command like:
kubectl run-container test_container ubuntu:latest --replicas=1
但是容器会退出几秒钟(就像使用docker run
命令启动时没有上面我提到的选项一样).然后ReplicationController反复再次启动它.
But the container exits for a few seconds (just like launching with the docker run
command without options I mentioned above). And ReplicationController launches it again repeatedly.
是否可以像docker run
命令中的-td
选项那样保持容器在Kubernetes上运行?
Is there a way to keep a container running on Kubernetes like the -td
options in the docker run
command?
推荐答案
容器在其主进程退出时退出.做类似的事情:
A container exits when its main process exits. Doing something like:
docker run -itd debian
坦率地说,将容器保持打开状态是一种hack,仅应用于快速测试和示例.如果您只想要一个容器进行几分钟的测试,我会这样做:
to hold the container open is frankly a hack that should only be used for quick tests and examples. If you just want a container for testing for a few minutes, I would do:
docker run -d debian sleep 300
其优点是,如果您忘记了该容器,它将自动退出.另外,您可以在while
循环中放置类似的内容以使其永久运行,或者仅运行诸如top
之类的应用程序.所有这些都应该在Kubernetes中很容易做到.
Which has the advantage that the container will automatically exit if you forget about it. Alternatively, you could put something like this in a while
loop to keep it running forever, or just run an application such as top
. All of these should be easy to do in Kubernetes.
真正的问题是,为什么要这样做?您的容器应提供服务,其过程将使容器在后台运行.
The real question is why would you want to do this? Your container should be providing a service, whose process will keep the container running in the background.
这篇关于如何在Kubernetes上保持容器运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!