設定 Pod 使用 Projected Volume 進行儲存

本頁面說明如何使用 projected Volume 將多個現有的 Volume 來源掛載到同一個目錄中。目前,secretconfigMapdownwardAPIserviceAccountToken Volume 可以被 projected。

準備開始

您需要有一個 Kubernetes 叢集,並且必須將 kubectl 命令列工具設定為與您的叢集通訊。建議在至少有兩個節點且不作為控制平面主機的叢集上執行本教學課程。如果您還沒有叢集,可以使用 minikube 建立一個,或者您可以使用下列 Kubernetes Playground 之一

若要檢查版本,請輸入 kubectl version

為 Pod 設定 projected volume

在本練習中,您將從本機檔案建立使用者名稱和密碼 密鑰。然後,您將建立一個 Pod,該 Pod 執行一個容器,並使用 projected Volume 將密鑰掛載到同一個共享目錄中。

以下是 Pod 的組態檔案

apiVersion: v1
kind: Pod
metadata:
  name: test-projected-volume
spec:
  containers:
  - name: test-projected-volume
    image: busybox:1.28
    args:
    - sleep
    - "86400"
    volumeMounts:
    - name: all-in-one
      mountPath: "/projected-volume"
      readOnly: true
  volumes:
  - name: all-in-one
    projected:
      sources:
      - secret:
          name: user
      - secret:
          name: pass
  1. 建立密鑰

    # Create files containing the username and password:
    echo -n "admin" > ./username.txt
    echo -n "1f2d1e2e67df" > ./password.txt
    
    # Package these files into secrets:
    kubectl create secret generic user --from-file=./username.txt
    kubectl create secret generic pass --from-file=./password.txt
    
  2. 建立 Pod

    kubectl apply -f https://k8s.io/examples/pods/storage/projected.yaml
    
  3. 驗證 Pod 的容器是否正在執行,然後監看 Pod 的變更

    kubectl get --watch pod test-projected-volume
    

    輸出看起來像這樣

    NAME                    READY     STATUS    RESTARTS   AGE
    test-projected-volume   1/1       Running   0          14s
    
  4. 在另一個終端機中,取得執行中容器的 Shell

    kubectl exec -it test-projected-volume -- /bin/sh
    
  5. 在您的 Shell 中,驗證 projected-volume 目錄是否包含您的 projected 來源

    ls /projected-volume/
    

清除

刪除 Pod 和密鑰

kubectl delete pod test-projected-volume
kubectl delete secret user pass

下一步

上次修改時間:2023 年 8 月 24 日下午 6:38 PST:Use code_sample shortcode instead of code shortcode (e8b136c3b3)