【问题标题】:why busybox container is in completed state instead of running state为什么busybox容器处于完成状态而不是运行状态
【发布时间】:2020-08-25 01:27:10
【问题描述】:

我正在使用busybox 容器来理解kubernetes 的概念。 但是如果运行一个简单的test-pod.yaml 带有繁忙的框图像,它处于完成状态而不是运行状态 谁能解释一下原因

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "ls /etc/config/" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        # Provide the name of the ConfigMap containing the files you want
        # to add to the container
        name: special-config
  restartPolicy: Never

【问题讨论】:

  • 容器执行了给终止的命令,因此它终止了。

标签: kubernetes


【解决方案1】:

看,你应该明白这里的基本概念了。您的 docker 容器将一直运行,直到其主进程生效。一旦你的主进程停止,它就会完成。

在您的情况下逐步进行: 你用主进程"/bin/sh", "-c", "ls /etc/config/" 启动busybox 容器,显然这个进程已经结束。一旦命令完成并且您列出了目录 - 您的进程抛出 Exit:0 status,容器停止工作,您会看到已完成的 pod。

如果你想运行容器更长的时间,你应该在主进程中显式运行一些命令,这将使你的容器在你需要的时候运行。

可能的解决方案

  • @Daniel 的回答 - 容器将执行 ls /etc/config/ 并将额外存活 3600 秒

  • 使用sleep infinity 选项。请注意,很久以前有一个问题,当这个选项不能与busybox完全正常工作时。这已在 2019 年修复,更多信息 here。实际上这不是 INFINITY 循环,但是对于任何测试目的来说它应该足够了。您可以在Bash: infinite sleep (infinite blocking) 线程中找到大量解释

例子:

apiVersion: v1
kind: Pod
metadata: 
  name: busybox-infinity
spec: 
  containers: 
    - 
      command: 
        - /bin/sh
        - "-c"
        - "ls /etc/config/"
        - "sleep infinity"
      image: busybox
      name: busybox-infinity
  • 您可以使用不同的 while 循环、拖尾等。这仅取决于您的想象力和需求。

例子:

["sh", "-c", "tail -f /dev/null"]

command: ["/bin/sh"]
args: ["-c", "while true; do echo hello; sleep 10;done"]

command: [ "/bin/bash", "-c", "--" ]
args: [ "while true; do sleep 30; done;" ]

【讨论】:

    【解决方案2】:

    那是因为busybox运行命令并退出。您可以通过使用以下命令更新containers 部分中的命令来解决它:

     [ "/bin/sh", "-c", "ls /etc/config/", "sleep 3600"]
    

    【讨论】:

      猜你喜欢
      • 2021-07-13
      • 1970-01-01
      • 2018-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多