【问题标题】:Kubernetes - Trying to create pod with init containerKubernetes - 尝试使用 init 容器创建 pod
【发布时间】:2019-08-15 20:37:07
【问题描述】:

我正在尝试使用 init pod。我想使用init container 创建文件和默认容器来检查文件是否存在并休眠一段时间。

我的 yaml:

apiVersion: v1
kind: Pod
metadata:
  name: init-test-pod
spec:
  containers:
  - name: myapp-container
    image: alpine
    command: ['sh', '-c', 'if [ -e /workdir/test.txt ]; then sleep 99999; fi']
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', 'mkdir /workdir; echo>/workdir/test.txt']

当我尝试从 alpine 映像调试时,我使用以下命令创建:

kubectl run alpine --rm -ti --image=alpine /bin/sh

If you don't see a command prompt, try pressing enter.
/ # if [ -e /workdir/test.txt ]; then sleep 3; fi
/ # mkdir /workdir; echo>/workdir/test.txt
/ # if [ -e /workdir/test.txt ]; then sleep 3; fi
/ *here shell sleeps for 3 seconds
/ #

似乎命令按预期工作。

但在我真正的 k8s 集群上,我只有 CrashLoopBackOff 用于主容器。

kubectl 描述 pod init-test-pod

只显示那个错误:

Containers:
  myapp-container:
    Container ID:  docker://xxx
    Image:         alpine
    Image ID:      docker-pullable://alpine@sha256:xxx
    Port:          <none>
    Host Port:     <none>
    Command:
      sh
      -c
      if [ -e /workdir/test.txt ]; then sleep 99999; fi
    State:          Waiting
      Reason:       CrashLoopBackOff
    Last State:     Terminated
      Reason:       Completed
      Exit Code:    0
    Ready:          False
    Restart Count:  3
    Environment:    <none>

【问题讨论】:

    标签: docker kubernetes


    【解决方案1】:

    这里的问题是您的主容器没有找到您创建的文件夹。当您的初始容器完成运行时,该文件夹将被擦除。您需要使用 Persistent Volume 才能在两个容器之间共享文件夹:

    kind: PersistentVolumeClaim
    apiVersion: v1
    metadata:
      name: mypvc
    spec:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 1Gi
    ---
    apiVersion: v1
    kind: Pod
    metadata:
      name: init-test-pod
    spec:
      volumes:
      - name: mypvc
        persistentVolumeClaim:
          claimName: mypvc
      containers:
      - name: myapp-container
        image: alpine
        command: ['sh', '-c', 'if [ -f /workdir/test.txt ]; then sleep 99999; fi']
        volumeMounts:
        - name: mypvc
          mountPath: /workdir
      initContainers:
      - name: init-myservice
        image: busybox:1.28
        command: ['sh', '-c', 'mkdir /workdir; echo>/workdir/test.txt']
        volumeMounts:
        - name: mypvc
          mountPath: /workdir
    

    你也可以看看emptyDir,这样你就不需要PVC了:

    apiVersion: v1
    kind: Pod
    metadata:
      name: init-test-pod
    spec:
      volumes:
      - name: mydir
        emptyDir: {}
      containers:
      - name: myapp-container
        image: alpine
        command: ['sh', '-c', 'if [ -f /workdir/test.txt ]; then sleep 99999; fi']
        volumeMounts:
        - name: mydir
          mountPath: /workdir
      initContainers:
      - name: init-myservice
        image: busybox:1.28
        command: ['sh', '-c', 'mkdir /workdir; echo>/workdir/test.txt']
        volumeMounts:
        - name: mydir
          mountPath: /workdir
    

    【讨论】:

    • 你也可以使用 Emptydir 来达到同样的效果,而不需要持久化文件。
    【解决方案2】:

    那是因为您的 2 个容器具有独立的文件系统。您需要使用emtyDir 卷共享此文件:

    apiVersion: v1
    kind: Pod
    metadata:
      name: init-test-pod
    spec:
      containers:
      - name: myapp-container
        image: alpine
        command: ['sh', '-c', 'if [ -e /workdir/test.txt ]; then sleep 99999; fi']
        volumeMounts:
        - mountPath: /workdir
          name: workdir
      initContainers:
      - name: init-myservice
        image: busybox:1.28
        command: ['sh', '-c', 'mkdir /workdir; echo>/workdir/test.txt']
        volumeMounts:
        - mountPath: /workdir
          name: workdir
      volumes:
      - name: workdir
        emptyDir: {}
    

    【讨论】:

      猜你喜欢
      • 2021-10-01
      • 2018-09-09
      • 2020-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-20
      • 1970-01-01
      相关资源
      最近更新 更多