在 Pod 中使用映象卷

功能狀態: `Kubernetes v1.33 [beta]` (預設啟用:false)

本頁面展示瞭如何使用映象捲來配置 Pod。這允許你在容器內部掛載來自 OCI 登錄檔的內容。

準備工作

你需要有一個 Kubernetes 叢集,並且 kubectl 命令列工具已配置為與你的叢集通訊。建議在本教程中在至少有兩個不充當控制平面主機的節點組成的叢集上執行。如果你還沒有叢集,你可以透過使用 minikube 來建立一個叢集,或者你可以使用這些 Kubernetes 演練場中的一個。

你的 Kubernetes 伺服器版本必須是 v1.31 或更高版本。

要檢查版本,請輸入 kubectl version

  • 容器執行時需要支援映象卷功能。
  • 你需要能夠在主機中執行命令。
  • 你需要能夠進入 Pod 內部執行命令。
  • 你需要啟用 ImageVolume 功能門控

執行一個使用映象卷的 Pod

Pod 的映象卷透過將 .specvolumes.[*].image 欄位設定為有效引用,並在容器的 volumeMounts 中使用它來啟用。例如:

apiVersion: v1
kind: Pod
metadata:
  name: image-volume
spec:
  containers:
  - name: shell
    command: ["sleep", "infinity"]
    image: debian
    volumeMounts:
    - name: volume
      mountPath: /volume
  volumes:
  - name: volume
    image:
      reference: quay.io/crio/artifact:v2
      pullPolicy: IfNotPresent
  1. 在你的叢集上建立 Pod。

    kubectl apply -f https://k8s.io/examples/pods/image-volumes.yaml
    
  2. 附加到容器。

    kubectl attach -it image-volume bash
    
  3. 檢查卷中檔案的內容。

    cat /volume/dir/file
    

    輸出類似於:

    1
    

    你也可以檢查不同路徑下的另一個檔案。

    cat /volume/file
    

    輸出類似於:

    2
    

使用 subPath(或 subPathExpr

在 Kubernetes v1.33 中,使用映象卷功能時,可以使用 subPathsubPathExpr

apiVersion: v1
kind: Pod
metadata:
  name: image-volume
spec:
  containers:
  - name: shell
    command: ["sleep", "infinity"]
    image: debian
    volumeMounts:
    - name: volume
      mountPath: /volume
      subPath: dir
  volumes:
  - name: volume
    image:
      reference: quay.io/crio/artifact:v2
      pullPolicy: IfNotPresent
  1. 在你的叢集上建立 Pod。

    kubectl apply -f https://k8s.io/examples/pods/image-volumes-subpath.yaml
    
  2. 附加到容器。

    kubectl attach -it image-volume bash
    
  3. 檢查卷中 dir 子路徑下檔案的內容。

    cat /volume/file
    

    輸出類似於:

    1
    

進一步閱讀

最後修改時間:2025 年 2 月 27 日上午 9:19 PST:[KEP-4639] 將映象卷源提升到 Beta (eab681370c)