Tasks

Step-by-step instructions for performing operations with Kubernetes.

Edit This Page


approvers:

这篇教程指导如何使用配套卷来挂载几个存在的卷到相同的目录。目前,secret, configMapdownwardAPI 卷都可以作为配套卷使用。

Before you begin

You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. If you do not already have a cluster, you can create one by using Minikube, or you can use one of these Kubernetes playgrounds:

To check the version, enter kubectl version.

给Pod配置配套卷

在这个例子里,我们会从本地文件里创建用户名和密码Secretes。然后新建一个Pod来运行这个容器,使用配套 卷来挂载Secretes到同一个共享目录。

下面是Pod的配置文件:

projected-volume.yaml
apiVersion: v1
kind: Pod
metadata:
  name: test-projected-volume
spec:
  containers:
  - name: test-projected-volume
    image: busybox
    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:

    # 创建文件包含用户名和密码:
    echo -n "admin" > ./username.txt
    echo -n "1f2d1e2e67df" > ./password.txt
    
    # 将这些文件打包到secretes:
    kubectl create secret generic user --from-file=./username.txt
    kubectl create secret generic pass --from-file=./password.txt
    
  2. 创建Pod:

    kubectl create -f projected-volume.yaml
    
  3. 验证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/

What’s next

Analytics

Create an Issue Edit this Page