配置 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)