尝试使用init容器创建pod

问题描述 投票:1回答:2

我想尝试使用init pods。我想使用init容器创建文件和默认容器来检查文件是否存在并暂停一段时间

我的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']

当Iam尝试从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
2个回答
3
投票

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

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: mypvc
    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

1
投票

那是因为你的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: {}
© www.soinside.com 2019 - 2024. All rights reserved.