为 Pod 配置使用 Projected Volume 作为存储

本页面展示了如何使用一个 projected Volume 将多个现有卷源挂载到同一个目录下。目前,secretconfigMapdownwardAPIserviceAccountToken 这些卷类型可以被投影。

开始之前

你需要拥有一个 Kubernetes 集群,并且 kubectl 命令行工具已经被配置为与集群通信。建议你在至少有两个非控制平面节点(control plane host)的集群上运行本教程。如果你还没有集群,可以使用 minikube 创建一个,或者使用以下 Kubernetes 游乐场之一

要检查版本,请输入 kubectl version

为 Pod 配置 Projected Volume

在本练习中,你将从本地文件创建用户名和密码 Secrets。然后,你创建一个运行一个容器的 Pod,使用 projected Volume 将 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:使用 code_sample shortcode 替换 code shortcode (e8b136c3b3)