如何让容器在 Kubernetes 上运行? [英] How can I keep a container running on Kubernetes?

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

问题描述

我现在正在尝试在 Kubernetes 集群上运行一个带有 shell (/bin/bash) 的简单容器.

I'm now trying to run a simple container with shell (/bin/bash) on a Kubernetes cluster.

我认为有一种方法可以通过使用 pseudo-tty 和分离选项(docker 上的 -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

坦率地说,保持容器打开是一种技巧,只应用于快速测试和示例.如果你只是想要一个容器来测试几分钟,我会这样做:

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 上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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