Kubernetes 初始化容器 CrashLoopBackOff

10

我正在创建一个具有一个 init container 的 Replication controller。然而,init container 无法启动,pod 的状态为:

NAME                       READY     STATUS             RESTARTS   AGE
testcontainer   0/1       CrashLoopBackOff   12         37m

我不确定到底哪个部分出了问题,而且日志也没有帮助。我的 kubectl 服务器版本是 1.4(与客户端版本不同),因此我正在使用:

annotations:
        pod.beta.kubernetes.io/init-containers:

这里是我正在使用的复制控制器 yaml 文件。 我正在使用“hello-world”映像(而不是 nginx),以使其更快。

apiVersion: v1
kind: ReplicationController
metadata:
  name: testcontainer
spec:
  replicas: 1
  selector:
    app: nginx
  template:
    metadata:
      labels:
        app: nginx
      annotations:
        pod.beta.kubernetes.io/init-containers: '[
            {
                "name": "install",
                "image": "hello-world"
            }
        ]'
    spec:
      containers:
      - name: nginx
        image: hello-world
      dnsPolicy: Default
      nodeName: x.x.x.x

kubectl describe pod的日志:

Warning FailedSync      Error syncing pod, skipping: failed to "StartContainer" for "nginx" with CrashLoopBackOff: "Back-off 5m0s restarting failed container=nginx pod=testcontainer()"

  32m   16s     145     {kubelet x.x.x.x}  spec.containers{nginx}  Warning BackOff Back-off restarting failed docker container

当我检查nginx和testcontainer的日志时,显示运行hello-world镜像的输出,因此我想这个镜像已经成功下载并启动了。但我不确定之后出了什么问题(我甚至尝试创建一个单独的pod,使用在https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-initialization/#creating-a-pod-that-has-an-init-container上提供的示例,但仍然失败了)。

1个回答

3
我认为问题不在于初始化容器。 hello-world 镜像打印一段文本并立即退出。由于 pod 的 .spec.restartPolicy 默认为 Always,因此每次都会重新启动该 pod。

错误消息可能有点令人困惑,但由于 pod 旨在永久运行,所以即使退出代码为0,显示错误也是有道理的。

如果您想仅运行一个 pod 一次,则应使用 作业 API


既然您对init容器示例感兴趣,我修复了您的示例:

apiVersion: v1
kind: ReplicationController
metadata:
  name: testcontainer
spec:
  replicas: 1
  selector:
    app: nginx
  template:
    metadata:
      labels:
        app: nginx
      annotations:
        pod.beta.kubernetes.io/init-containers: '[
            {
                "name": "install",
                "image": "hello-world"
            }
        ]'
    spec:
      containers:
      - name: nginx
        image: nginx # <--- this image shouldn't be a single shot application
      dnsPolicy: Default
      nodeName: x.x.x.x

你好svenwltr,我认为init容器(安装)应该运行,如果成功,则应用程序容器(nginx)将启动。如果init容器成功,为什么会重新启动?(“始终”重启策略不应仅在失败时适用吗?)我也尝试了这个例子,但是init容器仍然失败: https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-initialization/#creating-a-pod-that-has-an-init-container我看到“PodInitializig”,但它从未完成。 - greg
换言之,我如何在使用 hello-world 镜像的复制控制器上运行一个 init 容器(而不是作业)? - greg
您的 init 容器运行良好,但在 init 容器之后,名为 nginx 的容器开始运行。该容器也使用了 hello-world 镜像。您应该替换此镜像。 - svenwltr
如果 kubectl exec POD -c NAME 不起作用,我猜它还没有被实现。 - svenwltr
如果 init 容器休眠足够长的时间,您实际上可以使用 exec -c。您知道 init 容器中的 volumeMounts(或其他环境变量)是否与应用容器共享?或者它们是完全独立的? - greg
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接