kubectl pod部署后不存在k8s initContainer mountPath

下面是部署yaml,部署后,我可以访问pod 我可以看到mountPath“ / usr / share / nginx / html”,但是找不到 “ / work-dir”应该由initContainer创建。 有人可以解释一下原因吗? 谢谢和Rgds

apiVersion: v1
kind: pod
metadata:
 name: init-demo
spec:
 containers:
 - name: nginx
   image: nginx
   ports:
   - containerPort: 80
   volumeMounts:
   - name: workdir
     mountPath: /usr/share/nginx/html
 # These containers are run during pod initialization
 initContainers:
 - name: install
   image: busybox
   command:
   - wget
   - "-O"
   - "/work-dir/index.html"
   - http://kubernetes.io
   volumeMounts:
   - name: workdir
     mountPath: "/work-dir"
 dnsPolicy: Default
 volumes:
 - name: workdir
   emptyDir: {}
Sdfadfadf 回答:kubectl pod部署后不存在k8s initContainer mountPath

“ / work-dir”中的卷由init容器安装,而“ / work-dir”位置仅存在于init容器中。当init容器完成时,其文件系统消失了,因此init容器中的“ / work-dir”目录已“消失”。应用程序(nginx)容器也安装相同的(尽管位于不同的位置),从而为两个容器共享其内容提供了机制。

根据docs

  

初始化容器可以使用与文件系统不同的视图来运行   同一容器中的应用容器。

,

带有PVC的卷装入允许您共享/work-dir//use/share/nginx/html/的内容,但这并不意味着nginx container将具有/work-dir文件夹。鉴于此,您可能认为您可以只安装路径/,该路径将允许您共享下面的所有文件夹。但是,mountPath不适用于/

那么,您如何解决您的问题?如果您确实需要该文件夹,则可以使用另一个Pod挂载/work-dir/。这是一个示例(带有挂架的pvc和部署):

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: shared-fs-pvc
  namespace: default
  labels:
    mojix.service: default-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: default
  name: shared-fs
  labels:
    mojix.service: shared-fs
spec:
  replicas: 1
  selector:
    matchLabels:
      mojix.service: shared-fs
  template:
    metadata:
      creationTimestamp: null
      labels:
        mojix.service: shared-fs
    spec:
      terminationGracePeriodSeconds: 3
      containers:
      - name: nginx-c
        image: nginx:latest
        volumeMounts:
          - name: shared-fs-volume
            mountPath: /var/www/static/
      - name: alpine-c
        image: alpine:latest
        command: ["/bin/sleep","10000s"]
        lifecycle:
          postStart:
            exec:
              command: ["/bin/mkdir","-p","/work-dir"]
        volumeMounts:
          - name: shared-fs-volume
            mountPath: /work-dir/
      volumes:
      - name: shared-fs-volume
        persistentVolumeClaim:
          claimName: shared-fs-pvc
本文链接:https://www.f2er.com/3121382.html

大家都在问