简体   繁体   English

如何让容器在 Kubernetes 上运行?

[英]How can I keep a container running on Kubernetes?

I'm now trying to run a simple container with shell (/bin/bash) on a Kubernetes cluster.我现在正在尝试在 Kubernetes 集群上运行一个带有 shell (/bin/bash) 的简单容器。

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).我认为有一种方法可以通过使用pseudo-tty和分离选项( docker run命令上的-td选项)来保持容器在 Docker 容器上运行。

For example,例如,

$ sudo docker run -td ubuntu:latest

Is there an option like this in Kubernetes? Kubernetes中是否有这样的选项?

I've tried running a container by using a kubectl run-container command like:我尝试使用kubectl run-container命令运行容器,例如:

kubectl run-container test_container ubuntu:latest --replicas=1

But the container exits for a few seconds (just like launching with the docker run command without options I mentioned above).但是容器会退出几秒钟(就像使用docker run命令启动时没有我上面提到的选项一样)。 And ReplicationController launches it again repeatedly. ReplicationController 会重复启动它。

Is there a way to keep a container running on Kubernetes like the -td options in the docker run command?有没有办法让容器在 Kubernetes 上运行,就像docker run命令中的-td选项一样?

Containers are meant to run to completion.容器旨在运行完成。 You need to provide your container with a task that will never finish.你需要为你的容器提供一个永远不会完成的任务。 Something like this should work:这样的事情应该工作:

apiVersion: v1
kind: Pod
metadata:
  name: ubuntu
spec:
  containers:
  - name: ubuntu
    image: ubuntu:latest
    # Just spin & wait forever
    command: [ "/bin/bash", "-c", "--" ]
    args: [ "while true; do sleep 30; done;" ]

You could use this CMD in your Dockerfile :您可以在Dockerfile使用此 CMD:

CMD exec /bin/bash -c "trap : TERM INT; sleep infinity & wait"

This will keep your container alive until it is told to stop.这将使您的容器保持活动状态,直到它被告知停止。 Using trap and wait will make your container react immediately to a stop request .使用陷阱和等待将使您的容器立即对停止请求做出反应 Without trap/wait stopping will take a few seconds.没有陷阱/等待停止将需要几秒钟。

For busybox based images (used in alpine based images) sleep does not know about the infinity argument.对于基于 busybox 的图像(用于基于 alpine 的图像) sleep 不知道无穷大参数。 This workaround gives you the same immediate response to a docker stop like in the above example:此解决方法为您提供与上述示例中的docker stop相同的即时响应:

CMD exec /bin/sh -c "trap : TERM INT; sleep 9999999999d & wait"

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

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 .或者,您可以将类似的内容放在while循环中以使其永远运行,或者只运行诸如top的应用程序。 All of these should be easy to do in Kubernetes.所有这些在 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.您的容器应该提供一项服务,该服务的进程将使容器在后台运行。

  1. In your Dockerfile use this command:在你的 Dockerfile 中使用这个命令:

     CMD ["sh", "-c", "tail -f /dev/null"]
  2. Build your docker image.构建您的 docker 镜像。

  3. Push it to your cluster or similar, just to make sure the image it's available.将它推送到你的集群或类似的,只是为了确保它的图像可用。
  4.  kubectl run debug-container -it --image=<your-image>

In order to keep a POD running it should to be performing certain task, otherwise Kubernetes will find it unnecessary, therefore it stops.为了保持 POD 运行,它应该执行某些任务,否则 Kubernetes 会发现它没有必要,因此它会停止。 There are many ways to keep a POD running.有很多方法可以让 POD 保持运行。

I have faced similar problems when I needed a POD just to run continuously without doing any useful operation.当我需要一个 POD 只是为了连续运行而不做任何有用的操作时,我也遇到过类似的问题。 The following are the two ways those worked for me:以下是对我有用的两种方式:

  1. Firing up a sleep command while running the container.在运行容器时启动 sleep 命令。
  2. Running an infinite loop inside the container.在容器内运行无限循环。

Although the first option is easier than the second one and may suffice the requirement, it is not the best option.尽管第一个选项比第二个选项容易并且可能满足要求,但它不是最佳选择。 As, there is a limit as far as the number of seconds you are going to assign in the sleep command.因为,您要在 sleep 命令中分配的秒数是有限制的。 But a container with infinite loop running inside it never exits.但是一个在其中运行的无限循环的容器永远不会退出。

However, I will describe both the ways(Considering you are running busybox container):但是,我将描述两种方式(考虑到您正在运行 busybox 容器):

1. Sleep Command 1. 睡眠命令

apiVersion: v1
kind: Pod
metadata:
  name: busybox
  labels:
    app: busybox
spec:
  containers:
  - name: busybox
    image: busybox
    ports:
    - containerPort: 80
    command: ["/bin/sh", "-ec", "sleep 1000"]

2. Infinite Loop 2. 无限循环

apiVersion: v1
kind: Pod
metadata:
  name: busybox
  labels:
    app: busybox
spec:
  containers:
  - name: busybox
    image: busybox
    ports:
    - containerPort: 80
    command: ["/bin/sh", "-ec", "while :; do echo '.'; sleep 5 ; done"]

Run the following command to run the pod:运行以下命令来运行 pod:

kubectl apply -f <pod-yaml-file-name>.yaml

Hope it helps!希望能帮助到你!

The simplest command as it can be for k8s pod manifest to run container forever:最简单的命令,因为它可以让 k8s pod manifest 永远运行容器:

apiVersion: v1
kind: Pod
metadata:
  name: ubuntu
