kubernetes init容器CrashLoopBackOff

我正在创建一个带有一个init-container的Replication控制器.但是init容器无法启动,并且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的日志描述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图像的输出,所以我猜想图像已经下载并成功启动了.我不确定之后失败了什么(我甚至尝试使用https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-initialization/#creating-a-pod-that-has-an-init-container上提供的示例创建单个pod,但仍然失败)

谢谢!

最佳答案 我认为这里的问题不是init容器. hello-world图像打印文本并立即退出.由于pod的
.spec.restartPolicy默认为Always,因此每次都会重新启动pod.

错误消息可能有点令人困惑,但由于pod要永远运行,因此即使退出代码为0,显示错误也很有意义.

如果您只想运行一次pod,则应使用job API.

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

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
点赞