配置 Pod 使用投射捲進行儲存

本頁面展示瞭如何使用projected卷將多個現有卷源掛載到同一個目錄中。目前,secretconfigMapdownwardAPIserviceAccountToken卷可以被投射。

準備工作

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

要檢查版本,請輸入 kubectl version

為 Pod 配置投射卷

在這個練習中,你將從本地檔案建立使用者名稱和密碼Secrets。然後,你將建立一個執行一個容器的 Pod,使用projected卷將這些 Secrets 掛載到同一個共享目錄中。

以下是 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. 建立 Secrets

    # 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 目錄包含你的投射源

    ls /projected-volume/
    

清理

刪除 Pod 和 Secrets

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

下一步

最後修改於 2023 年 8 月 24 日下午 6:38 PST:使用 code_sample 簡碼代替 code 簡碼 (e8b136c3b3)