spec:
  containers:
  - name: ubuntu
    image: ubuntu:latest
    # Just sleep forever
    command: [ "sleep" ]
    args: [ "infinity" ]

I was able to get this to work with the command sleep infinity in Kubernetes, which will keep the container open.我能够让它与 Kubernetes 中的sleep infinity命令一起使用,这将使容器保持打开状态。 See this answer for alternatives when that doesn't work.当这不起作用时,请参阅此答案以获取替代方案。

Use this command inside you Dockerfile to keep the container running in your K8s cluster:在 Dockerfile 中使用此命令来保持容器在 K8s 集群中运行:

  • CMD tail -f /dev/null CMD tail -f /dev/null

My few cents on the subject.我在这个问题上的几分钱。 Assuming that kubectl is working then the closest command that would be equivalent to the docker command that you mentioned in your question, would be something like this.假设kubectl正在工作,那么与您在问题中提到的kubectl命令最接近的命令将是这样的。

$ kubectl run ubuntu --image=ubuntu --restart=Never --command sleep infinity

Above command will create a single Pod in default namespace and, it will execute sleep command with infinity argument -this way you will have a process that runs in foreground keeping container alive.上面的命令将在default命名空间中创建一个Pod ,它将执行带有infinity参数的sleep命令 - 这样你将有一个在前台运行的进程保持容器处于活动状态。

Afterwords, you can interact with Pod by running kubectl exec command.之后,您可以通过运行kubectl exec命令与Pod进行交互。

$ kubectl exec ubuntu -it -- bash

This technique is very useful for creating a Pod resource and ad-hoc debugging.这种技术对于创建 Pod 资源和临时调试非常有用。

add this: in template ->in spec-> in container ->in ports & after container port line添加:在模板中->在规范中->在容器中->在端口中和容器端口行之后

    command: ["/bin/sh", "-ec", "while :; do echo '.'; sleep 6 ; done"]

In my case, a pod with an initContainer failed to initialize.就我而言,带有 initContainer 的 pod 无法初始化。 Running docker ps -a and then docker logs exited-container-id-here gave me a log message which kubectl logs podname didn't display.运行docker ps -a然后docker logs exited-container-id-here给了我一条日志消息, kubectl logs podname没有显示。 Mystery solved :-)谜团已揭开 :-)

有许多不同的方法可以实现这一点,但最优雅的方法之一是:

kubectl run -i --tty --image ubuntu:latest ubuntu-test --restart=Never --rm /bin/sh

I'm now trying to run a simple container with shell (/bin/bash) on a Kubernetes cluster.我现在正在尝试在Kubernetes集群上运行带有shell(/ bin / bash)的简单容器。

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).我以为有一种方法可以通过使用pseudo-tty和detach选项( -td docker run命令上的-td选项)来保持容器在Docker容器上docker run

For example,例如,

$ sudo docker run -td ubuntu:latest

Is there an option like this in Kubernetes? Kubernetes中有这样的选择吗?

I've tried running a container by using a kubectl run-container command like:我试过使用kubectl run-container命令kubectl run-container例如:

kubectl run-container test_container ubuntu:latest --replicas=1

But the container exits for a few seconds (just like launching with the docker run command without options I mentioned above).但是容器会退出几秒钟(就像使用不带我上面提到的选项的docker run命令启动一样)。 And ReplicationController launches it again repeatedly.然后ReplicationController反复再次启动它。

Is there a way to keep a container running on Kubernetes like the -td options in the docker run command?有没有办法像docker docker run命令中的-td选项那样保持容器在Kubernetes上docker run

I did a hack by putting it in background:我把它放在后台做了一个黑客:

[root@localhost ~]# kubectl run hello -it --image ubuntu -- bash &
[2] 128461

Exec on pod hello在 pod 上执行hello

[root@localhost ~]# kubectl exec -it hello -- whoami
root
[root@localhost ~]# kubectl exec -it hello -- hostname
hello

Getting a shell得到一个壳

[root@localhost ~]# kubectl exec -it hello -- bash
root@hello:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

This command may help此命令可能会有所帮助

CMD exec /bin/bash -c "trap : TERM INT; sleep i infinity & wait"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 kube.netes 中停止 I/O 日志容器 - how to stop I/O log container in kubernetes 如何在 kubernetes 容器/pod 中进行日志轮换? - How can I get log rotation working inside a kubernetes container/pod? 将角色传递给运行在 kube.netes 上的 docker 容器 - Pass Role to docker container running on kubernetes 在 GCP 中,我可以查看 GKE Monitoring 仪表板。 如何为 Kube.netes 容器的 CPU 和 memory 利用率创建警报 - In GCP , I can able to view GKE Monitoring dashboard. How to create alerts for CPU and memory Utilization for Kubernetes Container 如何检查正在运行的 Google Cloud Run 容器实例的数量? - How can I check how many instances of a Google Cloud Run container are running? 当我的程序在集群外运行时,如何使用 AWS 使用 .NET 向 EKS Kube.netes API 进行身份验证? - How can I use AWS to authenticate to the EKS Kubernetes API using .NET when my program is running outside the cluster? 如何使用 Fargate 在 AWS ECS 中正在运行的容器中运行命令 - How can I run commands in a running container in AWS ECS using Fargate Kube.netes上运行的MySQL如何连接 - How to connect MySQL running on Kubernetes 如何为在 AWS ECS 中运行的 Docker 容器配置“ulimits”? - How do I configure "ulimits" for a Docker container running in AWS ECS? 我怎样才能 SSH 到我的 GCP Kube.netes 集群? - How can I SSH to my GCP Kubernetes cluster?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM